I have a script which more or less works just fine, although it does something weird - it is a form, with a separate submit-button for uploading a picture - so far so good - the picture gets uploaded to the folder it's supposed to just fine, but when I try using the variable with the path to the picture in the SQL UPDATE statement, it just won't work - and I have no idea why. The whole of the script might be a bit much to post, so I've taken out the bits I think is important - if you guys need more, I can post the whole thing, of course: The actual processing of the image: if (isset ($_FILES['new_image'])){ $imagename = $_FILES['new_image']['name']; //Normalize the filename for storage $imagename = strtolower($imagename); $imagename = preg_replace("/^[a-z0-9 -_.]#i/", "", $imagename); $imagename = str_replace(" ","_",$imagename); $source = $_FILES['new_image']['tmp_name']; $target = "webgfx/".$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "webgfx/" . $imagepath; //This is the new file you saving $file = "webgfx/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 115; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; //The image file path going to the database $filePath = $url_pics . $imagename; } Code (markup): And the sql-query: $query_update="UPDATE $about_info SET s_fname='$fname',s_lname='$lname',s_title='$title',`desc`='$ridesc',s_bilde='$filePath' WHERE ID='$iID'"; Code (markup): The weird part is - as it stands now, if I echo the $filePath-variable in the form, it works just fine - if I submit the form (after uploading a picture) and then look at the database, all it finds is the "$url_pics"-part of the $filePath - the $imagename part does not transfer to the database-update query. I'm at a loss to why the variable doesn't contain what it's supposed to when I push "submit" the second time - any help here would be greatly appreciated.
How about adding some debugging output to see if your regular expression is operating as you expect it to? Without knowing what your filenames are like it's hard to say. Does the picture actually end up with the proper name once it's been stored in your webgfx directory? Or does it have a random name smelling slightly of mktemp?
The file itself ends up exactly where it's supposed to be It has nothing to do with the regexp - see, as I said, if I echo the variable in the form after submitting the photo, it shows the correct variable - eg. webgfx/pic1.jpg - but when using the variable in the update-statement, the only thing going into the database is the "webgfx/" part - the filename is all of a sudden gone. I'm thinking it has something to do with the if-statements, and that the $imagename-variable which is a part of the $filePath-variable is being reset or something, before the update-statement... I'm posting the whole code, incl. the form here, so maybe it's easier to see: elseif ($iID=$_POST['ID']) { $query_edit = "SELECT * FROM $about_info WHERE ID = ('$iID')"; mysql_query($query_edit) or die(mysql_error()); $result_edit=mysql_query($query_edit); $user_array = mysql_fetch_array($result_edit,MYSQL_BOTH); $user_array['desc'] = str_replace("&","&",$user_array['desc']); $user_array['desc'] = str_replace("'","'",$user_array['desc']); $cnum = $user_array['cat_num']; if (isset ($_FILES['new_image'])){ $imagename = $_FILES['new_image']['name']; //Normalize the filename for storage $imagename = strtolower($imagename); $imagename = preg_replace("/^[a-z0-9 -_.]#i/", "", $imagename); $imagename = str_replace(" ","_",$imagename); $source = $_FILES['new_image']['tmp_name']; $target = "webgfx/".$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "webgfx/" . $imagepath; //This is the new file you saving $file = "webgfx/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 115; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; //The image file path going to the database $filePath = $url_pics . $imagename; } if (isset($_POST['submit'])) { $formArray['desc']=$_POST['ridesc']; $formArray['s_title']=$_POST['title']; $formArray['fname']=$_POST['fname']; $formArray['lname']=$_POST['lname']; $formArray['bilde']="webgfx/" . $imagepath; } else { $formArray['desc']=$user_array['desc']; $formArray['s_title']=$user_array['s_title']; $formArray['fname']=$user_array['s_fname']; $formArray['lname']=$user_array['s_lname']; $formArray['bilde']=$user_array['s_bilde']; } //the variables taken from the form $iID=$_POST['ID']; $fname=$_POST['fname']; $lname=$_POST['lname']; $title=$_POST['title']; $ridesc=$_POST['ridesc']; $update=$_POST['update']; $fname=mysql_real_escape_string($fname); $lname=mysql_real_escape_string($lname); $title=mysql_real_escape_string($title); $ridesc=mysql_real_escape_string($ridesc); //check to see if values have been entered //if not, display error-message if(!isset($_POST["fname"])) { $fname_error = ""; } elseif (empty($_POST["fname"])) { $fname_error = "<p class='error'>Du har ikke skrevet inn fornavn</p>"; } else { $fname_error = ""; } if(!isset($_POST["lname"])) { $lname_error = ""; } elseif (empty($_POST["lname"])) { $lname_error = "<p class='error'>Du har ikke skrevet inn etternavn</p>"; } else { $lname_error = ""; } if(!isset($_POST["title"])) { $title_error = ""; } elseif (empty($_POST["title"])) { $title_error = "<p class='error'>Du har ikke skrevet inn noen tittel</p>"; } else { $title_error = ""; } if(!isset($_POST["ridesc"])) { $desc_error = ""; } elseif (empty($_POST["ridesc"])) { $desc_error = "<p class='error'>Du har ikke skrevet inn noen tekst</p>"; } else { $desc_error = ""; } if ((isset($_POST["submit"])) && (!isset($_POST["update"]))) { $update_error = "<p class='error'>Du har ikke haket av for å oppdatere!</p>"; } else { $update_error = ""; } if ((isset($_POST["ridesc"])) && (isset($_POST["update"])) && $desc_error =="" && $title_error == "" && $fname_error == "" && $lname_error == "") { $query_update="UPDATE $about_info SET s_fname='$fname',s_lname='$lname',s_title='$title',`desc`='$ridesc',s_bilde='$filePath' WHERE ID='$iID'"; mysql_query($query_update) or die(mysql_error()); if($iID == 61) { header("location: $domain/index.php?page=paamelding"); } else { header("location: $domain/index.php?page=about"); } } ?> <fieldset id='about_innlegg'><legend>Endre om-oss informasjonen</legend> <form id='about_form_endring' method='post' action='<?php echo "$domain"; ?>/index.php?page=edit' enctype='multipart/form-data'> <p><input type='hidden' value='<?php echo $user_array['ID']; ?>' name='ID' /></p> <?php echo $update_error; ?> <?php if (($cnum == 3) || ($cnum == 4) || ($cnum == 5)) { ?> <div class='imageleft'><img src='../<?php echo $formArray['bilde']; ?>' alt='Bilde av <?php echo "$user_array[s_fname] $user_array[s_lname]"; ?>' /></div> <input name="new_image" id="new_image" size="30" type="file" /> <input class="button" name="submit" type="submit" value="Last opp bilde" /> <p><label>Fornavn: <?php echo $imp; ?></label> <input class="formlook" type="text" name="fname" size="25" maxlength="60" value="<?php echo $formArray['fname']; ?>" /> <label>Etternavn: <?php echo $imp; ?></label> <input class="formlook" type="text" name="lname" size="29" maxlength="100" value="<?php echo $formArray['lname']; ?>" /></p> <?php echo $fname_error; ?> <?php echo $lname_error; ?> <p><label>Tittel: <?php echo $imp; ?></label> <input class="formlook" type="text" name="title" size="73" maxlength="200" value="<?php echo $formArray['s_title']; ?>" /></p> <?php echo $title_error; ?> <p><label>Epostadresse: <?php echo $imp; ?></label> <input class="formlook" type="text" name="epost" size="64" maxlength="100" value="<?php echo $user_array['s_mail']; ?>" /></p> <?php } else {} ?> <p><label>Beskrivelse: <?php echo $imp; ?></label><br /> <textarea class='formlook' name='ridesc' rows='9' cols='60' value='<?php echo $formArray['desc']; ?>' ><?php echo $formArray['desc']; ?></textarea></p> <?php echo $desc_error; ?> <p><label>Oppdater:</label><input class='formlook' type='checkbox' name='update' value='1' /><span class='important'>Denne må hakes av dersom du skal få oppdatert</span></p> <p> <input class='button' type='submit' name='submit' value='Endre informasjon' /> <input class='button' type='button' name='cancel' value='Kanseller endring' onClick="window.location='<?php echo "$domain"; ?>/index.php?page=about'"> </p> </form> <?php } Code (markup):
Nope, nothing in that set of ifs is changing anything relevant. The variable $filePath is not mentioned between its assignment and its use in the query. The place to do the debugging output is right around where you are setting $filePath. e.g., $filePath = $url_pics . $imagename; echo "<p>{$filePath}</p>"; Code (markup): Also consider that your SQL query can be executed even if no file was uploaded. In that case are you still setting $filePath? The code to do so is not shown here, if it exists. If not, then that's your problem solved.
The point is - if I echo the $filePath after it is set, it shows the variable as it is supposed to be: "webgfx/filename.jpg" - but when I later push the second submit-button, all that is transferred to the database is the "webgfx/" part of the variable - ie. the $url_pics part of $filePath - it doesn't seem to transfer the . $imagepath-part, for some odd reason. And, yes, I know the query can be sent without the $filePath being set - I will fix this later, but as of now, it doesn't matter - the result is that the only part transferred to the database is the "webgfx/" part, even if it's set or not... and I can't for the life of me understand why...
It sounds like you are submitting two forms, one that uploads and one that does the INSERT. The file is not going to be set anymore for the second form post. You should be able to combine all this into one form and the field will be set.
Yeah, I figured it out, eventually I thought the variable kept the filename, but it obviously didn't - I've combined the two scripts, and it is working more or less as intended now - now I just need to find a way to preview the picture, but that is javascript-bs, so I'll head over there, I think Thanks for all the help anyway, guys.