update query whre to put it?

Hello,

I found some code and modified it to my needs.
It makes 3 images in deferent sizes and puts them in separated directories.
Probably not the most elegant way but it works for me.
(But I’m always open fur suggestions.)

The problem I have is when/while the image is uploaded I would like to store it in my Dbase.

I am trying al afternoon but nothing seems to work. :-\

After submitting the ‘id’ is no longer in the url, and where do I put my update query?

Someone give me a hint?

submit.php
[php]

<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?> <?php // Connect DBase include ("../../inc/connect_Shop.inc.php"); // GET ID FROM URL $_id=$_GET['id'] ; ?> <?php // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { // soorten afbeeldingen // if uploaded image was JPG/JPEG if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){ $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was GIF if($_FILES["image_upload_box"]["type"] == "image/gif"){ $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]); } // BMP doesn't seem to be supported so remove it form above image type test (reject bmps) // if uploaded image was BMP if($_FILES["image_upload_box"]["type"] == "image/bmp"){ $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was PNG if($_FILES["image_upload_box"]["type"] == "image/x-png"){ $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]); } // Afbeelding upload 3 verschillende dir's //standaard $remote_file = "../../images/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file,100); chmod($remote_file,0644); //klein $remote_file_kl = "../../images_kl/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file_kl,100); chmod($remote_file_kl,0644); //Thumb $remote_file_tmb = "../../images_tmb/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file_tmb,100); chmod($remote_file_tmb,0644); // get width and height of original image list($image_width, $image_height) = getimagesize($remote_file); if($image_width>$img_width || $image_height >$img_height){ $proportions = $image_width/$image_height; if($image_width>$image_height){ $new_width = 800; $new_height = round(800/$proportions); $new_width_kl = 160; $new_height_kl = round(160/$proportions); $new_width_tmb = 100; $new_height_tmb = round(100/$proportions); } else{ $new_height = 800; $new_width = round(800*$proportions); $new_height_kl = 160; $new_width_kl = round(160*$proportions); $new_height_tmb = 100; $new_width_tmb = round(100*$proportions); } $image_source = imagecreatefromjpeg($remote_file); $new_image = imagecreatetruecolor($new_width , $new_height); imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); imagejpeg($new_image,$remote_file,100); $new_image_kl = imagecreatetruecolor($new_width_kl , $new_height_kl); imagecopyresampled($new_image_kl, $image_source, 0, 0, 0, 0, $new_width_kl, $new_height_kl, $image_width, $image_height); imagejpeg($new_image_kl,$remote_file_kl,100); $new_image_tmb = imagecreatetruecolor($new_width_tmb , $new_height_tmb); imagecopyresampled($new_image_tmb, $image_source, 0, 0, 0, 0, $new_width_tmb, $new_height_tmb, $image_width, $image_height); imagejpeg($new_image_tmb,$remote_file_tmb,100); imagedestroy($new_image); imagedestroy($new_image_kl); imagedestroy($new_image_tmb); } imagedestroy($image_source); header("Location: submit.php?upload_message=Afbeelding ge-upload&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: submit.php?upload_message=Bestand moet een jpg, gif or png zijn en kleiner dan4MB &upload_message_type=error"); exit; } } ?> Afbeeldingen uploaden <!-- body,td,th { font-family: Arial, Helvetica, sans-serif; color: #333333; font-size: 12px; }

.upload_message_success {
padding:4px;
background-color:#009900;
border:1px solid #006600;
color:#FFFFFF;
margin-top:10px;
margin-bottom:10px;
}

.upload_message_error {
padding:4px;
background-color:#CE0000;
border:1px solid #990000;
color:#FFFFFF;
margin-top:10px;
margin-bottom:10px;
}

–>

Afbeelding uploaden

    <?php if(isset($_REQUEST['upload_message'])){?>
        <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
        <?php echo htmlentities($_REQUEST['upload_message']);?>
        </div>
	<?php }?>
Afbeelding, soorten jpg, gif, png:



