Jump to content

[SOLVED] INSERT INTO DB help


tqla

Recommended Posts

All of the variables are registered with the session and I see them when I do a test. Knowing that I now wish to use the code below to insert them into the DB. But it doesn't work! When I run the script it appears to work but the DB does not get updated.

 

Can someone take a look at this and tell me what I am doing wrong? Thanks.

 

<?php
session_start();
require_once('db/db.php');

                     
    $sql = "INSERT INTO promocode WHERE code = '$promocode' (clubCard,firstName,lastName,address,additionalAddress,city,state,zip,email,confirmEmail,okToContact,q1,q2yes,q2no,q3,q4,q5,q6,q7,q8,q9) 
        VALUES ('$clubCard','$firstName','$lastName','$address','$additionalAddress','$city','$state','$zip','$email','$confirmEmail','$okToContact,'$q1','$q2yes','$q2no','$q3','$q4','$q5','$q6','$q7','$q8','$q9')";
?>

Thank You for your Time

Link to comment
https://forums.phpfreaks.com/topic/58113-solved-insert-into-db-help/
Share on other sites

You need to execute the query, not just define the query string, AND the conditional WHERE usually belongs at the end of the query string.

 

$sql = "INSERT .....   WHERE condition = ...";
$result = mysql_query($sql) or die(mysql_error()); // execute the query

Don't use where clause in insert statement...

Below is the syntax that can be used.

 

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [iGNORE]

    [iNTO] tbl_name [(col_name,...)]

    VALUES ({expr | DEFAULT},...),(...),...

    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

 

Or:

 

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [iGNORE]

    [iNTO] tbl_name

    SET col_name={expr | DEFAULT}, ...

    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

 

Or:

 

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [iGNORE]

    [iNTO] tbl_name [(col_name,...)]

    SELECT ...

    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Oops. I feel kinda dumb for that one. Okay I changed it to:

 

<?php
session_start();
require_once('db/db.php');

                      
    $sql = "INSERT INTO promocode (clubCard,firstName,lastName,address,additionalAddress,city,state,zip,email,confirmEmail,okToContact,q1,q2yes,q2no,q3,q4,q5,q6,q7,q8,q9) 
VALUES ('$clubCard','$firstName','$lastName','$address','$additionalAddress','$city','$state','$zip','$email','$confirmEmail','$okToContact,'$q1','$q2yes','$q2no','$q3','$q4','$q5','$q6','$q7','$q8','$q9') WHERE code = '$promocode'";

$result = mysql_query($sql) or die(mysql_error()); // execute the query
?>

 

But I get this error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'yes','','','','','','400-600','','','message') WHERE code = 'L12

Hi Andy. That's right!! You just gave me an idea! Originally I was trying to add info to a row that already had a "code" in it. Now I'm just going to create a new table for the final information. Thanks!!

 

Barand. Yes I am using $_SESSION but it's elsewhere in the script. Thanks though!

 

Thanks AndyB, Skali, and Barand.

 

Ticket closed! Yeah!

Archived

This topic is now archived and is closed to further replies.

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