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
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

Link to comment
Share on other sites

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, ... ]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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