Re: If( Query)

From: Date: Sun, 07 Sep 2008 22:03:33 +0000
Subject: Re: If( Query)
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-45373@lists.php.net to get a copy of this message
Niel Archer wrote:
Hi
I have the following function: function add_item($item_name,$item_desc,$item_price,$item_man_id,$item_cat_id,$item_pix)
    {
    connect();
    if($item_pix == "")
    {
        $sql = "INSERT INTO items (item_name,item_desc,item_price,item_man_id,item_cat_id) VALUES ('$item_name','$item_desc','$item_price','$item_man_id','$item_cat_id')";
    }
    else {
        $sql = "INSERT INTO items (item_name,item_desc,item_price,item_pix,item_man_id,item_cat_id) VALUES ('$item_name','$item_desc','$item_price','$item_pix','$item_man_id','$item_cat_id')";
    }
    mysql_query($sql);
    return;
} I am using the if statement because i want it so that if no picture is uploaded the entry is blank and the mysql database has a default entry of na.gif which is a "picture coming soon picture". It works fine when i run in localy on MAMP, but if i run it on my web server it doesnt add the row.
    
You should be checking the mysql_query call for success and output the error if it fails. Something like: mysql_query($sql) or die('Insert failed: ' . mysql_error()); You'll now why it's failing then. Make sure you have error reporting enabled.
Is this a compatability error? or is there a better way to write this?
    
-- Niel Archer I have fixed it now:
function add_item($item_name,$item_desc,$item_price,$item_man_id,$item_cat_id,$item_pix) { connect(); if($item_pix == "") {
       $sql = "INSERT INTO items (item_name,item_desc,item_price,item_man_id,item_cat_id) VALUES ('$item_name','$item_desc','$item_price','$item_man_id','$item_cat_id')";
} else {
       $sql = "INSERT INTO items (item_name,item_desc,item_price,item_pix,item_man_id,item_cat_id) VALUES ('$item_name','$item_desc','$item_price','$item_pix','$item_man_id','$item_cat_id')";
} mysql_query($sql); return; } Thanks anyway.

« previous php.db (#45373) next »