uploaden kan even duren !

  <label>De 3 afbeeldingen hebben Max. maten: <br>800x800pc<br>160x160pc<br>100x100pc<br> </label>

  </TR></TD><TR><TD ALIGN=LEFT>
  <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
  
  <strong>Notes:</strong><br />

Als de afbeedling ge-upload is dan zal deze hieronder getoond worden.
Afmeting max 160x160pc.

  </tr></td><tr><td align=middle>
<?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){ ?>

<?php } ?>

[/php]

This query I have build and rebuild but somehow the ’ id ’ from the url doesnot get thrue.

[php]// SQL - update img
mysql_query("UPDATE Prod SET image = ‘name’ WHERE id = ‘$id’ ");[/php]

Thanks.

(It is for the administrator so I do not bother about any security issues.)

Rob

based on a quick glance it looks like $id is never definined… you have:

"$_id=$_GET[‘id’] ; "

try mysql_query("UPDATE Prod SET image = ‘name’ WHERE id = ‘$_id’ ");

I don’t remember however the rules when using _infront of variable names so it might be better to go with just changing"$id=$_GET[‘id’] ; "

Let us know if this works or if there are other problems that need addressing.

The reason why you aren’t getting the $id value is because you are searching for where id = $id, since $id is part of the string.

this is what you want to do:
[php]
mysql_query(“UPDATE Prod SET image = ‘name’ WHERE id = '” . $id . "’ ");
[/php]

Thanks for all the help,

I am almost there…

I tried your proposals but I can not get it to work. Somehow I’m overseeing something but after a while it all looks the same.
I open the page with:
http://192.168.2.3/catalogus/admin/img/submit.php?id=21 As you can see there is an “id” .

When I use this “id” hard coded the update query works.
As far I can see, after submitting the form the images will be uploaded and the query executed. After this the URL changes into :
http://192.168.2.3/catalogus/admin/img/submit.php?upload_message=Afbeelding%20ge-upload&upload_message_type=success&show_image=ben440.jpg

This part gives me the :-
[php] // GET ID FROM URL
$id=$_GET[‘id’] ;
// insert image name
//Doesnot work | mysql_query(“UPDATE Prod SET image = '”.$_FILES[“image_upload_box”][“name”]."’ WHERE id = ‘$id’ ");
mysql_query("UPDATE Prod SET image = ‘image_upload_box’ WHERE id = ‘21’ "); [/php]

What do I wrong?

The total code so far…
[php]<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>

<?php // Connect DBase include ("../../inc/connect_Shop.inc.php"); ?> <?php // upload the file if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) { // file needs to be jpg,gif,bmp,x-png and 4 MB max if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000)) { // soorten afbeeldingen // if uploaded image was JPG/JPEG if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){ $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was GIF if($_FILES["image_upload_box"]["type"] == "image/gif"){ $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]); } // BMP doesn't seem to be supported so remove it form above image type test (reject bmps) // if uploaded image was BMP if($_FILES["image_upload_box"]["type"] == "image/bmp"){ $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]); } // if uploaded image was PNG if($_FILES["image_upload_box"]["type"] == "image/x-png"){ $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]); } // Afbeelding upload 3 verschillende dir's //standaard $remote_file = "../../images/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file,100); chmod($remote_file,0644); //klein $remote_file_kl = "../../images_kl/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file_kl,100); chmod($remote_file_kl,0644); //Thumb $remote_file_tmb = "../../images_tmb/".$_FILES["image_upload_box"]["name"]; imagejpeg($image_source,$remote_file_tmb,100); chmod($remote_file_tmb,0644); // get width and height of original image list($image_width, $image_height) = getimagesize($remote_file); if($image_width>$img_width || $image_height >$img_height){ $proportions = $image_width/$image_height; if($image_width>$image_height){ $new_width = 800; $new_height = round(800/$proportions); $new_width_kl = 160; $new_height_kl = round(160/$proportions); $new_width_tmb = 100; $new_height_tmb = round(100/$proportions); } else{ $new_height = 800; $new_width = round(800*$proportions); $new_height_kl = 160; $new_width_kl = round(160*$proportions); $new_height_tmb = 100; $new_width_tmb = round(100*$proportions); } $image_source = imagecreatefromjpeg($remote_file); $new_image = imagecreatetruecolor($new_width , $new_height); imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); imagejpeg($new_image,$remote_file,100); $new_image_kl = imagecreatetruecolor($new_width_kl , $new_height_kl); imagecopyresampled($new_image_kl, $image_source, 0, 0, 0, 0, $new_width_kl, $new_height_kl, $image_width, $image_height); imagejpeg($new_image_kl,$remote_file_kl,100); $new_image_tmb = imagecreatetruecolor($new_width_tmb , $new_height_tmb); imagecopyresampled($new_image_tmb, $image_source, 0, 0, 0, 0, $new_width_tmb, $new_height_tmb, $image_width, $image_height); imagejpeg($new_image_tmb,$remote_file_tmb,100); // GET ID FROM URL $id=$_GET['id'] ; // insert image name //Doesnot work | mysql_query("UPDATE Prod SET image = '".$_FILES["image_upload_box"]["name"]."' WHERE id = '$id' "); mysql_query("UPDATE Prod SET image = 'image_upload_box' WHERE id = '21' "); imagedestroy($new_image); imagedestroy($new_image_kl); imagedestroy($new_image_tmb); } imagedestroy($image_source); header("Location: submit.php?upload_message=Afbeelding ge-upload&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]); exit; } else{ header("Location: submit.php?upload_message=Bestand moet een jpg, gif or png zijn en kleiner dan4MB &upload_message_type=error"); exit; } } ?> Afbeeldingen uploaden <!-- body,td,th { font-family: Arial, Helvetica, sans-serif; color: #333333; font-size: 12px; }

.upload_message_success {
padding:4px;
background-color:#009900;
border:1px solid #006600;
color:#FFFFFF;
margin-top:10px;
margin-bottom:10px;
}

.upload_message_error {
padding:4px;
background-color:#CE0000;
border:1px solid #990000;
color:#FFFFFF;
margin-top:10px;
margin-bottom:10px;
}

–>

Afbeelding uploaden

    <?php if(isset($_REQUEST['upload_message'])){?>
        <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
        <?php echo htmlentities($_REQUEST['upload_message']);?>
        </div>
	<?php }?>
Afbeelding, soorten jpg, gif, png:



uploaden kan even duren !

  <label>De 3 afbeeldingen hebben Max. maten: <br>800x800pc<br>160x160pc<br>100x100pc<br> </label>

  </TR></TD><TR><TD ALIGN=LEFT>
  <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
  
  <strong>Notes:</strong><br />

Als de afbeedling ge-upload is dan zal deze hieronder getoond worden.
Afmeting max 160x160pc.

  </tr></td><tr><td align=middle>
<?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){ ?>

<?php } ?>

