12 Replies - 871 Views - Last Post: 05 June 2012 - 07:19 AM Rate Topic: -----

#1 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

update statment with image not working

Posted 05 June 2012 - 06:02 AM

I have a php page with form for updating records and image I don’t know what is wrong with the update statement ,,, the values of fields are taken and I can see them on url through the GET method ... But when I run the page and update record information is not changing and nothing appear on the page ,,, since none of fields r taking the update I think my update statement having problem ,,,here is the code ,,, I really wish some1 can help ...

// Connect to the database
    require("includes/conn.php");
    // Script Variables

    $target_dir = 'images/';

    $file_given = false;
    $inputs_given = false;
    $id_given = false;

    if(isset($_POST['serialid']) && $_POST['serialid'] != "")
    {
        $serialid = $_POST['serialid'];
        $id_given = true;
    }


            // You only need to catch input from a create or modify action, so start by checking for ALL the REQUIRED inputs
            if(isset($_POST['name']) && $_POST['name'] != "" && isset($_POST['description']) && $_POST['description'] != "" && isset($_POST['price']) && $_POST['price'] != "")
            {
                $name = $_POST['name'];
                $paragraph = $_POST['description'];
                $price = $_POST['price'];

                if(isset($_POST['picture']) && $_POST['picture'] != "")
                {
                    $picture = basename($_FILES['picture']['name']);
                    $file_given = true; 
                } 

                // Just some verification (not really much, but you can write your own functions and slot them in
                $name_safe = true;
                $description_safe = true;
                $price_safe = true;
                $picture_safe = false;


                if($_FILES["picture"]["type"] == "image/gif" || $_FILES["picture"]["type"] == "image/jpg" || $_FILES["picture"]["type"] == "image/png" || $_FILES["picture"]["type"] == "image/bmp")
                    $picture_safe = true;

                if($name_safe && $description_safe && $price_safe && $picture_safe)
                    $inputs_given = true;
            }



        if($id_given && $inputs_given)
        {
            // Search for the record and see if it exists
            $get_record = mysql_query("SELECT serial, picture FROM products WHERE serial='$serialid'");
            $record_exists = mysql_num_rows($get_record);

            if($record_exists == 1)
            {
                if($file_given)
                {
                    $update_image = ", picture='$picture'";

                    // Now we need to remove the old image from the file system and upload our new one in it's place

                    $previous_image = mysql_result($get_record,'0','picture');
                    unlink($target_dir . $previous_image);

                    //Now that the previous image has been removed, we need to upload our new image
                    $new_image = $target_dir . $picture ;
                    move_uploaded_file($_FILES['picture']['tmp_name'], $new_image);
                }
                else
                    $update_image = "";

                if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price', " . $update_image . " WHERE serial='$serialid'"))
                    $action_output = "Record successfully modified.";
                else
                    $action_output = "Record modification unsuccessful.";
            }
            else
                $action_output = "The record id you specified does not exist.";
        }


?>
<html>
    <head>
        <title>Manage Records</title>
    </head>

    <body>
        <?php echo $action_output; ?>
    </body>
</html>
<?php
    // Disconnect from the database
?>


My Modify Form is this

<?php

    // Connect to the database
    require("includes/conn.php");
    $id_given = false;
    if(isset($_POST['serialid']) && $_POST['serialid'] != "")
    {
        $serialid = $_POST['serialid'];
        $id_given = true;
    }

    if($id_given)
    {
        $get_record = mysql_query("SELECT * FROM products WHERE serial='$serialid'");
        $record = mysql_fetch_array($get_record);

        $output = '<form method="POST" enctype="multipart/form-data" action="update.php?serialid=' . $record['serialid'] . '&action=modify">

                    <table>
                    <tr>
                    <td>Name:</td>
                    <td><input name="name" type="text"  value="' . $record['name'] . '"/></td>
                    </tr>
                    <tr>
                    <td>Description :</td>
                    <td><textarea name="description" cols="45" rows="5">' . $record['description'] . '</textarea></td>
                    </tr>
                    <tr>
                    <td>Price:</td>
                    <td><input name="price" type="text"  value="' . $record['price'] . '"/></td>
                    </tr>
                    <td colspan="2"><img height="50" width="50" src="../images/' . $record['picture'] . '"/><br/>' . $record['picture'] . '</td>
                    </tr> 
                    <tr>
                    <td>Modify Image:</td>
                    <td><input name="picture" type="file" value="" /></td>
                    </tr>
                    <tr>
                    <td colspan="2"><input type="submit" value="Modify Record"/>
                    </td>
                    </tr>
                    </table>

</form>';

    }
    else
        $output = 'No record id was specified.';
