Andrew Asmer
Andrew Asmer

Reputation: 43

Product ID is not being stored in the database

can anyone help? my code doesn't seem to store the value of product id here in my code have a look I am also getting the ID from another table

    <?php
include("Connection.php");
$dTime = time();
$myValue = $_REQUEST['dValue'];
echo "<p>
The time is: {$dTime}<br/>
The choice is {$myValue}
</p>
";



$sql = "Select ID from product where NAME = '$myValue'";

$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) 
    $pid=$row["PRODUCT_ID"];



$sql2 = "INSERT INTO  `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL ,  '',  '$pid',  '1'
)";

$result2 = mysql_query($sql2);
?>

updated the code

Upvotes: 0

Views: 197

Answers (3)

user529649
user529649

Reputation:

in your original code there is no error handling,you should do something like this:

$sql = "Select ID from product where NAME = '$myValue'";
if ($sql) {
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result))
        $pid = $row["PRODUCT_ID"];



    $sql2 = "INSERT INTO  `starbucks`.`order_details` (
    `ID` ,
    `ORDER_ID` ,
    `PRODUCT_ID` ,
    `QTY`
    )
    VALUES (
    NULL ,  '',  '$pid',  '1'
    )";

    $result2 = mysql_query($sql2);
    if (!$result2) {
        echo mysql_error();
        break;
    }
} else {
    echo mysql_error();
}

And see what error you get.

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270677

You have an incorrect array value for $id instead of the array's ID key:

$id = $row;

// Should be
$id = $row['ID'];

Upvotes: 1

shiying yu
shiying yu

Reputation: 103

$id = $row["ID"]

instead of:

$id = $row;

Upvotes: 1

Related Questions