Jump to content

broken sql and php


shalendar

Recommended Posts

I'm building a store with php and sql. the user can shop around, add stuff to their cart and then buy it. when they buy it, the site creates an invoice. the code is supposed to change the column `invoice` in the cart table to the id number of the invoice on all the item in that invoice. and change the `status` to '2'.for some reason it changes `invoice` and `status` to '0'.heres the code:

		$timestamp = date('Y.m.d.H.i');\\ get the user id	session_start();	$connection = mysql_connect("server", "user", "password") or die(mysql_error());	$db = mysql_select_db(database,$connection)or die(mysql_error());	$sql ="SELECT * FROM mkp_users WHERE email = '$_SESSION[email]' AND pass = '$_SESSION[pass]'";	$result = mysql_query($sql,$connection) or die(mysql_error());	$row = mysql_fetch_array($result);	$userid = $row['userid'];\\ enter the invoice into the db	$connection = mysql_connect("server", "user", "password") or die(mysql_error());	$db = mysql_select_db(database,$connection)or die(mysql_error());	$sql = "INSERT INTO mkp_invoices		VALUES ('', '$userid', '$timestamp', '$total', '1')";	$result = mysql_query($sql,$connection) or die(mysql_error());\\ retrieve the automated id of the invoice	$connection = mysql_connect("server", "user", "password") or die(mysql_error());	$db = mysql_select_db(database,$connection)or die(mysql_error());	$sql ="SELECT * FROM mkp_invoices WHERE `timestamp` = '$timestamp' LIMIT 1";	$result = mysql_query($sql,$connection) or die(mysql_error());	$row = mysql_fetch_array($result);	$id = $row['id'];\\ change the status and the invoice id of the item in the invoice	$connection = mysql_connect("server", "user", "password") or die(mysql_error());	$db = mysql_select_db(database,$connection)or die(mysql_error());	$sql = "UPDATE mkp_cart SET `status` = '1' AND `invoice` = '$id' WHERE `userid` = '$userid' AND `status` = '0'";	$result = mysql_query($sql,$connection) or die(mysql_error());

Link to comment
Share on other sites

session_start(); should be at the most top of you code

\\ enter the invoice into the db	$connection = mysql_connect("server", "user", "password") or die(mysql_error());	$db = mysql_select_db(database,$connection)or die(mysql_error());	$sql = "INSERT INTO mkp_invoices		VALUES ('', '$userid', '$timestamp', '$total', '1')";	$result = mysql_query($sql,$connection) or die(mysql_error());

aren't you suppose to tell where to insert the VALUES? some thing like
\\ enter the invoice into the db	$connection = mysql_connect("server", "user", "password") or die(mysql_error());	$db = mysql_select_db(database,$connection)or die(mysql_error());	$sql = "INSERT INTO mkp_invoices (username, user_id, full_date, total, flag)		VALUES ('', '$userid', '$timestamp', '$total', '1')";	$result = mysql_query($sql,$connection) or die(mysql_error());

Link to comment
Share on other sites

you only have to list the columns if youre not using every single column. I just put a '' in the space for the column i'm not using.

Link to comment
Share on other sites

Is this the query that is setting the fields to 0?$sql = "UPDATE mkp_cart SET `status` = '1' AND `invoice` = '$id' WHERE `userid` = '$userid' AND `status` = '0'";You might want to try printing that query out and run it on the database directly, with something like phpMyAdmin.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...