Seaholme Posted July 1, 2010 Share Posted July 1, 2010 Hey all, I've been trying to get a script to run which will allow me to use PHP to update fields in the database. I found http://www.phpeasystep.com/mysql/9.html this website which explains how to do it, and followed all the steps. Every one of them has worked except for my attempt at the final step (Create file update_ac.php) which throws up the message it gives when the whole thing has been unsuccessful (ERROR). Can anybody help me work out what it is about it that is causing the error to be thrown up? Also: could it be in one of the previous pages? <?php $dbhost = 'xxxxx'; $dbuser = 'xxxx $dbpass = 'xxxxx $dbname = 'xxxxx'; $tbl_name="players"; // Table name // Connect to server and select database. mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); // update data in mysql database $sql="UPDATE players SET displayname='$displayname', campname='$campname', WHERE id='$id'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; } else { echo "ERROR"; } ?> Thanks! Link to comment https://forums.phpfreaks.com/topic/206348-errors-when-updating-data-in-mysql/ Share on other sites More sharing options...
djfox Posted July 1, 2010 Share Posted July 1, 2010 I don`t see anywhere in your code that $displayname $campname and $id are picked up on for the system. You would need to add in $displayname = $_GET['displayname']; $campname = $_GET['campname']; $id = $_GET['id']; If you`re using post method for the form on the previous page, then replace GET with POST. Without specifying those before you tell the system to add them in, the system won`t know what it`s supposed to grab. Link to comment https://forums.phpfreaks.com/topic/206348-errors-when-updating-data-in-mysql/#findComment-1079422 Share on other sites More sharing options...
Seaholme Posted July 1, 2010 Author Share Posted July 1, 2010 Thanks! I changed it as you suggested, but it still throws up an error. This is the edited version... <?php $dbhost = 'xxx'; $dbuser = 'xxx'; $dbpass = 'xxx'; $dbname = 'xxxx'; $tbl_name="players"; // Table name // Connect to server and select database. mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); $displayname = $_POST['displayname']; $campname = $_POST['campname']; $id = $_POST['id']; // update data in mysql database $sql="UPDATE players SET displayname = '$displayname', campname = '$campname', WHERE id='$id'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; } else { echo "ERROR"; } ?> EDIT: I also thought I'd add in the php for the previous page, in case that helps <?php $dbhost = 'xxx'; $dbuser = 'xxxx'; $dbpass = 'xxx'; $dbname = 'xxx'; $tbl_name="players"; // Table name // Connect to server and select database. mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect"); mysql_select_db("$dbname")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Username</strong></td> <td align="center"><strong>Camp Name</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="displayname" type="text" id="displayname" value="<? echo $rows['displayname']; ?>"></td> <td align="center"><input name="campname" type="text" id="campname" value="<? echo $rows['campname']; ?>" size="15"></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> <? // close connection mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/206348-errors-when-updating-data-in-mysql/#findComment-1079426 Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2010 Share Posted July 1, 2010 EDIT: You were posting the form as I was typing this out, apparently . . . Need to see more of the code, and the form to get a better idea of what's going on. Also, according to the website, the `id` field in the database table is of the integer type, so you should remove the single quotes around the variable in the WHERE clause: WHERE id='$id'", and the variable $id should be cast as an integer as well. Link to comment https://forums.phpfreaks.com/topic/206348-errors-when-updating-data-in-mysql/#findComment-1079427 Share on other sites More sharing options...
djfox Posted July 1, 2010 Share Posted July 1, 2010 Ok, this line: $sql="UPDATE players SET displayname = '$displayname', campname = '$campname', WHERE id='$id'"; Change it to: $sql="UPDATE players SET displayname = '$displayname', campname = '$campname' WHERE id='$id'"; You have an extra comma that shouldn`t be there. Link to comment https://forums.phpfreaks.com/topic/206348-errors-when-updating-data-in-mysql/#findComment-1079428 Share on other sites More sharing options...
Seaholme Posted July 1, 2010 Author Share Posted July 1, 2010 You guys are absolute lifesavers! It's working now Thanks so much! Link to comment https://forums.phpfreaks.com/topic/206348-errors-when-updating-data-in-mysql/#findComment-1079432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.