943,546 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 14306
  • PHP RSS
Feb 19th, 2008
0

Populating form with existing MySQL data

Expand Post »
I've searched through the threads and I can't find what I am looking for exactly. I apologise if I'm posting something already resolved for someone else.

I have a page for browsing the contents of a database. An option for each of those records is to update/edit. The user clicks an edit button and a new page is called using $_GET to specify the record id and query the database for all fields in that record. I want my form to be populated with those fields so I can edit the data. This is the code so far:
php Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <?php
  4.  
  5. //from the url, get the id
  6. $id = $_GET["id"];
  7. if (!$id)
  8. {
  9. die("Variable id not defined. Script terminating.");
  10. }
  11. //connect to the database
  12. $con = mysql_connect("localhost","root");
  13. if (!$con)
  14. {
  15. die('Could not connect: ' . mysql_error());
  16. }
  17. //select Jokes database
  18. $db=mysql_select_db("jokes", $con) or die(mysql_error());
  19. //define the query
  20. $sql="SELECT * FROM data WHERE id=".$id."";
  21.  
  22. echo "<head>";
  23. echo " <title>Editing record id=".$id."</title>";
  24. echo "</head>
  25. ";
  26. echo "<body>
  27. ";
  28.  
  29. $result = mysql_query($sql, $con) or die('Could not connect: ' . mysql_error());
  30. If (!$result) {
  31. die('Result not returned:' . mysql_error());
  32. }
  33.  
  34. //close the connection
  35. mysql_close();
  36. while ($list = mysql_fetch_array($result)){
  37. echo "<form ENCTYPE=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
  38. ";
  39. echo "<input type=\"text\" value=\"".$list['category']."\" maxlength=\"64\" name=\"category\" size=\"25\" /><br />
  40. ";
  41. echo "<input type=\"text\" value=\"".$list['title']."\" maxlength=\"128\" name=\"category\" size=\"25\" /><br />
  42. ";
  43. echo "<textarea cols=\"80\" rows=\"10\" wrap=\"ON\" name=\"body\">
  44. $list['localstyles']</textarea><br />
  45. ";
  46. echo "<textarea cols=\"80\" rows=\"15\" wrap=\"ON\" name=\"body\">
  47. $list['body']</textarea><br />
  48. ";
  49. echo "<button type=\"submit\">Update</button>
  50. ";
  51. echo "</form>";
  52. }
  53. ?>
  54. </body>
  55. </html>

The latter part in the while {} statement is the problem. I've done the one quoted above and variations that have provided the form but not the data associated with the variables. Inspecting the HTML results displays value="" in each of the forms tags. I know the array is being returned for testing I have done using print_r(mysql_fetch_array($result)) displays the data.

I realise there may be several ways to do this and would like help as to the most efficient that will set-up the form and populate it with the existing array of data. I know the following:
  1. I'm only going to have one row to work with. By querying on the id, it's only going to return one record.
  2. To iterate the data, I'll need to do it using an inline method like the code quoted above or assign it to variables using mysql_result before the form, then include each of those variables in the forrm.
  3. For defining the form, I can do it the way it's quoted or use a method using
    php Syntax (Toggle Plain Text)
    1. echo <<<FORMENT
    2. <form action = "{$_SERVER['PHP_SELF']}" method = "post">
    3.  
    4. quoted form text and textareas as I would in html
    5.  
    6. </form>
    7. FORMENT;
    I picked up that method for creating a form for data entry on a tutorial but I don't know how to make it work to populate the fields with existing data. As well, I can't find documentation for that echo <<< FORMENT ... FORMENT; syntax. The site I obtained it from has since removed its tutorials and gone over to a forum only format.

I know that I will still have code to write for updating the record once the Update button is clicked and I know that part.

If you can't tell, I'm a newbie to all of this. I would really appreciate on this and thank you for reading this far.

Dan
Reputation Points: 10
Solved Threads: 0
Light Poster
dwlamb_001 is offline Offline
32 posts
since Feb 2008
Feb 19th, 2008
0

Re: Populating form with existing MySQL data

Umm.. Are you working on linux by any chance ? Because in linux, the column names are case-sensitive. If you use $list['name'] in your query and you have NAME as a column name, then it would return null ! That might be the problem. Print out $list['category']. There must be something wrong ! And check this link for the syntax of heredoc.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 19th, 2008
0

Re: Populating form with existing MySQL data

All looks okay to me but you could try the following mysql_fetch_assoc($result) instead of array.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
coffeepot! is offline Offline
11 posts
since Feb 2008
Feb 19th, 2008
0

Re: Populating form with existing MySQL data

Click to Expand / Collapse  Quote originally posted by nav33n ...
Umm.. Are you working on linux by any chance ? Because in linux, the column names are case-sensitive. If you use $list['name'] in your query and you have NAME as a column name, then it would return null ! That might be the problem. Print out $list['category']. There must be something wrong ! And check this link for the syntax of heredoc.
Thanks for the input. No, I am not on Linux. I'm aware of the need for case consistency and have adopted the practise of using field names in the case they are defined as in the database. That way if I ever work on a Linux based project, I don't have to "unlearn" bad habits.

