Forum Moderators: coopster

Message Too Old, No Replies

Help with code

dunno

         

bono

3:21 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



can anyone see where my code is going wrong?

$sql="SELECT count(*) FROM orders WHERE sessionID = $sessionID AND Date = $today";
$result = mysql_query(sql);

if ($result="1") {
$query = "INSERT INTO orders VALUES ('', '$order','$today','$total','','processing','$sessionID')";
}
if ($result = "0") {
$query = "UPDATE orders SET ( order='$order', totalammount=$total) WHERE 'sessionID'=$sessionID ";
}

think i may be tryin to calculate the result of the query wrong...

boxrec

3:22 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



if ($result="1")

is always true! you mean

if ($result == "1") {

coopster

10:43 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, boxrec.

Although you are correct in the assignment versus comparison difference here, the logic is still incorrect and there are a few other errors. bono, what you need to do is FETCH your data from the result set first. Also, note the bold updates to your original code ...


// AS is what is called an ALIAS; it lets us refer to the column later on ...
$sql="SELECT count(*) AS count FROM orders WHERE sessionID = $sessionID AND Date = $today";
// You forgot the dollar sign to indicate sql is a variable:
$result = mysql_query($sql);
// Here is where you will FETCH the data from the result set,
// notice how we use the ALIAS defined earlier:
$row = mysql_fetch_array($result);
if ($row['count'] == 1) {

$query = "INSERT INTO orders VALUES ('', '$order','$today','$total','','processing','$sessionID')";
}
if ($row['count'] == 0) {
$query = "UPDATE orders SET ( order='$order', totalammount=$total) WHERE sessionID=$sessionID ";
}

Note: if that Date column is truly of DATE type, you may need to enclose it in quotation marks, too.

$sql="SELECT count(*) AS count FROM orders WHERE sessionID = $sessionID AND Date = '$today'";

bono

8:22 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Thanks coopster,

was getting towards the end of the day yesterday so I didnt really have the energy to sort it out was all still theoretical..

ps~The date was done like this.... $today = date("Ymd");

bono

9:40 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Thanks.. it worked after abit of jiggery pokery... must remember not to use order as a field name.. lol

heres my final code :-)

$sql="SELECT count(*) AS count FROM orders WHERE sessionID = '$sessionID' AND Date = '$today'";
$result = mysql_query($sql);

$row = mysql_fetch_array($result);
if ($row['count'] == 0) {
$query = "INSERT INTO orders VALUES ('','$order','$today','$total','','processing','$sessionID')";
echo "ran INSERT";
}
if ($row['count'] == 1) {
$query = "UPDATE orders SET custorder='$order', totalamount='$total' WHERE sessionID='$sessionID' AND Date = '$today'";
echo "ran UPDATE";
}