Jump to content

MySQL Inserting Two Rows


btoles

Recommended Posts

	if($_GET['action'] == "move")
{
	$id = $_GET['item_id'];

	$inv = mysql_fetch_object(mysql_query("SELECT * FROM `inventory` WHERE `item_id` = '$id' LIMIT 1"));

	mysql_query("INSERT INTO `storage` (`item_id`, `data_id`, `user_id`) VALUES ('$id', '$inv->data_id', '$thisuser->id')") or die(mysql_error());

	mysql_query("DELETE FROM `inventory` WHERE `item_id` = '$id'") or die(mysql_error());

	echo "<center>Your item has been moved to storage</center>";
}

 

 

The line is the INSERT, it inserts two rows everytime I use the query, they are exactly identical except one has no data_id.  I only want it to insert ONE ROW!!!!  It's doing this to me with another table as well (inventory table)

 

Link to comment
https://forums.phpfreaks.com/topic/166882-mysql-inserting-two-rows/
Share on other sites

Your code is being executed twice. This is usually because your browser requested the page twice but it can also be cause by url rewriting or if you are including code twice or doing a redirect that causes the code to be executed twice.

 

The best way to prevent this (assuming it not being caused by a logic error in your code) is to put some validation logic in your code. If the SELECT query fails and does not return anything for $inv->data_id, don't execute the INSERT and DELETE queries.

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.