Web Hosting Talk







View Full Version : PHP upload file and insert into table field


Sydcon
04-10-2007, 04:06 PM
Hi everyone. Thanks in advance for helping. I'm having a problem with some of my code. I have a form, that uploads a file to my server and inserts the file name into a table. The users can go in to an edit form and modify other info and re-upload a new file. The problem is when the user does not upload a new file, the value in the table(for that file) gets blanked out. I tried solving this by passing a hidden field with the old image name, but that didn't work.

So basically I want this:

if no file to upload...don't blank it out.

if there is a file...upload it and change the record in the table.

Here is my processing code:

if($_POST['img1'] == "") {
$_POST['img1'] = $_POST['oldimg1'];
$image1 = $_POST['oldimg1'];
}
if($_POST['img2'] == "") {
$_POST['img2'] = $_POST['oldimg2'];
$image2 = $_POST['oldimg2'];
}
if($_POST['img3'] == "") {
$_POST['img3'] = $_POST['oldimg3'];
$image3 = $_POST['oldimg3'];
}
if($_POST['img4'] == "") {
$_POST['img4'] = $_POST['oldimg4'];
$image4 = $_POST['oldimg4'];
}

$target = "../ads/";
$target = $target . basename( $_FILES['img1']['name']) ;
$image1 = basename( $_FILES['img1']['name']) ;
if(move_uploaded_file($_FILES['img1']['tmp_name'], $target));

$target2 = "../ads/";
$target2 = $target2 . basename( $_FILES['img2']['name']) ;
$image2 = basename( $_FILES['img2']['name']) ;
if(move_uploaded_file($_FILES['img2']['tmp_name'], $target));

$target3 = "../ads/";
$target3 = $target3 . basename( $_FILES['img3']['name']) ;
$image3 = basename( $_FILES['img3']['name']) ;
if(move_uploaded_file($_FILES['img3']['tmp_name'], $target));

$target4 = "../ads/";
$target4 = $target4 . basename( $_FILES['img4']['name']) ;
$image4 = basename( $_FILES['img4']['name']) ;
if(move_uploaded_file($_FILES['img4']['tmp_name'], $target));


$updateSQL = sprintf("UPDATE ports SET location=%s, address=%s, city=%s, `state`=%s, zip=%s, lake=%s, website=%s, lat=%s, lon=%s, googlelat=%s, googlelon=%s, portstatus=%s, img1=%s, img2=%s, img3=%s, img4=%s, url1=%s, url2=%s, url3=%s, url4=%s WHERE id=%s",
GetSQLValueString($_POST['location'], "text"),
GetSQLValueString($_POST['address'], "text"),
GetSQLValueString($_POST['city'], "text"),
GetSQLValueString($_POST['state'], "text"),
GetSQLValueString($_POST['zip'], "text"),
GetSQLValueString($_POST['lake'], "text"),
GetSQLValueString($_POST['website'], "text"),
GetSQLValueString($_POST['lat'], "text"),
GetSQLValueString($_POST['lon'], "text"),
GetSQLValueString($_POST['googlelat'], "text"),
GetSQLValueString($_POST['googlelon'], "text"),
GetSQLValueString($_POST['portstatus'], "text"),
GetSQLValueString($image1, "text"),
GetSQLValueString($image2, "text"),
GetSQLValueString($image3, "text"),
GetSQLValueString($image4, "text"),
GetSQLValueString($_POST['url1'], "text"),
GetSQLValueString($_POST['url2'], "text"),
GetSQLValueString($_POST['url3'], "text"),
GetSQLValueString($_POST['url4'], "text"),
GetSQLValueString($_POST['id'], "int"));

mysql_select_db($database_air2access, $air2access);
$Result1 = mysql_query($updateSQL, $air2access) or die(mysql_error());

Ks Jeppe
04-10-2007, 04:46 PM
I would most likely change to something like this:
<?php
$basetarget = '../ads/';

for ($i = 1; $i <= 4; $i++) {
if(!is_uploaded_file($_FILES['img'+$i]['tmp_name'])
$image[$i] = $_POST['oldimg'+$i];
else {
$image[$i] = basename($_FILES['img'+$i]['name']);
move_uploaded_file($_FILES['img'+$i]['tmp_name'], $basetarget.basename($_FILES['img'+$i]['name']));
}
}

$updateSQL = sprintf("UPDATE ports SET location=%s, address=%s, city=%s, `state`=%s, zip=%s, lake=%s, website=%s, lat=%s, lon=%s, googlelat=%s, googlelon=%s, portstatus=%s, img1=%s, img2=%s, img3=%s, img4=%s, url1=%s, url2=%s, url3=%s, url4=%s WHERE id=%s",
GetSQLValueString($_POST['location'], "text"),
GetSQLValueString($_POST['address'], "text"),
GetSQLValueString($_POST['city'], "text"),
GetSQLValueString($_POST['state'], "text"),
GetSQLValueString($_POST['zip'], "text"),
GetSQLValueString($_POST['lake'], "text"),
GetSQLValueString($_POST['website'], "text"),
GetSQLValueString($_POST['lat'], "text"),
GetSQLValueString($_POST['lon'], "text"),
GetSQLValueString($_POST['googlelat'], "text"),
GetSQLValueString($_POST['googlelon'], "text"),
GetSQLValueString($_POST['portstatus'], "text"),
GetSQLValueString($image[1], "text"),
GetSQLValueString($image[2], "text"),
GetSQLValueString($image[3], "text"),
GetSQLValueString($image[4], "text"),
GetSQLValueString($_POST['url1'], "text"),
GetSQLValueString($_POST['url2'], "text"),
GetSQLValueString($_POST['url3'], "text"),
GetSQLValueString($_POST['url4'], "text"),
GetSQLValueString($_POST['id'], "int"));

mysql_select_db($database_air2access, $air2access);
$Result1 = mysql_query($updateSQL, $air2access) or die(mysql_error());
?>
A quick view of what i did:

- You had if($_POST['img1'] == ""), but files inside a file input don't get sent as $_post, but rather $_files, so i changed it to see if there was a file uploaded in the fields via the php function is_uploaded_file, which is the "correct" way of doing it.
- I made a loop to to through 4 cycles, which makes it a lot easier to do and makes you get a better overview of your code, in the loop I check for each time it runs whether or not the files field was empty when submitted (via. the is_uploaded_file function), if it was, then we just set the image name to the oldimg values, else, we se them to the new values as well as upload the file
- Instead of having $image1, $image2, $image3 and $image4, i made it as an array instead, and changed to the correct values in the sql query as well...

Hope that helps :)

Sydcon
04-10-2007, 05:15 PM
Thanks for the help. Makes total sense. I got an error when i tried it. This is it:

Parse error: parse error, unexpected T_VARIABLE in c:\Inetpub\wwwroot\air2access\admin\edit_location.php on line 39


line 39 is in the loop.

Ks Jeppe
04-10-2007, 05:20 PM
Sorry, forgot to close the brackets in the if... just make it if(!is_uploaded_file($_FILES['img'+$i]['tmp_name'])) instead :) (note the two ending brackets instead of just one :P)