0

I try writing code to upload and second image and result is save in the database but when upload the pictures in folder images takes only one with name eg. 1.jpg2

Code Normal:

Upload.php

 <form enctype="multipart/form-data" action="save_data.php" method="POST"> 
 Name: <input type="text" name="name"><br> 
 E-mail: <input type="text" name = "email"><br> 
 Phone: <input type="text" name = "phone"><br> 
 Photo: <input type="file" name="photo"><br> 
 Photo2: <input type="file" name="photo2"><br> 
 <input type="submit" value="Add"> 
 </form>

Save_data.php

<?php 

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 
 $pic2=($_FILES['photo2']['name']);

 // Connects to your Database 
 mysql_connect("localhost", "root", "root") or die(mysql_error()) ; 
 mysql_select_db("test_db") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic', '$pic2')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?>

View.php

<?php
 // Connects to your Database 
 mysql_connect("localhost", "root", "root") or die(mysql_error()) ; 
 mysql_select_db("test_db") or die(mysql_error()) ; 
$select_all="SELECT * FROM employees";
$select_all_query=mysql_query($select_all) or mysql_error();
$select_all_count=mysql_num_rows($select_all_query) or mysql_error();
$i=0;
while ($select_all_count > $i ) {
    $out_name=mysql_result($select_all_query,$i,"name");
    $out_email=mysql_result($select_all_query,$i,"email");
    $out_phone=mysql_result($select_all_query,$i,"phone");
    $out_photo=mysql_result($select_all_query,$i,"photo");
    $out_photo2=mysql_result($select_all_query,$i,"photo2");
    $i++;
    echo ("$out_name<br />");
    echo ("$out_email<br />");
    echo ("$out_phone<br />");
    echo ("<img src='http://127.0.0.1/upload/test/images/$out_photo' width='10%'><br />");
    echo ("<img src='http://127.0.0.1/upload/test/images/$out_photo2' width='10%'><br /><br />");
    }
?>

Certainly the change will want to Save_data.php :/

Paris
  • 33
  • 6
  • You are at risk of `mysql injections`. Learn more http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – Fabio Jun 14 '13 at 15:22
  • Don't insert `$_POST` data directly! It's a major security issue http://en.wikipedia.org/wiki/SQL_injection. Also, don't use `mysql_connect` - http://php.net/manual/en/function.mysql-connect.php See : http://xkcd.com/327/ and http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work – Nick R Jun 14 '13 at 15:26
  • you say for how data is stored know, but I'm looking something to record multi images into a form.. :/ – Paris Jun 14 '13 at 15:29
  • You don't have any code to move the `2nd` image to a `tmp` folder and then copy it. But you really need to fix the security issues with your code. – Nick R Jun 14 '13 at 15:33
  • I try to do and I get,how to done;... – Paris Jun 14 '13 at 15:38

1 Answers1

0

In moving the images from tmp to target, you have code only for 'photo', you need to implement the same code for 'photo2' also.

thanedar
  • 51
  • 6
  • maybe you is easy to show me how to come out why i does everything...thanks – Paris Jun 14 '13 at 15:41
  • http://webcache.googleusercontent.com/search?q=cache:pJq19W0o0u0J:www.daniweb.com/web-development/php/threads/278836/image-and-form-field-upload-problem+&cd=4&hl=en&ct=clnk&gl=uk&client=firefox-a and http://forums.phpfreaks.com/topic/269945-why-functionmove-uploaded-file-failed-to-open-stream-no-such-file-or-directory/ - This is why you shouldn't use old crappy vulnerable code. – Nick R Jun 14 '13 at 15:45
  • Look at this bit `//Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))` You have nothing in your code to move the `2nd` image. – Nick R Jun 14 '13 at 16:13
  • if(move_uploaded_file($tmp_name, $target.'/'.$photo) & ($tmp_name, $target.'/'.$photo2)) I've put so wrong but we have to put it;.. – Paris Jun 14 '13 at 16:18