zour1el Posted August 5, 2011 Share Posted August 5, 2011 I have a few HTML and PHP pages that does a circle.... Basically.... Html form to php that inserts into mysql then PHP form that searches the mysql for a record and then outputs via echo I want another text box on this page that will use that record criteria and allow an update to the record.... any help please on what is best case practice before I start this last piece Quote Link to comment https://forums.phpfreaks.com/topic/243978-php-to-mysql-update-help/ Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 You want an update form correct? Usually, I would simply grab all the data from the mysql that I am going to update, get its current values in the SQL table and populate the input fields respectively. Then when the form submits, I update the SQL table based on the changes.. $field1 would from the FORM.. or in your case the text box. UPDATE `table_name` SET `field1` = '$field1' WHERE `field_id`= '$id' Its the simplest way. Quote Link to comment https://forums.phpfreaks.com/topic/243978-php-to-mysql-update-help/#findComment-1252839 Share on other sites More sharing options...
zour1el Posted August 9, 2011 Author Share Posted August 9, 2011 sorry for the delay thanks..... I went back to the code and the database and found something odd...... It was adding a blank record as well.... so i went basic and grabbed a cheesy 101 form and php and created a new DB and it was also adding a blank record... here are the 2 pages <html> <body> <form action="duh.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> and <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("final", $con); $sql="INSERT INTO my_db (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/243978-php-to-mysql-update-help/#findComment-1255089 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 this is most likely because you are not first checking to see if the form has been submitted.. you will want to add the name attribute to the input type submit as well...for this example we will say that the name is name='submit'... <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("final", $con); $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $age = $_POST['age']; $submit = $_POST['submit']; if(isset($submit)){ if(!empty($firstname) && !empty($lastname) && !empty($age)){ $sql="INSERT INTO my_db (FirstName, LastName, Age) VALUES ('$firstname',$lastname',$age')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }else{ echo "1 record added"; } } } mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/243978-php-to-mysql-update-help/#findComment-1255091 Share on other sites More sharing options...
zour1el Posted August 16, 2011 Author Share Posted August 16, 2011 Thank you it worked..... Quote Link to comment https://forums.phpfreaks.com/topic/243978-php-to-mysql-update-help/#findComment-1258170 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.