0

I've created a database with a table called pages in it.

Within this table there are fields called pages_id, pagename, pagedesc, pagekey and pagecont.

How can I pull the data from these fields in the database, in to editable text fields? I've tried below, but my code isn't working :(

<?php
// Make a MySQL Connection
mysql_connect("####", "####", "####") or die(mysql_error());
mysql_select_db("####") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages") 
or die(mysql_error());  

?>
<p>Page title</p><br />
<?php echo"<input name=\"pagename\" type=\"text\" id=\"pagename\" value=\"" .$pagename. "\">"; ?>

<p>Keywords</p><br />
  <?php echo"<input name=\"pagekey\" type=\"text\" id=\"pagekey\" value=\"" .$pagekey. "\">"; ?>

<p>Description</p><br />
<?php echo"<input name=\"pagedesc\" type=\"text\" id=\"pagedesc\" value=\"" .$pagedesc. "\">"; ?>

<p>Content</p><br />
<?php echo "<textarea name=\"pagecont\" cols=\"120\" rows=\"20\" id=\"pagecont\" >".$pagecont."</textarea>";?>

7 Answers 7

1

You haven't fetched a row from your query result, and are outputting undefined variables. The proper sequence should be:

$result = mysql_query($sql) or die(mysql_error()); // run the query
$row = mysql_fetch_assoc($row); // fetch a result row

echo $row['name_of_field']; // output one of the result row's data fields.

Also note that when you're outputting data into an HTML form as you are, you have to make sure that nothing in the data you're outputting will 'break' the form. e.g. If the output text contains a ", and you're using those quotes around your form field's attributes, you'll "break" the html. best practice for output-to-html is to use:

<input type="text" name="somefield" value="<?php echo htmlspecialchars($row['name_of_field']) ?>" />
Sign up to request clarification or add additional context in comments.

Comments

0

Hopefully you know how to connect to MySQL. I included a dummy connection just for reference.

<?php   
    //Open connection

    $qry="SELECT * FROM <table>";
    $result=mysql_query($qry);


    while($row = mysql_fetch_array($result))
    {
       $variable = $row["column_name"];
    }
?>

The HTML:

<input type="text" id="test" value="<?=$variable?>">

If you're trying to get specific results, consider using a WHERE clause in your SQL statement. See here for more info: W3Schools

3 Comments

Don't do "<?=$variable?>", that's nasty shorthand. Do <?php echo $variable ?> instead.
Why? I was always taught it was perfectly valid & fine.
Yes it's valid, but <? can lead to problems with other parsers and it's just not readable or necessary.
0
<?php
// Make a MySQL Connection
mysql_connect("####", "####", "####") or die(mysql_error());
mysql_select_db("####") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages") or die(mysql_error());

$result = mysql_fetch_array($result, MYSQL_ASSOC);

foreach($result as $page): ?>

    <p>Page title</p><br />
    <input name="pagename" type="text" id="pagename" value="<?= $page['pagename']; ?>">

    <p>Keywords</p><br />
    <input name="pagekey" type="text" id="pagekey" value="<?= $page['pagekey']; ?>">

    <p>Description</p><br />
    <input name="pagedesc" type="text" id="pagedesc" value="<?= $page['pagedesc']; ?>">

    <p>Content</p><br />
    <textarea name="pagecont" cols="120" rows="20" id="pagecont" ><?= $page['pagecont']; ?></textarea>
    <hr />

<?php endforeach; ?>

1 Comment

Where are $pagename,$pagekey etc defined? Also, you could use mysql_fetch_assoc(), and check if $result isn't false before assuming it's an array. Oh, and yu should convert the entities, or a double quote in the string would brake your html (assuming you trust your source, otherwise XSS attacks could also be a problem)
0

You're running the query, but not fetching the results from it. Try this:

$fields = mysql_fetch_assoc($result);

Then you can refer to the results of the query with $fields['pagename'] and so forth.

Documentation here. You may want to look at the docs for the mysql module in general.

Comments

0

Using a form like you have, you can edit one (1) row at a time. Hence you need 2 pages. Page #1 to list all pages in a table with a hyperlink to the edit page passing the pagekey as GET parameter and Page #2 that is your edit page which received a page ID as GET parameter.

Page #2 URL can be:

http://domain.com/editpage.php?pagekey=abc

Code for it will be as follows:

<?php
// Make a MySQL Connection
mysql_connect("####", "####", "####") or die(mysql_error());
mysql_select_db("####") or die(mysql_error());

$key = $_GET['pagekey'];

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages where pagekey = $key")
    or die(mysql_error());  

$row = mysql_fetch_assoc($result);

?>
<p>Page title</p><br />
<?php echo"<input name=\"pagename\" type=\"text\" id=\"pagename\" value=\"" .$row['pagename']. "\">"; ?>

<p>Keywords</p><br />
  <?php echo"<input name=\"pagekey\" type=\"text\" id=\"pagekey\" value=\"" .$row['pagekey']. "\">"; ?>

<p>Description</p><br />
<?php echo"<input name=\"pagedesc\" type=\"text\" id=\"pagedesc\" value=\"" .$row['pagedesc']. "\">"; ?>

<p>Content</p><br />
<?php echo "<textarea name=\"pagecont\" cols=\"120\" rows=\"20\" id=\"pagecont\" >".$row['pagecont']."</textarea>";?>

The above code is very basic but will serve your requirement for the moment to display the row contents in editable input boxes.

Comments

0

I created this function which will work to retrieve all the data from a column:

 function page_data($fieldname){

              $result = mysql_query("SELECT ".$fieldname." FROM pages");

              while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                  $arrdata[] =  $row[$fieldname];
              }

              return $arrdata;   
          }

retrieves all data from pages table where column is 'pagename'

    $pagename=array();
    $pagename=page_data('pagename');
    $numpages=count($pagename)-1;

for each value in pagename echo the value into a textbox

    for($i=0; $i <= $numpages; $i++){
    echo '<input name="pagename" type="text" value=".$pagename[$i]."/>';
    }

Comments

0
<?php
  // Make a MySQL Connection
  mysql_connect("####", "####", "####") or die(mysql_error());
  mysql_select_db("####") or die(mysql_error());

  // Get all the data from the "example" table
  $result = mysql_query("SELECT * FROM pages") or die(mysql_error());
  while($row = mysql_fetch_array($result))
  {
?>

  <p>Page title</p><br />
  <input name="pagename" type="text" id="pagename" value="<?php echo $row['pagename']; ?>">
  <p>Keywords</p><br />
  <input name="pagekey" type="text" id="pagekey" value="<?php echo $row['pagekey']; ?>">
  <p>Description</p><br />
  <input name="pagedesc" type="text" id="pagedesc" value="<?php echo $row['pagedesc']; ?>">
  <p>Content</p><br />
  <textarea name="pagecont" cols="120" rows="20" id="pagecont"><?php echo $row['pagecont']; ?></textarea>

<?php
  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.