Populating form with existing MySQL data

Thread Solved

Join Date: Feb 2008
Posts: 24
Reputation: dwlamb_001 is an unknown quantity at this point 
Solved Threads: 0
dwlamb_001 dwlamb_001 is offline Offline
Newbie Poster

Populating form with existing MySQL data

 
0
  #1
Feb 19th, 2008
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:
  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
    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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating form with existing MySQL data

 
0
  #2
Feb 19th, 2008
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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: coffeepot! is an unknown quantity at this point 
Solved Threads: 2
coffeepot!'s Avatar
coffeepot! coffeepot! is offline Offline
Newbie Poster

Re: Populating form with existing MySQL data

 
0
  #3
Feb 19th, 2008
All looks okay to me but you could try the following mysql_fetch_assoc($result) instead of array.
Anything one man can make - another man will try to break.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 24
Reputation: dwlamb_001 is an unknown quantity at this point 
Solved Threads: 0
dwlamb_001 dwlamb_001 is offline Offline
Newbie Poster

Re: Populating form with existing MySQL data

 
0
  #4
Feb 19th, 2008
Originally Posted by nav33n View Post
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 24
Reputation: dwlamb_001 is an unknown quantity at this point 
Solved Threads: 0
dwlamb_001 dwlamb_001 is offline Offline
Newbie Poster

Re: Populating form with existing MySQL data

 
0
  #5
Feb 19th, 2008
Hello again,

I worked on this some more and came up with the following to populate data in the form fields;
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating form with existing MySQL data

 
0
  #6
Feb 19th, 2008
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
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: coffeepot! is an unknown quantity at this point 
Solved Threads: 2
coffeepot!'s Avatar
coffeepot! coffeepot! is offline Offline
Newbie Poster

Re: Populating form with existing MySQL data

 
0
  #7
Feb 20th, 2008
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.
Anything one man can make - another man will try to break.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 68
Reputation: djjjozsi is an unknown quantity at this point 
Solved Threads: 10
djjjozsi djjjozsi is offline Offline
Junior Poster in Training

Re: Populating form with existing MySQL data

 
0
  #8
Jun 21st, 2009
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.

  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 ...";
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC