bri0987 Posted October 25, 2007 Share Posted October 25, 2007 My code is not working. But it DOES NOT return any errors... It's weird. The pages loads fine and I checked all of the varibles by echoing them into the page for me to see and make sure they were good... but when I check to see if the data is in the database (mysql), nothing is there... Need help with this. <?php if (isset($_POST['AddToCart'])) { $qty = "1"; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); mysql_query("INSERT INTO tblshoppingcart (Cookie_ID, Product_ID, Component_ID, qty) VALUES (" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . intval($qty) . ")"); } //header("Location: cart.php"); } else { $ihateyou = "I HATE YOU"; } ?> Anyone see anything thats maybe wrong... and could cause this. FYI: GetCartId() just returns the session ID ... which currently for me when echo is: f765354f720f0d73ac26194fe47fb016 Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/ Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 first things first.. lets add some error capturing.. <?php if (isset($_POST['AddToCart'])) { $qty = "1"; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); $query = "INSERT INTO tblshoppingcart (Cookie_ID, Product_ID, Component_ID, qty) VALUES (" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . intval($qty) . ")"; mysql_query($query) or die("query='$query '<br>".mysql_error()); } //header("Location: cart.php"); } else { $ihateyou = "I HATE YOU"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377473 Share on other sites More sharing options...
Michan Posted October 25, 2007 Share Posted October 25, 2007 Try: mysql_query('INSERT INTO tblshoppingcart (Cookie_ID, Product_ID, Component_ID, qty) VALUES (" '. GetCartId() .' ", " '. intval($ProductID) .' ", " '. intval($component) .' ", " '. intval($qty) .' ")'); Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377475 Share on other sites More sharing options...
unidox Posted October 25, 2007 Share Posted October 25, 2007 Here u go: <?php if (isset($_POST['AddToCart'])) { $qty = "1"; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); mysql_query("INSERT INTO `tblshoppingcart` (Cookie_ID, Product_ID, Component_ID, qty) VALUES (" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . intval($qty) . ")") or die (mysql_error()); } //header("Location: cart.php"); } else { $ihateyou = "I HATE YOU"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377489 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 Okay cool thanks... I put in the error code... Here is the error I got: Unknown column 'f765354f720f0d73ac26194fe47fb016' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377496 Share on other sites More sharing options...
unidox Posted October 25, 2007 Share Posted October 25, 2007 Please post your code again. Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377500 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 <?php if (isset($_POST['AddToCart'])) { $qty = 1; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); mysql_query("INSERT INTO tblshoppingcart (Cookie_ID, Product_ID, Component_ID, qty) VALUES (" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . $qty . ")") or die (mysql_error()); } //header("Location: cart.php"); $ihateyou = $_POST['AddToCart']; } else { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377504 Share on other sites More sharing options...
StormTheGates Posted October 25, 2007 Share Posted October 25, 2007 Try using `s For example: `Cookie_ID`, `Product_ID`, `Component_ID`, `qty` Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377507 Share on other sites More sharing options...
unidox Posted October 25, 2007 Share Posted October 25, 2007 Use this: <? if (isset($_POST['AddToCart'])) { $qty = 1; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); mysql_query("INSERT INTO `tblshoppingcart` (Cookie_ID, Product_ID, Component_ID, qty) VALUES ('" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . $qty . ")") or die (mysql_error()); } //header("Location: cart.php"); $ihateyou = $_POST['AddToCart']; } else { } ?> If that doesnt work, its something to do with the info being sent. Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377510 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 I changed the code to this: <?php if (isset($_POST['AddToCart'])) { $qty = 1; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); $insertquery = "INSERT INTO `tblshoppingcart` (Cookie_ID, Product_ID, Component_ID, qty) VALUES (" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . $qty . ")"; mysql_query($insertquery) or die ("query= `$insertquery` <br />" . mysql_error()); } //header("Location: cart.php"); $ihateyou = $_POST['AddToCart']; } else { } ?> Here is the query and error message I got. query= `INSERT INTO `tblshoppingcart` (Cookie_ID, Product_ID, Component_ID, qty) VALUES (f765354f720f0d73ac26194fe47fb016, 1, 13, 1)` Unknown column 'f765354f720f0d73ac26194fe47fb016' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377519 Share on other sites More sharing options...
teng84 Posted October 25, 2007 Share Posted October 25, 2007 Okay cool thanks... I put in the error code... Here is the error I got: Unknown column 'f765354f720f0d73ac26194fe47fb016' in 'field list' i know you will get that error error means your inserting int into character field or vise versa @unidox missed ' '" . GetCartId() . "' Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377520 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 teng84 <<< what do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377523 Share on other sites More sharing options...
teng84 Posted October 25, 2007 Share Posted October 25, 2007 tell us the data types of this fields Cookie_ID, Product_ID, Component_ID, qty Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377524 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 Okay.. try this update.. is it possible to the error.. the part thats starts query='INSERT INTO ...etc..' as the field are all static its kinda a strange message.. <?php if (isset($_POST['AddToCart'])) { $qty = "1"; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); $query = "INSERT INTO tblshoppingcart (`Cookie_ID`, `Product_ID`, `Component_ID`, `qty`) VALUES ('" . GetCartId() . "', " . intval($ProductID) . ", " . intval($component) . ", " . intval($qty) . ")"; mysql_query($query) or die("query='$query '<br>".mysql_error()); } //header("Location: cart.php"); } else { $ihateyou = "I HATE YOU"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377526 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 ShoppingCart_ID int(11) Cookie_ID varchar(50) Product_ID int(11) Component_ID int(11) qty int(11) Cart_Date timestamp Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377527 Share on other sites More sharing options...
StormTheGates Posted October 25, 2007 Share Posted October 25, 2007 Try this <?php if (isset($_POST['AddToCart'])) { $qty = 1; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); mysql_query("INSERT INTO tblshoppingcart (`Cookie_ID`, `Product_ID`, `Component_ID`, `qty`) VALUES (" . GetCartId() . ", " . intval($ProductID) . ", " . intval($component) . ", " . $qty . ")") or die (mysql_error()); } //header("Location: cart.php"); $ihateyou = $_POST['AddToCart']; } else { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377528 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 MadTechie << I'm confused on what you want me to do Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377531 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 you should of got a error message like this query='INSERT INTO blar blar ' Unknown column 'f765354f720f0d73ac26194fe47fb016' in 'field list' can you post the blar blar part also did you try the last code set i posted.. i know with every man, woman & dog posting it can get confusing.. Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377534 Share on other sites More sharing options...
teng84 Posted October 25, 2007 Share Posted October 25, 2007 that last code given by madtechi should work Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377535 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 should but Unknown column 'f765354f720f0d73ac26194fe47fb016' in 'field list'... value list i'll expect but field! Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377537 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 MadTechie <<<<<<<<<<< Your code worked it updated the MySQL database... Here is your code... <?php if (isset($_POST['AddToCart'])) { $qty = "1"; foreach ($_POST["Component_ID"] as $type => $component) { // mysql_select_db($database_DM_database, $DM_database) or die(mysql_error()); $query = "INSERT INTO tblshoppingcart (`Cookie_ID`, `Product_ID`, `Component_ID`, `qty`) VALUES ('" . GetCartId() . "', " . intval($ProductID) . ", " . intval($component) . ", " . intval($qty) . ")"; mysql_query($query) or die("query='$query '<br>".mysql_error()); } //header("Location: cart.php"); } else { $ihateyou = "I HATE YOU"; } ?> What did you do or change to make it work????? Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377538 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 New problem... My time stamp is set to update on create ... but its not updating ??? Cart_Date f765354f720f0d73ac26194fe47fb016 1 13 1 0000-00-00 00:00:00 f765354f720f0d73ac26194fe47fb016 1 15 1 0000-00-00 00:00:00 f765354f720f0d73ac26194fe47fb016 1 16 1 0000-00-00 00:00:00 f765354f720f0d73ac26194fe47fb016 1 13 1 0000-00-00 00:00:00 f765354f720f0d73ac26194fe47fb016 1 15 1 0000-00-00 00:00:00 f765354f720f0d73ac26194fe47fb016 1 16 1 0000-00-00 00:00:00 Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377540 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 used backtick as i was confused why you was getting a Field error.. but the key was VALUES ('" . GetCartId() . "', " GetCartId() is a string thus must be quoted the others are int's so no quotes were needed TimeStamp fix option #1 (via PHP code) $query = "INSERT INTO tblshoppingcart (`Cookie_ID`, `Product_ID`, `Component_ID`, `qty`, `Cart_Date`) VALUES ('" . GetCartId() . "', " . intval($ProductID) . ", " . intval($component) . ", " . intval($qty) . ", NOW())"; EDIT: option #2 (auto via MySQL) from phpMyAdmin run SQL ALTER TABLE `tblshoppingcart` CHANGE `Cart_Date` `Cart_Date` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377542 Share on other sites More sharing options...
bri0987 Posted October 25, 2007 Author Share Posted October 25, 2007 okay okay ... I see now... Wild... I have been trying to figure this out for a while. I was starting to think I was going PHP dumb! hahahaha ... I even pulled out the beginner tutorials to see if I forgot something... Man thanks... I also go the time_stamp working Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377545 Share on other sites More sharing options...
MadTechie Posted October 25, 2007 Share Posted October 25, 2007 please note the 2 options above just takes pratice.. i have spent 2 days trying to work out a problem (was a hard one) but the solution was so simple when i thought about it later Quote Link to comment https://forums.phpfreaks.com/topic/74671-solved-insert-in-mysql-problem-my-code-is-not-working/#findComment-377546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.