1

I need to insert data from a table named wishlist into another table (wishlisturi_salvate) and altough the insert looks ok, something doesn't work right and no inseration is being made.Thanks for the help, I really appreciate it.

<?php
session_start();
include ('conex.php');
$sel2="select id_wishlist  from wishlisturi_salvate";
$que2=mysql_query($sel2);
while($rez2=mysql_fetch_array($que2))
{
$a=$rez2['id_wishlist'];
}
$id_wishlist=$a;
echo $id_wishlist;
$sel="SELECT * from wishlist";
$que=mysql_query($sel);
while ($rez=mysql_fetch_array($que))
{
$insert="INSERT INTO 'wishlisturi_salvate'('id_user', 'id_wishlist', 'id_produs',        'nume_produs', 'pret_produs', 'cantitate_produs', 'suma') 
   VALUES('".$_SESSION['id']."','".$id_wishlist."','".$rez['id_produs']."','".$rez['nume_produs']."','".$rez['pret_produs']."','".$rez['cantitate_produs']."','".$rez['suma']."')";
if(!mysql_query($insert)) echo "fml";
echo "<br>".$insert;
}

if(mysql_query($insert))
{
header("location:user.php");
}
else echo "Nu s-a facut inserarea"; 
?>
1
  • echo mysql_error() is your friend. Commented Nov 2, 2014 at 10:27

3 Answers 3

1

No insertion is being made most likely because of the errors inside the query:

Right of the bat, there is already an error:

INSERT INTO 'wishlisturi_salvate'('id_user', 'id_wishlist', 'id_produs',        'nume_produs', 'pret_produs', 'cantitate_produs', 'suma') 

The proper quoting of table/column names must be backtickts, not single quotes

INSERT INTO `wishlisturi_salvate` (`id_user`, `id_wishlist`, `id_produs`, `nume_produs`, `pret_produs`, `cantitate_produs`, `suma`)

Or just omit them, its okay in this case.

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Sidenote:

If you haven't already, always turn on error reporting:

error_reporting(E_ALL);
ini_set('display_errors', '1');
Sign up to request clarification or add additional context in comments.

4 Comments

yeah, i've made a mistake, but i tried without the single quotes and with the backtickts but it still isn't working, so that's not the major problem.Can you figure it out? I really need to get this thing going, because this is a project and i'm running out of time...
@mihaicata1205 just do a step by step debugging, start off with the first query, check if the id is properly echoed, next build the sql statement for the next query, echo that again, and you can use an mysql_query(..) or die(mysql_error());
yeah, i've tried but i can't figure out the problem.Is there anything wrong with the insert?(except for the single quotes)
@mihaicata1205 try to echo $insert and use it on phpmyadmin to find out
0

First of all I'll rcomend you to use PDO or mysqli instead of mysql to avoid SQL injection.

Anyway, if you want to insert elements from one table to another one I recommend you to use an insert statment with a subselect. That way it'll be faster and you will waste less memory.

Comments

0

Not an answer, more of an observation ; It will be far more efficient to loop through your results to build up a single SQL insert multiple statement that you send to the db once.

$insert = "INSERT INTO 'wishlisturi_salvate'('id_user', 'id_wishlist', 'id_produs',        'nume_produs', 'pret_produs', 'cantitate_produs', 'suma') VALUES ";

foreach( of your results ){
$insert .= "(x,y,z,a,b,c,d),";
}

// now trim off last comma, then send to db.
// or create an array then join it to the $insert

Same info can be read here : http://www.electrictoolbox.com/mysql-insert-multiple-records/

2 Comments

Ah yes, subselect as user1514329 says. Do the work in your db.
that's right, thanks for the advice but first i need to get this thing going, than i can optimize it.

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.