Jump to content

[SOLVED] INSERT in MySQL problem... My code is not working?


bri0987

Recommended Posts

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

Link to comment
Share on other sites

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";		
}	
?>

 

Link to comment
Share on other sites

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";		
}	
?>

Link to comment
Share on other sites

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

}	
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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' 

Link to comment
Share on other sites

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

 

error means your inserting int into character field or vise versa

@unidox missed '

 

'" . GetCartId() . "'

 

Link to comment
Share on other sites

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";		
}	
?>

 

Link to comment
Share on other sites

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 {

}	
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  

Link to comment
Share on other sites

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... :D

 

I also go the time_stamp working :)

 

 

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.