?>
<html>
    <head>
        <title>Modify Record</title>
    </head>

    <body>
        <?php echo $output; ?>
    </body>
</html>
<?php
    // Disconnect from the database
?>


Is This A Good Question/Topic? 0
  • +

Replies To: update statment with image not working

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: update statment with image not working

Posted 05 June 2012 - 06:09 AM

uh, you say that you have GET data (URL) but your script looks for POST data ...
Was This Post Helpful? 0
  • +
  • -

#3 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

Re: update statment with image not working

Posted 05 June 2012 - 06:13 AM

I mean to say I used a GET just to see the value on the URL which appear "For testing I used GET" plz check my update statement ,,, for days Im trying to know what the problem still I dont know whats wrong with my code
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: update statment with image not working

Posted 05 June 2012 - 06:16 AM

but even if you put your data in GET solely for the purpose of testing, how are you supposed to have POST data then?

if there is an error, mysql_error() will tell you.
Was This Post Helpful? 0
  • +
  • -

#5 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

Re: update statment with image not working

Posted 05 June 2012 - 06:22 AM

When I test with the GET ,, I change all the code and the form method also to GET just for testing purpose but I dont think this is the problem ,,,
my main inquiry is how to update the picture but nothing is being update ?!
Was This Post Helpful? 0
  • +
  • -

#6 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

Re: update statment with image not working

Posted 05 June 2012 - 06:27 AM

it don't give me error ,,, after clicking update it show me an empty page and url will be this:
http://localhost/Shopping/update.php?name=View+Sonic+LCD&description=LCD&price=250&picture=C%3A%5CDocuments+and+Settings%5Ce2565%5CMy+Documents%5CTwasul%5Ctlogo%5Cicon%5Cpic1.jpg&serialid=1



I really appreciate ur reply and hope we can solve it
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: update statment with image not working

Posted 05 June 2012 - 06:30 AM

View Postlune, on 05 June 2012 - 03:22 PM, said:

When I test with the GET ,, I change all the code and the form method also to GET just for testing purpose but I dont think this is the problem ,,,

and why weren’t you giving us that code so we could have excluded that problem from the start?


View Postlune, on 05 June 2012 - 03:22 PM, said:

my main inquiry is how to update the picture but nothing is being update ?!

because you directly uploaded picture sits in a temporary directory and is soon to be deleted and the file name is not a place in the file system.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: update statment with image not working

Posted 05 June 2012 - 06:36 AM

you should test with POST ...
Was This Post Helpful? 0
  • +
  • -

#9 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

Re: update statment with image not working

Posted 05 June 2012 - 06:43 AM

Dormilich ,,,, I think u mixed up with all this explanation let me simplify it here.

Im creating a shopping web ,,, and on the admin part I want to allow the admin to insert a product information with a picture ,,, I did that and the uploaded picture is being saved in a folder called "images" and the name of the picture is saved in the database. This part is working .

Now the 2nd part which Im struggling with is that I want to create a page that allow the admin to update the product information and change the product picture ,, in case admin changed the picture the previous picture of the product will be deleted from the "images" folder and the new one will be saved.

I tried many codes and didn't worked till I reached the above code and I stuck ,,, I have no idea how this could be done ,,, so I wish I find a way to complete the 2nd part .

sorry for my long explanation and I really appreciate ur replies coz I know u r trying to help me
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: update statment with image not working

Posted 05 June 2012 - 06:52 AM

some steps you need to verify in your code:

what exactly goes into the picture field?

does your query fail?

does your serialID match?

do you get into the right condition branch?
Was This Post Helpful? 0
  • +
  • -

#11 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

Re: update statment with image not working

Posted 05 June 2012 - 07:04 AM

I dont know if I can answer ur questions ,,, I really don't know :( but the id matches
I think the query may be fail because when I check the record on the database it is not updated and on images folder same image is there.
I think the update is not executing at all :(
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,402
  • Joined: 08-June 10

Re: update statment with image not working

Posted 05 June 2012 - 07:07 AM

View Postlune, on 05 June 2012 - 04:04 PM, said:

I dont know if I can answer ur questions ,,,

the process answering those question is called: Debugging.

and yes, you need some extra code (e.g. var_dump()) to make the values of interest visible. otherwise we’re fishing in the dark without getting further.
Was This Post Helpful? 0
  • +
  • -

#13 lune  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-June 12

Re: update statment with image not working

Posted 05 June 2012 - 07:19 AM

so I was fishing in the dark from yesterday :rolleyes2:

I cant debug now I need to be @ work I will try the
var_dump();
and c whats going wrong

To be honest this function is new for me ,,, I was debugging with the GET method ,,, I thing this way wasn't helping @ all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1