patheticsam Posted December 31, 2008 Share Posted December 31, 2008 Hi! I'm having problems updating mySQL database with an HTML POST form I'm new in PHP so I can't find out what is the problem...Thanks for you help!! The form part is working and the data fetch in the form correctly from mySQL database Here's the HTML update form: <?php $id=$_POST["modif"]; $db = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('database',$db); $sql = "SELECT `id`, `firstname`, `lastname` FROM table WHERE id='$id'"; $req = mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br>'.mysql_error()); while($data = mysql_fetch_assoc($req)) { echo " <center> Modify <table border=0 style=\"border: 1px solid #A7883F\" width=85% cellpadding=0 cellspacing=0> <form action=modify.php method=post> <tr> <td colspan=4><center><u>Informations</u></center><br></td> </tr> <tr> <td colspan=4> ID :<input type=text value=\"$data[id]\"> </td> </tr> <tr> <td align=right width=25%>First name :</td> <td width=25%><input name=firstname value=\"$data[firstname]\" type=text size=20 MAXLENGTH=40></td> <td align=right width=25%>Lastname :</td> <td width=25%><input name=lastname value=\"$data[lastname]\" type=text size=20 MAXLENGTH=40></td> </tr> <tr> <td colspan=4> <center><br><input type=submit value=Save></center> </td> </tr> </center> </form> </table> "; } mysql_close(); ?> Here the part that isn't working... modify.php : <?php $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); mysql_query(" UPDATE table SET firstname = ('$_POST[firstname]'), lastname = ('$_POST[lastname]') WHERE id = ('$_POST[id]')"); echo "Update Sucessful"; mysql_close($con); ?> If someone could tell me what i'm doing wrong it woulb be really appreciated! Thanks!! (edited to change the tags to tags) Quote Link to comment https://forums.phpfreaks.com/topic/138999-solved-problem-with-post-form-to-update-mysql-database/ Share on other sites More sharing options...
premiso Posted December 31, 2008 Share Posted December 31, 2008 mysql_close is not needed. Just an fyi. Your issue: mysql_query(" UPDATE table SET firstname = '{$_POST['firstname']}', lastname = '{$_POST['lastname']}' WHERE id = '{$_POST['id']}'"); With SQL Injection issues I would highly advise escaping the post data before inserting into a database. But that should solve your problem. If you would have done this: mysql_query(" UPDATE table SET firstname = '{$_POST['firstname']}', lastname = '{$_POST['lastname']}' WHERE id = '{$_POST['id']}'") or die(mysql_error()); The error would have showed up to you. Quote Link to comment https://forums.phpfreaks.com/topic/138999-solved-problem-with-post-form-to-update-mysql-database/#findComment-726940 Share on other sites More sharing options...
patheticsam Posted December 31, 2008 Author Share Posted December 31, 2008 Ok I inserted that line : '{$_POST['id']}'") or die(mysql_error()); ..and made the modifications...I get no error message but the update is still not working...Thanks again for you help! Quote Link to comment https://forums.phpfreaks.com/topic/138999-solved-problem-with-post-form-to-update-mysql-database/#findComment-726953 Share on other sites More sharing options...
premiso Posted December 31, 2008 Share Posted December 31, 2008 echo $_POST['id'] out and see if that contains a value I doubt it does cause of this: ID :<input type=text value=\"$data[id]\"> Should be ID :<input type=text name=id value=\"$data[id]\"> That should do the update. Quote Link to comment https://forums.phpfreaks.com/topic/138999-solved-problem-with-post-form-to-update-mysql-database/#findComment-726958 Share on other sites More sharing options...
patheticsam Posted December 31, 2008 Author Share Posted December 31, 2008 It worked!! Thank you so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/138999-solved-problem-with-post-form-to-update-mysql-database/#findComment-726969 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.