Fearpig Posted August 10, 2006 Share Posted August 10, 2006 Hello again Guys,I'm stuck yet again!! I've now created the following code to populate three textboxes with data from a row in MySQL (linked through the $id from the page before). I now want to edit these fields, click submit and have it update the row in the database.<html><head></head><body><?php$db = mysql_connect("localhost", "root", "password");$id = $_GET['id'];mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);$myrow = mysql_fetch_array($result)?><form action="Edit_SpecificPerson.php" method="post"><INPUT TYPE="HIDDEN" NAME="ID" VALUE="<?php echo $myrow["ID"] ?>"><br> <INPUT TYPE="TEXT" NAME="First_Name" VALUE="<?php echo $myrow["First_Name"] ?>" SIZE=30><br><INPUT TYPE="TEXT" NAME="Last_Name" VALUE="<?php echo $myrow["Last_Name"] ?>" SIZE=30><br><INPUT TYPE="TEXT" NAME="Role" VALUE="<?php echo $myrow["Role"] ?>" SIZE=30><br><input type="Submit" value="Update"></form> </body></html>I know I need another SQL query like:$query="UPDATE contacts SET First_Name='$ud_first', Last_Name='$ud_last', Role='$ud_role' WHERE id='$ud_id'";But how do I include it into this page and tie it to the submit button? If anyone could suggest some code or a tutorial I would be very grateful.Cheers.Tom. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 10, 2006 Share Posted August 10, 2006 You do not need all of those <?php ?>'s that is causeing unessicary drag on your server. You would noticed a lot faster processing time if you just echoed the entire form like this[code]<?php$db = mysql_connect("localhost", "root", "password");$id = $_GET['id'];mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);while ($myrow = mysql_fetch_assoc($result)) {echo '<form action="Edit_SpecificPerson.php" method="post"><INPUT TYPE="HIDDEN" NAME="ID" VALUE="' . $myrow["ID"] . '"> <INPUT TYPE="TEXT" NAME="First_Name" VALUE="' . $myrow["First_Name"] . '" SIZE=30><INPUT TYPE="TEXT" NAME="Last_Name" VALUE="' . $myrow["Last_Name"] . '" SIZE=30><INPUT TYPE="TEXT" NAME="Role" VALUE="' . $myrow["Role"] . '" SIZE=30><input type="Submit" value="Update"></form>';}mysql_free_result($result);?>[/code]Now to your question. I would do this. Use an [code=php:0]if (isset($_POST['whatever'])) {[/code] . Now before I get to far into this let me make sure I understand you correctly. You want to edit the information obtained from the script that you posted? If so then I would do this.[code]<?phpif (isset($_POST['submit'])) { $id = $_POST['id']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $role = $_POST['role']; $query = mysql_query("UPDATE `contacts` SET `first_name` = '$first_name', `last_name` = '$last_name', `role`= '$role' WHERE `id` = '$id'"); if (!$query) { echo "Unable to update the users information"; }else{ echo "The users information was updated"; }}?><html><head></head><body><?php$db = mysql_connect("localhost", "root", "password");$id = $_GET['id'];mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);while ($myrow = mysql_fetch_assoc($result)) {echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"><INPUT TYPE="HIDDEN" NAME="ID" VALUE="' . $myrow["ID"] . '"> <INPUT TYPE="TEXT" NAME="First_Name" VALUE="' . $myrow["First_Name"] . '" SIZE=30><INPUT TYPE="TEXT" NAME="Last_Name" VALUE="' . $myrow["Last_Name"] . '" SIZE=30><INPUT TYPE="TEXT" NAME="Role" VALUE="' . $myrow["Role"] . '" SIZE=30><input type="Submit" value="Update"></form>';}mysql_free_result($result);?></body></html>[/code]You see in the version I have cut the processing of the <?php ?> down to two. Hope this helps,Tom Quote Link to comment Share on other sites More sharing options...
Fearpig Posted August 10, 2006 Author Share Posted August 10, 2006 Cheers for that tomfmason,It looks as though that what I'm after. I'm now getting a new error though.Notice: Undefined index: PHPSELF in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 28Do you know what this is? I'm sure I've had it before but can't remember how to resolve it!Cheers again though.Tom Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 10, 2006 Share Posted August 10, 2006 sorry typo I wrote that on the fly. It is [code=php:0]$_SERVER['PHP_SELF'][/code] forgot the [b]_[/b] Also I forgot a couple ' in the query. Sees the edited version above ^Good luck,Tom Quote Link to comment Share on other sites More sharing options...
Fearpig Posted August 10, 2006 Author Share Posted August 10, 2006 Aaaaaarrrrrgggggghhhhh............I'm sooo close to cracking this now! The problem seems to be now that when you click on submit the php file refers to itself but fails when it gets to the $id variable as that is defined on the page before.(I now get the error message:Notice: Undefined index: id in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 23which is this line:$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db);)Any ideas on this one....? Quote Link to comment Share on other sites More sharing options...
dual_alliance Posted August 10, 2006 Share Posted August 10, 2006 You should edit this:[code]<input type="Submit" value="Update">[/code]to[code]<input name = "submit" type="Submit" value="Update">[/code]Because you have:[code=php:0]if (isset($_POST['submit']))[/code]But before there was no submit.....And your other problem this might fix it:[code=php:0]$db = mysql_connect("localhost", "root", "password") or die('Error : ' . mysql_error());$id = $_GET['id'];mysql_select_db("Telephonelist") or die('Error : ' . mysql_error());$result = mysql_query("SELECT * FROM tbl_telephonenumbers WHERE ID='$id'",$db) or die('Error : ' . mysql_error());[/code]<br />//Edit: "$db" isnt needed when selecting a database. Quote Link to comment Share on other sites More sharing options...
Fearpig Posted August 10, 2006 Author Share Posted August 10, 2006 Nope it didn't like that one.......Notice: Undefined index: id in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 4Notice: Undefined index: first_name in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 5Notice: Undefined index: last_name in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 6Notice: Undefined index: role in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 7Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 9Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\Intranet v3\php_Telephone_List\Edit_SpecificPerson.php on line 9Unable to update the users information Do you know where I can find a tutorial that I can follow as oppose to bugging you every few minutes! It looks as though I need to go through all of the basics again? Thanks again for your help.Tom Quote Link to comment Share on other sites More sharing options...
dual_alliance Posted August 10, 2006 Share Posted August 10, 2006 I'm sorry l couldn't get it working. It wouldn't even display :( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.