0

我尝试编写代码来上传第二张图片,结果保存在数据库中,但是当上传文件夹中的图片时,图片只需要一个名称,例如。1.jpg2

代码正常:

上传.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>

保存数据.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."; 
 } 
 ?>

查看.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 />");
    }
?>

当然,更改将要Save_data.php:/

4

1 回答 1

0

在将图像从 tmp 移动到目标时,您只有“照片”的代码,您还需要为“照片2”实现相同的代码。

于 2013-06-14T15:38:58.147 回答