0

I have need help in the below coding, right now it update the image every time i insert new trough form, i need it should update/insert image in each row not update the same image, kindly help .. code is below

<?PHP
if(isset($_POST['add_value'])){
    $sql ="INSERT INTO tb_special_offer (offer_price, offer_title, offer_desc, offer_link) VALUES ('"
        .addslashes($_REQUEST['offer_price'])."', '"
        .addslashes($_REQUEST['offer_title'])."', '"
        .addslashes($_REQUEST['offer_desc'])."', '"
        .addslashes($_REQUEST[offer_link])."')";
    $qry = mysql_query($sql) or die (mysql_error());
    //Image

    if($_FILES['offer_img']['name']){
        $uploaded_image = $_FILES['offer_img']['name'];
        $imgpath = "userfiles/specialoffer/";
        if(file_exists($imgpath.$uploaded_image)) unlink($imgpath.$uploaded_image);
        if(!move_uploaded_file($_FILES['offer_img']['tmp_name'], $imgpath.$uploaded_image)){
            $errMsg= "UPLOAD ERROR..!!!".$_FILES['offer_img']['name'];
        }
        else {
            $sql = "update tb_special_offer set offer_img='$uploaded_image'   ";
            $qry = mysql_query($sql) or die (mysql_error());
        }
    }

    header("Location: specialoffer?msg=Special Offer Added Successfully!");
    exit;
}
?>
2
  • SO are you saying that the code is not looping well when it takes the file from $_FILES? Commented Jun 26, 2011 at 7:50
  • yes . i am looking for the code that insert the image every time the code runs not just update the same image mean image with each row of information. Commented Jun 26, 2011 at 8:09

1 Answer 1

1

Your query means that all rows in your database get that image as value for the offer_img column. Update means just that: update a row.

If you want to update a specific row, not every row, do something like this:

update tb_special_offer set offer_img='$uploaded_image' where id=xxxx

But I suspect you want to use an INSERT query. As you've not provided any more info I cannot write it for you, but it should be easy. Just read the manual, but it boils down to something like

INSERT into tb_special_offer (offer_img) VALUES ('$uploaded_image')
Sign up to request clarification or add additional context in comments.

4 Comments

i try that as well, my image int type is used now is Varchar, kindly send me insert comment that insert the image in each row each time i upload the form ..
My sql database is look like below CREATE TABLE IF NOT EXISTS tb_special_offer2 ( offer_id int(11) NOT NULL auto_increment, offer_img varchar(64) NOT NULL, offer_title varchar(500) default NULL, offer_price varchar(50) NOT NULL, offer_desc text NOT NULL, offer_link varchar(200) NOT NULL, PRIMARY KEY (offer_id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=54 ;
thanks i done. i just replace the full insert quarry with update .. thanks for urs answers and help.. :)
If you think my answer was correct, please mark it as such. Otherwise, provide your own sollution in a separate answer. This will help future googlers :) meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.