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

3 Answers3

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');
Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 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 Nov 02 '14 at 10:35
  • @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());` – Kevin Nov 02 '14 at 11:54
  • 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 Nov 02 '14 at 12:12
  • @mihaicata1205 try to echo `$insert` and use it on phpmyadmin to find out – Kevin Nov 02 '14 at 12:17
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.

M0N0NE
  • 63
  • 1
  • 2
  • 8
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/

Cups
  • 6,901
  • 3
  • 26
  • 30