PDA

View Full Version : php script not uploading to mysql


urbanrays
09-29-2009, 06:56 PM
Hi

I have been messing with php/mysql the problem I am having is the info does not seem to be uploading into the sql database, I am using an image upload to a seperat file called image on my server and thats is getting through but when I try to retrive the data its just not there.

Here is my html code for the form

<form action="php/test.php" method="post" enctype="multipart/form-data" name="tester" id="tester">
Name<input name="name" type="text"><br>
Address<input name="address" type="text"><br>
email<input name="email" type="text"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
upload<input name="upload" type="file"><br>
<input name="submit" type="submit" value="submit">
</form>

Here is the php processing script

<?php
$target = "image/";
$target = $target . basename( $_FILES['upload']['name']);
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$upload=($_FILES['upload']['name']);
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("derbyartists_co") or die(mysql_error()) ;
mysql_query("INSERT INTO `test` VALUES ('$name', '$address', '$email', '$upload')");
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

And this is the script i am using to retrieve the info

<?php
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("derbyartists_co") or die(mysql_error()) ;
$data = mysql_query("SELECT * FROM test") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Echo "<img src=http://www.derbyartists.co.uk/php/image".$info['upload'] ."> <br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
Echo "<b>Email:</b> ".$info['email'] . " <br>";
Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";
}
?>



Any help would be much appreciated

bee80
09-29-2009, 09:24 PM
is the file uploading to your root /images folder? check if its uploaded it there as you havnt set a full path

$target = "image/";

for a local test environment it should be something like :

$target = "c:/users/you/desktop/work/website/image/";

also save upload to remote site too!!

urbanrays
09-30-2009, 08:17 AM
the images do go in to my image folder, the Database just says it has no enteries, I dont get no error codes.
Is there something I could be doing wrong in the SQL database

bee80
09-30-2009, 03:45 PM
i always insert field names:

mysql_query("INSERT INTO `test` VALUES ('$name', '$address', '$email', '$upload')");


should be:

mysql_query("INSERT INTO `test` (Name,Adddress,Email,Image )
VALUES ('$name', '$address', '$email', '$upload')");


obviously change the field names to what your fields are named

bee80
09-30-2009, 03:50 PM
also note that your not inserting phone data ($phone) into db


mysql_query("INSERT INTO `test` VALUES ('$name', '$address', '$email', '$upload')")

so when you call

Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";


it wont contain data anyway

edbr
10-01-2009, 01:04 AM
mysql_query("INSERT INTO `test` VALUES ('$name', '$address', '$email', '$upload')");
if you do this it will insert in the order the fields are in the table, so its better to stipulate as bee80 shows

urbanrays
10-01-2009, 07:31 PM
Thanks people, now I have aken it out of test mode, and created the php to handle the upload to my sql.

Here is the processor script
<?php
$target = "image/";
$target = $target . basename( $_FILES['upload']['name']);
$target2 = "image/";
$target2= $target2 . basename( $_FILES['upload1']['name']);
$name=$_POST['name'];
$address=$_POST['address'];
$address1=$_POST['address1'];
$city=$_POST['city'];
$postcode=$_POST['postcode'];
$telephone=$_POST['telephone'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
$upload=($_FILES['upload']['name']);
$upload1=($_FILES['upload1']['name']);
$bio=$_POST['bio'];
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("derbyartists_co") or die(mysql_error()) ;
$sql = sprintf("INSERT INTO `signup` (`name`, `address`, `address1`, `city`,`postcode`,`telephone`,`mobile`,`email`,`up load`, `upload1`,`bio`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($address),
mysql_real_escape_string($address1),
mysql_real_escape_string($city),
mysql_real_escape_string($postcode),
mysql_real_escape_string($telephone),
mysql_real_escape_string($mobile),
mysql_real_escape_string($email),
mysql_real_escape_string($upload),
mysql_real_escape_string($upload1),
mysql_real_escape_string($bio));
mysql_query($sql) or trigger_error("Query Error: " . mysql_error(), E_USER_ERROR);

if(move_uploaded_file($_FILES['upload']['tmp_name'], $target) && move_uploaded_file($_FILES['upload1']['tmp_name'], $target2))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>


Now everything is working fine the only problem is when I use php script to view the database in the browser, it wont pull up the uploaded images, any ideas please people.

Here is the script to view the database

I have left the part to communicate with the database, so please dont say that is what the fault is.

$data = mysql_query("SELECT * FROM signup") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Echo "<img src=www.derbyartists.co.uk/php/image/".$info['upload']."> <br>";
Echo "<img src=www.derbyartists.co.uk/php/image/".$info['upload1']."> <br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
Echo "<b>Address:</b> ".$info['address'] . "<br> ";
Echo "<b>Address1:</b> ".$info['address1'] . "<br> ";
Echo "<b>City:</b> ".$info['city'] . "<br> ";
Echo "<b>Postcode:</b> ".$info['postcode'] . "<br> ";
Echo "<b>Telephone:</b> ".$info['telephone'] . "<br> ";
Echo "<b>Mobile:</b> ".$info['mobile'] . "<br> ";
Echo "<b>Email:</b> ".$info['email'] . " <br>";
Echo "<b>Bio:</b> ".$info['bio'] . " <hr>";
}
?>

Like I say it is just showing the usual no image red cross.

Thanks in advance

PS If I look at the source code when viewing the database the upload image names appear there but no image in the browser window

bee80
10-01-2009, 09:59 PM
does the rest of the info display?? name address etc?
are you using dreamweaver? if so make sure you put local files to remote , i had this problem and realised when i uploaded a file it uploaded to local server and not the remote server

urbanrays
10-02-2009, 12:41 PM
Thanks I have sorted it, yes it was showing all the other info just a problem with
Echo "<img src=www.derbyartists.co.uk/php/image/".$info['upload']."> <br>";
Echo "<img src=www.derbyartists.co.uk/php/image/".$info['upload1']."> <br>";

echo '<img src="http://www.derbyartists.co.uk/php/image/'.$info['upload'].'" /> <br />';
echo '<img src="http://www.derbyartists.co.uk/php/image/'.$info['upload1'].'" /> <br />';