After posting my code here, i noticed inconsistency in the use of quotes and double-quotes in the variable I wanted output. I went back to my original code and tried with different variations. The variables were still empty.
Last edited by dwlamb_001; Feb 19th, 2008 at 10:56 pm. Reason: Fixed a typo
Reputation Points: 10
Solved Threads: 0
Light Poster
dwlamb_001 is offline Offline
32 posts
since Feb 2008
Feb 19th, 2008
0

Re: Populating form with existing MySQL data

Hello again,

I worked on this some more and came up with the following to populate data in the form fields;
php Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <?php
  4. ...Save as originally posted....
  5.  
  6. $result = mysql_query($sql, $con) or die('Could not connect: ' . mysql_error());
  7. If (!$result) {
  8. die('Result did not function:' . mysql_error());
  9. }
  10. //determine number of rows in the result
  11. $num=mysql_numrows($result);
  12.  
  13. //close the connection
  14. mysql_close();
  15. $i=0;
  16. while ($i < $num) {
  17. $id=mysql_result($result,$i,"id");
  18. $category=mysql_result($result,$i,"category");
  19. $title=mysql_result($result,$i,"title");
  20. $localstyles=mysql_result($result,$i,"localstyles");
  21. $body=mysql_result($result,$i,"body");
  22. $i++;
  23. }
  24. echo "<form ENCTYPE=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."?id=".$id."\" method=\"post\">\n";
  25. echo "<input type=\"text\" value=\"".stripslashes($category)."\" maxlength=\"64\" name=\"category\" size=\"25\" /><br />\n";
  26. echo "<input type=\"text\" value=\"".stripslashes($title)."\" maxlength=\"128\" name=\"title\" size=\"64\" /><br />\n";
  27. echo "<textarea cols=\"80\" rows=\"5\" wrap=\"ON\" name=\"localstyles\">".stripslashes($localstyles)."</textarea><br />\n";
  28. echo "<textarea cols=\"80\" rows=\"15\" wrap=\"ON\" name=\"body\">".stripslashes($body)."</textarea><br />\n";
  29. echo "<button type=\"submit\">Update</button>
  30. ";
  31. echo "</form>";
  32.  
  33. ?>
  34. </body>
  35. </html>

Why it is working under this method as opposed to the previous one, I don't know. I tried coffepot's suggestion of mysql_fetch_assoc($result) and the variables still did not display.

This code does include the stripslashes function but that's a recent add-on. I had the code working on this arrangement before the stripslashes were added.

I guess this one can be marked as solved but I'd still appreciate it if someone can compare the two processes and establish why the first one failed.

Have a Great Day!

Dan
Reputation Points: 10
Solved Threads: 0
Light Poster
dwlamb_001 is offline Offline
32 posts
since Feb 2008
Feb 19th, 2008
0

Re: Populating form with existing MySQL data

mysql_fetch_array does the work of both mysql_fetch_row and mysql_fetch_assoc. (mysql_fetch_array can take an extra parameter!). Anyway, stripslashes just takes off the added slashes. unless you are storing the values using addslashes, You don't have to use stripslashes. Print out what's in $category. If it has slashes, then probably, that slash 'escaped' the " character and maybe that was the reason you were getting null ?

Cheers,
Naveen
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 20th, 2008
0

Re: Populating form with existing MySQL data

Well I have to do this exact same operation. The only difference I can see, and this code below is tested and works fine, is the way i construct the HTML...

while($row=mysql_fetch_assoc($result)){
echo("<form name='myform' action='action_form.php' method='post'><tr><input type='hidden' name='control' value='".$row['id']."'><td>".$row['id']."</td><td>".$row['menuitem']."</td><td>".$row['pagetitle']."</td>
<td><input type='submit' name='submit' value='Do This'></td></tr></form>");
}

The difference is I wrap the values in single quotes.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
coffeepot! is offline Offline
11 posts
since Feb 2008
Jun 21st, 2009
0

Re: Populating form with existing MySQL data

in teh second code you have a mysql_numrows function, which is bad. you mean mysql_num_rows

that method is quite the longest way you fetch the values from a database.

php Syntax (Toggle Plain Text)
  1. $sql_edit="select * from `table`";
  2. $result_result=mysql_query($sql_edit) or die(mysql_error());
  3. if(mysql_num_rows($result_result)>0)
  4. {
  5. while($row=mysql_fetch_assoc($result_result))
  6. {
  7. print '<tr>';
  8. echo "<td>". ( empty($row["category"]) ? "&nbsp;" : (htmlspecialchars($row["category"])) )."</td>";
  9. echo "<td>". ( empty($row["title"]) ? "&nbsp;" : (htmlspecialchars($row["title"])) )."</td>";
  10. echo "<td>". ( empty($row["localstyles"]) ? "&nbsp;" : (htmlspecialchars($row["localstyles"])) )."</td>";
  11. echo "<td>". ( empty($row["body"]) ? "&nbsp;" : (htmlspecialchars($row["body"])) )."</td>";
  12. echo "<td><a href=\"?edit={$row["id"]}\">Edit</a></td>";
  13. print '</tr>';
  14. }
  15. }
  16. else
  17. echo "No result in result table ...";
Reputation Points: 12
Solved Threads: 11
Junior Poster in Training
djjjozsi is offline Offline
69 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Badword Filter (Intermediate)
Next Thread in PHP Forum Timeline: how to not update when there is a value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC