1

I am confused, I don't know what's wrong. I'm about to transfer all data from my first table to the other. Here is my code:

$getdata = mysql_query("SELECT Quantity, Description, Total FROM ordercart");

while($row = mysql_fetch_row($getdata))
{
foreach($row as $cell){

$query1 = mysql_query("INSERT INTO ordermem (Quantity, Description, Total) VALUES 
($cell)",$connect);

}
mysql_free_result($getdata);
}

I get the error: Warning: mysql_fetch_row(): 5 is not a valid MySQL result resource.

2

4 Answers 4

5

You only pass one value in the INSERT, which expects three values to be passed to the fields Quantity, Description, Total:

INSERT INTO ordermem (Quantity, Description, Total) VALUES 
($cell);

Change it to:

INSERT INTO ordermem (Quantity, Description, Total) VALUES 
($cell, $descriptionParam, $totalParam);

You may also try to use INSERT INTO SELECT directly instead of two distinct statements like so:

INSERT INTO ordermem (Quantity, Description, Total)
SELECT Quantity, Description, Total FROM ordercart;
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to insert 1 value into 3 fields. You need to have 1 value for each field. For example:

$quantity="$_GET['qty']";
$description="$_GET['desc']";
$total="$_GET['total']";

$query = mysql_query("INSERT INTO ordermem (Quantity, Description, Total) 
                                    VALUES ('$quantity','$description','$total'))

Comments

0

Use debugging to find out the source of your problem.

mysql_query() returns a boolean value that tells you whether the operation succeeded or not. If it did not succeed, the mysql_error() function give you mySQL's error message.

Example:

$query1 = mysql_query("INSERT INTO ordermem (Quantity, Description, Total) VALUES ($cell)",$connect);

if (!$query1)  
  trigger_error("mySQL Error: mySQL returned ".mysql_error(), E_USER_ERROR);

This will give you a message something like "Number of values does not match the number of columns", which gives you a hint about what's wrong.

Comments

0

Try this :

$query = "INSERT INTO ordermem (Quantity, Description, Total) SELECT Quantity, Description, Total FROM ordercart";

mysql_query($query);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.