Jump to content

  mysqli INSERT INTO from tabel not working


Go to solution Solved by Barand,

Recommended Posts

Hi there,

 

I'm trying to move a mysql item from one to another db tabel. It worked but after I worked further it stopt working...

The delete part works.

(Its my first php project)

Did I make some mistakes in the code below?

if ($_POST['mode'] === 'GO') {
  
    mysqli_query($conn, "INSERT INTO a SELECT * FROM b WHERE id='" . $_POST["id"] . "'");
    mysqli_query($conn, "DELETE FROM b WHERE id='" . $_POST["id"] . "'");   
   echo json_encode(true);

}  

 

Edited by RRO

Try this to see if there are some issues:

$q = "INSERT INTO a SELECT * FROM b WHERE id='" . $_POST["id"] . "'";
if (mysqli_query($conn, $q))
	echo "Insert query successful";
else
{
	echo "Insert query failed.  Query is<br>$q<br>";
	exit();
}

$q = "DELETE FROM b WHERE id='" . $_POST["id"] . "'";
if (mysqli_query($conn, $q))
	echo "Delete query successful";
else
{
	echo "Delete query failed.  Query is<br>$q<br>";
	exit();
}
echo json_encode(true);  // Not sure what this is.

If you get a failure try adding the mysqli error reporting function to it as already suggested.  This will give you at least some indication.

Suggestion - IMHO it is good practice to NOT bury a query statement inside of another function as you are doing.  By assigning it to a variable it makes it easy to echo out your query string should you want to analyze it during any debugging you may need to do as in this case perhaps.

 

Did a quick (Really quick!) lookup and found this.  You have the wrong syntax so apparently you did not do your homework.

INSERT INTO destination_table_name(column_1, column_2) 
	SELECT column_1,column_2 
		FROM source_table 
		WHERE condition;

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.