Jump to content

Why won't this query work?


Canadian

Recommended Posts

I'm trying out the code below.  I'm able to connect to the database.  To make sure I have entered my values into my database I have set up a test to look for errors.  I assign the query to $results and then have PHP tell me whether or not it worked.  Unfortunately, I keep getting the message...

 

"An error has occurred.  The item was not added."

 

Thanks for the help.

 

 

------------Code-------------

 

<?php

 

 

 

$npn_email=$_POST['npn_email'];

$purchase_email=$_POST['purchase_email'];

$zip=$_POST['zip'];

 

 

 

 

date_default_timezone_set('Canada/Atlantic');

$current_date='';

$current_date = date('Y-m-d H:i:s');

 

 

if (!$npn_email) {

 

 

if (!get_magic_quotes_gpc()) {

    $purchase_email = addslashes($purchase_email);

    $zip = addslashes($zip);

$current_date = addslashes($current_date);

}

 

 

@ $db = new mysqli('XXXXXXXX', 'XXXXXXXX', 'XXXXXXXX', 'XXXXXXXX');

 

 

 

  if (mysqli_connect_errno()) {

    echo "Error: Could not connect to database.  Please try again later.";

    exit;

  }

 

 

 

$query = "insert into orders (email, zip, current_date) values ('$purchase_email', '$zip', '$current_date')";

 

 

$result = $db->query($query);

 

 

 

 

  if ($result) {

      echo  "Thank you for your purchase!  You have been entered to win!";

  } else {

    echo "An error has occurred.  The item was not added.";

  }

 

 

 

 

 

$db->close();

 

 

}

 

else {

echo"You hit the NPN button";

}

 

 

?>

Link to comment
Share on other sites

I'm trying out the code below.  I'm able to connect to the database.  To make sure I have entered my values into my database I have set up a test to look for errors.  I assign the query to $results and then have PHP tell me whether or not it worked.  Unfortunately, I keep getting the message...

 

"An error has occurred.  The item was not added."

 

Thanks for the help.

 

 

------------Code-------------

 

<?php

 

 

 

$npn_email=$_POST['npn_email'];

$purchase_email=$_POST['purchase_email'];

$zip=$_POST['zip'];

 

 

 

 

date_default_timezone_set('Canada/Atlantic');

$current_date='';

$current_date = date('Y-m-d H:i:s');

 

 

if (!$npn_email) {

 

 

if (!get_magic_quotes_gpc()) {

    $purchase_email = addslashes($purchase_email);

    $zip = addslashes($zip);

$current_date = addslashes($current_date);

}

 

 

@ $db = new mysqli('XXXXXXXX', 'XXXXXXXX', 'XXXXXXXX', 'XXXXXXXX');

 

 

 

  if (mysqli_connect_errno()) {

    echo "Error: Could not connect to database.  Please try again later.";

    exit;

  }

 

 

 

$query = "insert into orders (email, zip, current_date) values ('$purchase_email', '$zip', '$current_date')";

 

 

$result = $db->query($query);

 

 

 

 

  if ($result) {

      echo  "Thank you for your purchase!  You have been entered to win!";

  } else {

    echo "An error has occurred.  The item was not added.";

  }

 

 

 

 

 

$db->close();

 

 

}

 

else {

echo"You hit the NPN button";

}

 

 

?>

 

dumping code is not good idea.

show what you are getting in error...

Link to comment
Share on other sites

Please use the

 

[code=php:0]   .... 

[/code]

 

BBcode blocks around your code in the future.

 

Try adding this code:

 

f ($result) {
               echo  "Thank you for your purchase!  You have been entered to win!";
           } else {
                echo "An error has occurred.  The item was not added.";
                echo $db->error;
           }

Link to comment
Share on other sites

This is the error.  Any idea what I would change to fix this?

 

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 'current_date) values ('chris@paintingwithpurpose.com', '89144', '2010-02-07 19:5' at line 1

 

And I'm sorry about the dumping code.  I didn't get an error so I didn't know what to do. 

 

Sorry about the BBcode  block thing too.  I'll correct that for next time

Link to comment
Share on other sites

Alright.  I fixed the syntax.  Here is the new error I'm getting. 

 

Column count doesn't match value count at row 1 

 

Here is how my table is set up...

 

Column 1:  name = order_id (auto increment), type = int

Column 2:  name = email, type = varchar

Column 3:  name = zip, type = char

Column 4:  name = time, type = timestamp

 

I'm clearly no pro but it seems like my query is missing a value for the order_id column. 

 

Here is the corrected query I am using...

 

$query = "insert into orders values ('".$purchase_email."', '".$zip."', '".$current_date."')";

Link to comment
Share on other sites

You have 4 columns and 3 inputs,

 

So you can either do this

$query = "insert into orders set (`email`, `zip`, `time`) values ('".$purchase_email."', '".$zip."', '".$current_date."')";

or

$query = "insert into orders values (null,'".$purchase_email."', '".$zip."', '".$current_date."')";

Link to comment
Share on other sites

You have 4 columns and 3 inputs,

 

So you can either do this

$query = "insert into orders set (`email`, `zip`, `time`) values ('".$purchase_email."', '".$zip."', '".$current_date."')";

or

$query = "insert into orders values (null,'".$purchase_email."', '".$zip."', '".$current_date."')";

 

 

Thanks!  That worked perfect!

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.