Nucleus Posted February 3, 2010 Share Posted February 3, 2010 Hello, I am using this form, to connect on MySQL database and edit the fields "FirstName", "LastName" and "PhoneNumber" in the "Employees" table. <? //connect to mysql include 'connect.php'; //If cmd has not been initialized if(!isset($cmd)) { //display all the fields $result = mysql_query("select * from Employees order by id"); //run the while loop that grabs all the fields while($r=mysql_fetch_array($result)) { //grab the title and the ID of the fields $ID=$r["ID"]; $FirstName=$r["FirstName"]; $LastName=$r["LastName"]; $PhoneNumber=$r["PhoneNumber"]; //make the title a link echo "<a href='edit.php?cmd=edit&id=$id'>$FirstName - Edit</a>"; echo "<br>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["submit"])) { $id = $_GET["ID"]; $sql = "SELECT * FROM Employees WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <form action="edit.php" method="post"> <input type=hidden name="id" value="<?php echo $myrow["id"] ?>"> First Name:<INPUT TYPE="TEXT" NAME="FirstName" VALUE="<?php echo $myrow["FirstName"] ?>" SIZE=30><br> Last Name:<INPUT TYPE="TEXT" NAME="LastName" VALUE="<?php echo $myrow["LastName"] ?>" SIZE=30><br> Phone Number:<INPUT TYPE="TEXT" NAME="PhoneNumber" VALUE="<?php echo $myrow["PhoneNumber"] ?>" SIZE=30><br> <input type="hidden" name="cmd" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } ?> <? if ($_POST["$submit"]) { $FirstName = $_POST["FirstName"]; $LastName = $_POST["LastName"]; $PhoneNumber = $_POST["PhoneNumber"]; $sql = "UPDATE Employees SET FirstName='$FirstName',LastName='$LastName',PhoneNumber='$PhoneNumber' WHERE id=$id"; //replace the fields with your table name above $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?> My problem is that I get this warning message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home1/koutouro/public_html/ioannou/edit.php on line 34 As a result I cannot update the database. I started working with PHP and MySQL yesterday, so please be patient with me. Thanks. Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/ Share on other sites More sharing options...
jammesz Posted February 3, 2010 Share Posted February 3, 2010 $sql = "SELECT * FROM Employees WHERE id=$id"; should be $sql = "SELECT * FROM Employees WHERE ID=$id"; from what i can gather from this line $ID=$r["ID"]; Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/#findComment-1005995 Share on other sites More sharing options...
Nucleus Posted February 3, 2010 Author Share Posted February 3, 2010 The warning is now gone, but the form does not display the "Thank you! Information updated" message, nor it updates the table. :-\ Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/#findComment-1006000 Share on other sites More sharing options...
jl5501 Posted February 3, 2010 Share Posted February 3, 2010 There is no evidence that the update query is being run. You might mean if(isset($_POST['submit'])) where you have if ($_POST["$submit"]) Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/#findComment-1006001 Share on other sites More sharing options...
Nucleus Posted February 3, 2010 Author Share Posted February 3, 2010 That worked. Thank you very much! Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/#findComment-1006055 Share on other sites More sharing options...
Nucleus Posted February 3, 2010 Author Share Posted February 3, 2010 It seems that for some reason when I update a record, it overwrites another. Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/#findComment-1006134 Share on other sites More sharing options...
New Coder Posted February 3, 2010 Share Posted February 3, 2010 check you are actually passing the ID value, echo it somewhere, it may be empty Link to comment https://forums.phpfreaks.com/topic/190771-warning-when-editing-mysql-using-php/#findComment-1006136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.