cdoggg94 Posted April 12, 2012 Share Posted April 12, 2012 I have a form right now that Updates a table on my database. I was wondering if I could possibly still have that working but also Insert the same information into another table on the database when the submit button is pressed? Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/ Share on other sites More sharing options...
scootstah Posted April 12, 2012 Share Posted April 12, 2012 Sure, but why would you want duplicate data? Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336758 Share on other sites More sharing options...
cdoggg94 Posted April 12, 2012 Author Share Posted April 12, 2012 Im not inserting ALL of the same data...but I want to take pieces of the form and have it go to one table and update other (and some of the same) elements and put them into another. Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336761 Share on other sites More sharing options...
MMDE Posted April 12, 2012 Share Posted April 12, 2012 Just do two queries, one UPDATE and one INSERT. Also, make sure your database keeps itself somewhat normalized. Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336762 Share on other sites More sharing options...
cdoggg94 Posted April 12, 2012 Author Share Posted April 12, 2012 This was kind of thrown together, but is this on the right track ? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO table_1 (FirstName, LastName, Age) VALUES ('".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[age]."')"); mysql_query("UPDATE Table_2 SET Age=".$_POST[age]." WHERE ID='".$_POST['id']."'"); mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336764 Share on other sites More sharing options...
MMDE Posted April 12, 2012 Share Posted April 12, 2012 This was kind of thrown together, but is this on the right track ? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO table_1 (FirstName, LastName, Age) VALUES ('".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[age]."')"); mysql_query("UPDATE Table_2 SET Age=".$_POST[age]." WHERE ID='".$_POST['id']."'"); mysql_close($con); ?> Yes. You don't really need to close mysql, and you should not use POST data straight into a database query. Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336766 Share on other sites More sharing options...
cdoggg94 Posted April 12, 2012 Author Share Posted April 12, 2012 By not using POST data straight into the query, do you mean to make variables out of the POST data then use those ? Maybe that's a stupid question... Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336770 Share on other sites More sharing options...
MMDE Posted April 12, 2012 Share Posted April 12, 2012 By not using POST data straight into the query, do you mean to make variables out of the POST data then use those ? Maybe that's a stupid question... Not a stupid question, because I didn't explain why you shouldn't do it. It's because you should sanitize the input so people can't do stupid things to your database by giving it data you don't expect. It opens up for hackers to easily take control over your database. Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336771 Share on other sites More sharing options...
scootstah Posted April 12, 2012 Share Posted April 12, 2012 By not using POST data straight into the query, do you mean to make variables out of the POST data then use those ? Yes, and run them through mysql_real_escape_string to prevent SQL injection. Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336775 Share on other sites More sharing options...
cdoggg94 Posted April 12, 2012 Author Share Posted April 12, 2012 I am trying it out right now Thanks for everything you've done so far ! Quote Link to comment https://forums.phpfreaks.com/topic/260818-update-and-insert-at-the-same-time/#findComment-1336786 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.