timmah1 Posted May 6, 2007 Share Posted May 6, 2007 Is it possible to UPDATE a database entry and INSERT a database entry at the same time? For example: I need to update one row of a database, but I need to insert a row also $query="update friends set approved = 'y' where ID = '$id'"; $result2 = MYSQL_QUERY($query); $insert = mysql_query("insert into $table values ('NULL','$friendID','$driverID','y')") or die("Could not insert data into users because ".mysql_error()); This code does UPDATE and it does INSERT, but it INSERTS the row 3 times, I only need it to INSERT once. I'm not sure if this is possible. Link to comment https://forums.phpfreaks.com/topic/50225-mysql-insert-and-update/ Share on other sites More sharing options...
Barand Posted May 6, 2007 Share Posted May 6, 2007 It's certainly possible. Is the code inside a loop? Also don't put quotes around the NULL value. Link to comment https://forums.phpfreaks.com/topic/50225-mysql-insert-and-update/#findComment-246550 Share on other sites More sharing options...
timmah1 Posted May 6, 2007 Author Share Posted May 6, 2007 Taking the quotes off of NULL worked. Now it only adds 2 instead 1. This is the entire code: if ( $_GET["cid"] == "approve" ){ include 'dbConfig.php'; $server = "localhost"; // server to connect to. $database = "xxx"; // the name of the database. $db_user = "xxx"; // mysql username to access the database with. $db_pass = "xxx"; // mysql password to access the database with. $table = "friends"; // the table that this script will set up and use. // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); $query="update friends set approved = 'y' where ID = '$id'"; $result2 = MYSQL_QUERY($query); $insert = mysql_query("insert into $table values (NULL,'$friendID','$driverID','y')") or die("Could not insert data into users because ".mysql_error()); echo "Friend Accepted"; } Link to comment https://forums.phpfreaks.com/topic/50225-mysql-insert-and-update/#findComment-246553 Share on other sites More sharing options...
Barand Posted May 6, 2007 Share Posted May 6, 2007 Nothing I can see there should give multiple records, unless the code was called 3 times Link to comment https://forums.phpfreaks.com/topic/50225-mysql-insert-and-update/#findComment-246573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.