Canadian Posted February 7, 2010 Share Posted February 7, 2010 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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/ Share on other sites More sharing options...
hamza Posted February 7, 2010 Share Posted February 7, 2010 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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/#findComment-1008534 Share on other sites More sharing options...
gizmola Posted February 7, 2010 Share Posted February 7, 2010 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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/#findComment-1008537 Share on other sites More sharing options...
Canadian Posted February 7, 2010 Author Share Posted February 7, 2010 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 ('[email protected]', '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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/#findComment-1008591 Share on other sites More sharing options...
Canadian Posted February 8, 2010 Author Share Posted February 8, 2010 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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/#findComment-1008594 Share on other sites More sharing options...
MadTechie Posted February 8, 2010 Share Posted February 8, 2010 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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/#findComment-1008619 Share on other sites More sharing options...
Canadian Posted February 8, 2010 Author Share Posted February 8, 2010 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 https://forums.phpfreaks.com/topic/191269-why-wont-this-query-work/#findComment-1008634 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.