0

This is a simple shopping cart I found somewhere, a long time ago and now in need of to make an ordering system of. But it for some or other bizarre reason not insert all of the fields I feed it!

My functions below:

function get_categoryid($pid){
$result=mysql_query("select product_category_id from shopping_products where serial=$pid") or die("select product_category_id from shopping_products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['product_category_id'];
}
 function get_thecategory($pid){
$result=mysql_query("select product_category_title from shopping_products where serial=$pid") or die("select product_category_title from shopping_products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['product_category_title'];
 }
}

Here is the PHP code where it gets inserted into the "shopping_order_detail" table after the clients has filled in his/her info.

if ( isset($_REQUEST['command']) && $_REQUEST['command']=='update'){
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $address=$_REQUEST['address'];
    $phone=$_REQUEST['phone'];
    $loyalty=$_REQUEST['loyalty'];

    $result=mysql_query("insert into shopping_customers values('','$name','$email','$address','$phone','$loyalty')");
    $customerid=mysql_insert_id();
    $date=date('Y-m-d');
    $result=mysql_query("insert into shopping_orders values('','$date','$customerid')");
    $orderid=mysql_insert_id();

    /* ---- below my problem i think --- */
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=$_SESSION['cart'][$i]['qty'];
        $price=get_price($pid);
        $pcategoryid=get_categoryid($pid);
        $pcategory=get_thecategory($pid);
        $pvendor=get_thevendor($pid);

        mysql_query("INSERT INTO shopping_order_detail VALUES($orderid,$pid,$q,$price,$pcategoryid,$pcategory,$pvendor)");

    echo "vendor -" . $pvendor . "  Order ID - " . $orderid . " - Product ID - " . $pid . " - Category ID ". $pcategoryid ." - Price R ". $price .".00  - Quantity ". $q. "- Category Title: ". $pcategory ."<Br>";
    }
    echo "<br>=======================================================================================<br>";
    echo "<br>Data echoed above this works fine.. and pulls the correct shopping items<br>";
    echo "<br>Sucess ......Data Inserted!<br>";
    die('Thank You! your order has been placed');
}

Here is my problem !

The "$pcategoryid" is inserted into the table , But whatever amount of fields i ad after this not only does not insert, but it also for some or other reason prevent any information being inserted with this piece of script.

To recap my problem: If I remove "$pcategory" and "$pvendor" from the script the data gets inserted. if I add these two fields in question then the script does not insert, But/And it does not give any error messages.

My "Echo === etc" commands at the bottom of the script shows me that my script is working as it shows those two fields in question when it cycles through list of items

I have set my PHP error codes this way:

 ini_set('display_errors',1);
 ini_set('display_startup_errors',1);
 error_reporting(-1);

Is it some kind of restriction in Mysql as the Table in question does not have a primary key? I am at a loss for a solution!

Please, Any help will be appreciated thank you.

8
  • On a sidenote, PLEASE STOP using mysql and NON paramaterized queries. Commented Dec 3, 2014 at 15:26
  • can you post the echo of the query? echo "INSERT INTO shopping_order_detail VALUES($orderid,$pid,$q,$price,$pcategoryid,$pcategory,$pvendor)" Commented Dec 3, 2014 at 15:27
  • Make sure that the order of your fields in your insert query is correct and that these fields actually exist on your table. And on a sidenote, please DO stop using mysql and NON parameterized queries. You will save youself some serious headaches in a later time. Commented Dec 3, 2014 at 15:30
  • mysql_query() or die(mysql_error()) Commented Dec 3, 2014 at 15:32
  • By the way, is session_start(); loaded? Commented Dec 3, 2014 at 15:34

2 Answers 2

1

Try this

$result=mysql_query("insert into shopping_customers values('$name','$email','$address','$phone','$loyalty')");

And

$result=mysql_query("insert into shopping_orders values('$date','$customerid')");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Ranjeet Singh! Between yours and the previous users Giwwel I found the solution
1

the result of get_thecategory($pid) is the category-title, a string.. So the $pcategory must be quoted for the query like this:

mysql_query("INSERT INTO shopping_order_detail VALUES($orderid,$pid,$q,$price,$pcategoryid,'$pcategory',$pvendor)");

Maybe are more variables strings. quote these in query, too

1 Comment

Thank you @Giwwel ! I am new to PHP and only now realise that all the other fields are not strings. Thank you. It Works !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.