在表格中添加上面的照片& MySQL的

时间:2013-06-14 15:21:14

标签: php html image forms

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

代码正常:

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

当然,此更改需要 Save_data.php :/

1 个答案:

答案 0 :(得分:0)

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