Solved

MySQL insert not working in PHP Code ?!

Posted on 2007-04-04
7
285 Views
Last Modified: 2013-12-12
Gah!  I can't for the life of me figure out why this isn't working.  From within a while statement, I'm trying to insert records into a db.  

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

  $ins = "INSERT INTO temp (product_id, quantity, sessionid, price, shipping, description) VALUES ('".$row['product_id']."', '".$_SESSION['cart'][$row['product_id']]."', '".$sessnum."', '".$row['product_price']."', '".$row['shipping_cost']."', '".$row['product_desc']."')";

  $res = mysql_query($ins);

....
}

If I echo $ins, I get a good query:

INSERT INTO temp (product_id, quantity, sessionid, price, shipping, description) VALUES ('1345', '1', 'gt4qe3cv1vpkz9akt9trnnlat7', '129', '9', 'TSHIRT - Blue/Green - SMALL')

In fact, if I copy and paste this query at the mysql prompt, it works fine.  But it doesn't work when the script runs.  Nothing is inserted into my table.  I did:

if($res) {
  echo 'fine';
}

And it printed 'fine' to the screen. I don't understand what's happening.  I checked all of the syntax, etc., but it's not working.  Help!

0
Comment
Question by:n00b0101
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18852998
let me assume that some fields, like product_id, quantity, price etc are numerical fields, in which case you should NOT use the single quotes around the value...
0
 

Author Comment

by:n00b0101
ID: 18853023
No, even if I remove them, the information isn't inserted
0
 
LVL 24

Accepted Solution

by:
glcummins earned 500 total points
ID: 18853033
See if you can find out what the MySQL error is:

  $res = mysql_query($ins) or die("Failed while executing the following query: $ins<br />The MySQL error was: " . mysql_error());
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18853035
please check if you don't get any error printed...
if($res) {
  print mysql_error();
  echo 'fine';
}
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18853040
if that still doesn't give any error, then you are not looking at the same database...
0
 
LVL 10

Expert Comment

by:Chris_Gralike
ID: 18853236
Thats prop because the sql connection is allready being used for the resultset from wich you are getting the result set. An method of doing this (if you are using a class) is to setup a second connection to your database.

Not sure how the MYSQL_ASSOC is handled, but try to set up a second connection and use that to do the insert.

if using classes;

$sql['1'] = new c_mysql;
$sql['2'] = new c_mysql;

$sql = 'blablabla';
$result = $sql['1']->query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
       $sql2 = 'insert blablablabla';
       $sql['2']->query($sql2);
}

Or something like that...

-Regards,
 
0
 
LVL 17

Expert Comment

by:psimation
ID: 18853361
One other thing to check;

If you do a default install of MySQL, the default permissions on all new DB's and tables are READ ONLY.
If you have something like phpmyadmin installed, you can easily fix this by just going to the mysql DB and looking in the "user" and  "db" tables and put a "Y" in the "insert_priv" collumns for the particular table and user, then restart mysql.


0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP & MySql - Turn Select Query Results into a Glyphicon 3 31
how can i gnupg encrypt a PDF document? 12 33
sort in mysql based off of query param 4 24
comma true 6 33
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question