[/php]
Best regard,

Rob

is this word for word the query that works?
[php]mysql_query("UPDATE Prod SET image = ‘image_upload_box’ WHERE id = ‘21’ “); [/php]
as there’s a big difference from:
[php]“UPDATE Prod SET image = '”.$_FILES[“image_upload_box”][“name”].”’ WHERE id = ‘$id’ "[/php]

mainly ‘image_upload_box’ from the first is a string… not a file… I didn’t realize you were trying to store a file in the db at first or I would have gotten to this sooner… So while I look into this a little further let me ask you…

Is there a specific reason you’re wishing to store the image in the database? Is the sql field setup properly to accept a file? And have you considered the option of storing the file on the server while, its location and other information in the database for use in retrieval?(usually the better option)

I’m not sure I understand your answer, (PHP newby)

But I like to use 3 sizes of images, and store only the image name in the dbase.
The first query works.
[php]mysql_query("UPDATE Prod SET image = ‘image_upload_box’ WHERE id = ‘21’ [/php]

The second not, I use the same kind of query to update other fields in the dbase with the $id from the url.

Therefore I assumed the next one had to work, it looks like the page forgets the id ?!?!?!
[php]“UPDATE Prod SET image = '”.$_FILES[“image_upload_box”][“name”]."’ WHERE id = ‘$id’ "[/php]

Must I edit the next line and how would I do that?
Maby the id must be also in here but I do not understand how and why.
[php]header(“Location: submit.php?upload_message=Afbeelding ge-upload&upload_message_type=success&show_image=”.$_FILES[“image_upload_box”][“name”]);[/php]

Thanks.

sorry,

After reading my post…
This works: [php]mysql_query(“UPDATE Prod SET image = '”.$_FILES[“image_upload_box”][“name”]."’ WHERE id = ‘21’ ");[/php]

This doesn’t: [php]mysql_query(“UPDATE Prod SET image = '”.$_FILES[“image_upload_box”][“name”]."’ WHERE id = ‘$id’ ");[/php]
Today is even copy past is hard to do :smiley:

try this…

[php]$filename = ($_FILES[‘image_upload_box’][‘name’]);
mysql_query(“UPDATE Prod SET image = '” . $filename . “’ WHERE id = '” . $id" . ’ ");[/php]

I know breaking down the query isn’t absolutely necessary; it does make it easier for editing purposes and i’m wondering if the _FILES array is causing any issues with the double quotes, even though you would normally get a parse error… let me know

Sabmin’s code has the period miss placed after id[php]$filename = ($_FILES[‘image_upload_box’][‘name’]);
mysql_query(“UPDATE Prod SET image = '” . $filename . “’ WHERE id = '” . $id . "’ ");[/php]

Thank you Sabmin,

Sorry this doesn’t work…
I’ve tried without double quotes and all other options I can think off.

I get a error on the line where the query is :

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING 

This suggests that the quotes are wrong, but what is the right way?
[php]mysql_query(“UPDATE Prod SET image = '” . $filename . “’ WHERE id = '” . $id" . ’ ");
// I’ve have changed it in to :
mysql_query("UPDATE Prod SET image = ’ “. $filename . " ’ WHERE id = '” . $id . " ’ ");
[/php]
quotes around $id, typo :wink:
No more errors, but the name is not put in the dbase.

I still think something goes wrong with the $id …
I changed the html code to see if the $id is there and it displays the right one !
After “Submit” the id is gone , can that be the reason?
To test that how do I include this in:
[php]header(“Location: submit.php?upload_message=Afbeelding ge-upload&upload_message_type=success&show_image=”.$_FILES[“image_upload_box”][“name”]);[/php]
Or do I make a thinking error.
? Suggestions?
Thanks, Rob

are you passing the $id through after submission? like a hidden text field that posts the $id

Yeah I was getting ready for work at that point sorry for the typo–there was about to be a few below that I cought just before posting. :slight_smile:

try this:
[php]

//OR
//add this inside the form(keep in mind if you go this way you’ll need to use “$id = $_POST[‘id’]”):

[/php]

As for the filename issue, do me a favor and add this before the query:
[php]print_r($_FILES);
die;//don’t have to include this line but since we’re just debugging we don’t really need to run the rest of the script[/php]
and let us know what the results are.

:-* :-* Yes, it works !!! :-* :-*

I used the query I started with:

mysql_query("UPDATE Prod SET image = '".$_FILES["image_upload_box"]["name"]."' WHERE id = '$id' ");

Then there was :: !!!

<form action="submit.php?id=<?php echo $id; ?>" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">

I looked everywhere but didn’t see this line anymore. This did the trick, afters submitting the id was gone.

I also changed the line :

header("Location: submit.php?id=$id&upload_message=Afbeelding ge-upload&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);

[size=12pt]Foila, there it is.[/size]

Great help, I can go back to bed ;D

Thank you, thank you !!!

Glad we could help! and more importantly that the actual answer was discovered by you and help was merely assistance in direction! That is after all what programming and the learning of, makes most interesting! Please bring further issues to bounce between us again!

Sponsor our Newsletter | Privacy Policy | Terms of Service