Jump to content

MySQL Insert and Update


timmah1

Recommended Posts

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

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.