LOSMAN Posted April 28, 2011 Share Posted April 28, 2011 hello, im fairly new to php nad have been stuck with this problem for a few days now, im getting a "mysql_query(): supplied argument is not a valid MySQL-Link resource" error at line 39 and have no idea what's up with it, any help would be much appreciated! HERE'S MY ERROR INSERT INTO Orders VALUES id = 3 , date = '1303995318', creditcard = '54645656', expirydate= '12/12/2012' PHP Error Message Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/a2944313/public_html/processOrder.php on line 39 Free Web Hosting Error with Creating order Query in process order HERE'S MY CODE: <?php session_start(); include_once 'functions.php'; //This includes some common functions include_once 'connection.php'; // Check if we have established an authenticated if (!isset($_SESSION["authenticatedUser"])) { $_SESSION["Login_Error"] = "Please Login before you process your order"; header("Location: login.php"); } //Otherwise we know all the details. $errorString = ""; //Get the details from the payment page - validation required $total=$_POST["total"]; $creditcard = trim($_POST["creditcard"]); if (empty($creditcard)) $errorString .="The credit card field cannot be blank."; else if (strlen($creditcard) < 6) $errorString .="<br />Credit card field must be at least 6 characters."; $expiry = trim($_POST["expiry"]); if (empty($expiry)) $errorString .="<br />The expiry date cannot be blank."; //Create a timestamp for the order $date = time(); //Check to see if temporary basket is OK if (!empty($errorString)) { header("Location: payment.php?error=$errorString&total=$total"); } else // Else Update the cust_id for the items { //Update the temporary order to a permanent order by associating a customer id $createOrder = "INSERT INTO Orders VALUES ". "id = ".$_SESSION["id"]." , date = '$date', ". " creditcard = '$creditcard', expirydate= '$expiry' " ; print $createOrder; if (!($result = mysql_query ($createOrder, $connection))) die("Error with Creating order Query in process order"); else $OrderId = mysql_insert_id(); //Now add the basket items to the orders foreach ($_SESSION["basket"] as $basketItemArray) { //Calls the getPrice function in functions.php $itemPrice = getPrice($connection, $basketItemArray["ItemId"]); //Create the query to insert the item $addItem = "INSERT INTO Order_items VALUES (".$OrderId.",".$basketItemArray["ItemId"].",".$basketItemArray["quantity"].",".$itemPrice.")"; if (!($result = mysql_query ($addItem, $connection))) die("Error creating item in addItem Query in process order"); } //Relocate to Receipt header("Location: orderReceipt.php?OrderId=$OrderId"); } //end else 'No errors' ?> Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/ Share on other sites More sharing options...
JKG Posted April 28, 2011 Share Posted April 28, 2011 please put your code in code tags. Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207565 Share on other sites More sharing options...
LOSMAN Posted April 28, 2011 Author Share Posted April 28, 2011 hi, thanks for replying, but what are those? sorry im a noob Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207568 Share on other sites More sharing options...
kney Posted April 28, 2011 Share Posted April 28, 2011 he means like this <?php session_start(); include_once 'functions.php'; //This includes some common functions include_once 'connection.php'; // Check if we have established an authenticated if (!isset($_SESSION["authenticatedUser"])) { $_SESSION["Login_Error"] = "Please Login before you process your order"; header("Location: login.php"); } //Otherwise we know all the details. $errorString = ""; //Get the details from the payment page - validation required $total=$_POST["total"]; $creditcard = trim($_POST["creditcard"]); if (empty($creditcard)) $errorString .="The credit card field cannot be blank."; else if (strlen($creditcard) < 6) $errorString .="<br />Credit card field must be at least 6 characters."; $expiry = trim($_POST["expiry"]); if (empty($expiry)) $errorString .="<br />The expiry date cannot be blank."; //Create a timestamp for the order $date = time(); //Check to see if temporary basket is OK if (!empty($errorString)) { header("Location: payment.php?error=$errorString&total=$total"); } else // Else Update the cust_id for the items { //Update the temporary order to a permanent order by associating a customer id $createOrder = "INSERT INTO Orders VALUES ". "id = ".$_SESSION["id"]." , date = '$date', ". " creditcard = '$creditcard', expirydate= '$expiry' " ; print $createOrder; if (!($result = mysql_query ($createOrder, $connection))) die("Error with Creating order Query in process order"); else $OrderId = mysql_insert_id(); //Now add the basket items to the orders foreach ($_SESSION["basket"] as $basketItemArray) { //Calls the getPrice function in functions.php $itemPrice = getPrice($connection, $basketItemArray["ItemId"]); //Create the query to insert the item $addItem = "INSERT INTO Order_items VALUES (".$OrderId.",".$basketItemArray["ItemId"].",".$basketItemArray["quantity"].",".$itemPrice.")"; if (!($result = mysql_query ($addItem, $connection))) die("Error creating item in addItem Query in process order"); } //Relocate to Receipt header("Location: orderReceipt.php?OrderId=$OrderId"); } //end else 'No errors' ?> Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207569 Share on other sites More sharing options...
LOSMAN Posted April 28, 2011 Author Share Posted April 28, 2011 ahhhh i see, i'll edit the post now, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207571 Share on other sites More sharing options...
Muddy_Funster Posted April 28, 2011 Share Posted April 28, 2011 Could you post up your table structure please? On a tangent - you are aware of the data protection and storage regulations regarding the storing of of another entities (either individual or corporate) pesonal and financial information right? Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207577 Share on other sites More sharing options...
LOSMAN Posted April 28, 2011 Author Share Posted April 28, 2011 yeah im aware of the rules etc, but this is just for a university project of mine so peoples info won't be at risk. thanks for replying so quickly! here's my database: DROP TABLE IF EXISTS Customer CASCADE; DROP TABLE IF EXISTS Products CASCADE; DROP TABLE IF EXISTS Category CASCADE; DROP TABLE IF EXISTS Orders CASCADE; DROP TABLE IF EXISTS Order_items CASCADE; -- Create a Database table to represent the "Customer" entity. CREATE TABLE Customer( id int(11) NOT NULL auto_increment, username VARCHAR(30) NOT NULL, firstname VARCHAR(25) NOT NULL, secondname VARCHAR(25) NOT NULL, address1 VARCHAR(40) NOT NULL, address2 VARCHAR(30) NOT NULL, city VARCHAR(25) NOT NULL, postcode VARCHAR( NOT NULL, country VARCHAR(15) NOT NULL, mobilenumber VARCHAR(12) NULL, email VARCHAR(50) NOT NULL, password VARCHAR(12) NOT NULL, privledge VARCHAR(1) NOT NULL DEFAULT 1, PRIMARY KEY (id) ); CREATE TABLE Products ( ItemId MEDIUMINT NOT NULL AUTO_INCREMENT, Name VARCHAR(25) NOT NULL, Description VARCHAR(50) NOT NULL, Price DECIMAL(8,2) NOT NULL, ImageName VARCHAR(25) NOT NULL, CategoryId VARCHAR(12) NOT NULL REFERENCES Category(CategoryId), PRIMARY KEY (itemId) ); CREATE TABLE Category( CategoryId VARCHAR( NOT NULL, CategoryType VARCHAR (32) NOT NULL, PRIMARY KEY (CategoryId) ); CREATE TABLE Order_items ( OrderId int(5) NOT NULL, ItemId int(4) NOT NULL, qty int(3), price decimal(5,2), PRIMARY KEY (OrderId,ItemId) ); CREATE TABLE Orders ( OrderId int(5) NOT NULL auto_increment, id int(5) NOT NULL, date varchar(10), instructions varchar(128), creditcard char(16), expirydate char(5), PRIMARY KEY (OrderId) ); Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207586 Share on other sites More sharing options...
Muddy_Funster Posted April 28, 2011 Share Posted April 28, 2011 Right, the problem is that the orders table has more fields than you are using in your insert. In this case you need to explicitly tell the INSERT where to put the information into the table : INSERT INTO Orders (id, date, creditcard, expirydate) VALUES (...) Actualy, just having a second look at the code there, and you have totaly malformed the INSERT for that ( I looked at the format of the second one the first time ) Your First INSERT is formated as though you are using an UPDATE / SET query. Anyway, change to above and you should get on alright (will probably need to change the second one aswell. Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207598 Share on other sites More sharing options...
PFMaBiSmAd Posted April 28, 2011 Share Posted April 28, 2011 You may in fact have an error in your query statement, but the current error is because $connection isn't a valid database connection. You would need to troubleshoot why your connection.php file isn't creating a database connection in the $connection variable. Free Web Hosting ^^^ You should be learning php, developing php code, and debugging php code on a local development system on your computer. You will waste a TON of time constantly uploading your files to a live server just to see the result of each change you make. You will also have problems and errors when one of your files fails to upload correctly and/or if the web host has set disk-caching/web caching so that your changes don't take effect immediately. Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207603 Share on other sites More sharing options...
LOSMAN Posted April 28, 2011 Author Share Posted April 28, 2011 i tried the insert below but gave me the same error, have i written it wrong? $createOrder = "INSERT INTO Orders (' ',id,date,' ',creditcard, expirydate) VALUES (' ',".$_SESSION["id"].",'$date',' ','$creditcard','$expiry'"; PFMaBiSmAd, yeah your right im wasting loads of time waiting for the server but i have no choice at the moment as my uni server is playing up! should i post my connection.php because my connection.php is able to pass data into the DB on other pages such as register.php etc.??? Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207633 Share on other sites More sharing options...
Muddy_Funster Posted April 28, 2011 Share Posted April 28, 2011 please do Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207636 Share on other sites More sharing options...
LOSMAN Posted April 28, 2011 Author Share Posted April 28, 2011 im not so sure it's my connection.php as it already allows me to pass data into the database with other pages such as registering users and ammending products. any how here's my connection.php P.S. was my insert correct?? i was'nt so sure about it??? <? $host="mysql11.000webhost.com"; // Host name $dbusername="a2944313_1234"; // Mysql username $dbpassword="*****"; // Mysql password $db_name="a2944313_123"; // Database name //Connect to server and select database. mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207638 Share on other sites More sharing options...
PFMaBiSmAd Posted April 28, 2011 Share Posted April 28, 2011 Where in your code are you setting $connection to a valid mysql connection link? Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207667 Share on other sites More sharing options...
LOSMAN Posted April 28, 2011 Author Share Posted April 28, 2011 im not sure what you mean? i thought you only had to include it at the top? Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207670 Share on other sites More sharing options...
Pikachu2000 Posted April 28, 2011 Share Posted April 28, 2011 $connection = mysql_connect( 'host', // . . . etc. Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1207723 Share on other sites More sharing options...
LOSMAN Posted April 29, 2011 Author Share Posted April 29, 2011 please explian like you would a 5 year old lol!!!! i think you mean i should be defining what $connection is???? Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208084 Share on other sites More sharing options...
Muddy_Funster Posted April 29, 2011 Share Posted April 29, 2011 you have the line mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect to server"); To use this within other functions as $connection you will need to first assign it to $connection, so change the line to read as $connection = mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect to server"); . Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208087 Share on other sites More sharing options...
LOSMAN Posted April 29, 2011 Author Share Posted April 29, 2011 YEY! thankyou! now i just have to sort my inserts out! thanks guys! "Error creating item in addItem Query in process order" Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208092 Share on other sites More sharing options...
LOSMAN Posted April 29, 2011 Author Share Posted April 29, 2011 last bit now and i can't do it! the orders are now working but my insert for order_items is wrong and i can't see why. i have echo'd it back to see what is trying to be inserted but it is not valid sql this is the error from the brwoser INSERT INTO Order_items ( 'OrderId' , 'ItemId' , 'qty' , 'price' ) VALUES ( 33,1,3,180.00)Error creating item in addItem Query in process order so i tried entering this into mysql, but it doesn't like it! INSERT INTO Order_items ( 'OrderId' , 'ItemId' , 'qty' , 'price' ) VALUES ( 33,1,3,180.00) Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208100 Share on other sites More sharing options...
LOSMAN Posted April 29, 2011 Author Share Posted April 29, 2011 done! Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208105 Share on other sites More sharing options...
JKG Posted April 29, 2011 Share Posted April 29, 2011 try wrapping figures in single quotes. INSERT INTO Order_items ( 'OrderId' , 'ItemId' , 'qty' , 'price' ) VALUES ('33','1','3','180.00') the decimal might be screwing it up. Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208106 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2011 Share Posted April 29, 2011 The problem is the quotes around the column names. And don't put numbers inside of quotes. That just uses extra processing time converting them from strings to numbers and in the case of a number like 180.00, it results in first a conversion to a floating point number, then to a decimal number, which can result in a floating point conversion error. Quote Link to comment https://forums.phpfreaks.com/topic/234979-i-know-you-probably-get-this-all-the-time/#findComment-1208107 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.