Jump to content

I know you probably get this all the time!


LOSMAN

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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");

?>

Link to comment
Share on other sites

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");

.

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

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.

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.