Php 在表格&;中添加上面的照片;mysql

Php 在表格&;中添加上面的照片;mysql,php,html,image,forms,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

我尝试编写代码来上传第二张图片,结果保存在数据库中,但当上传文件夹中的图片时,图片只取一张名为的图片,例如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>

名称:
电子邮件:
电话:
照片:
照片2:
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."; 
 } 
 ?>

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

您面临着mysql注入的风险。了解更多信息不要直接插入数据!这是一个重大的安全问题。另外,不要使用
mysql\u connect
-请参阅:您所说的数据存储方式,我知道,但我正在寻找将多个图像记录到一个表单中的方法/您没有任何代码可以将
2nd
图像移动到
tmp
文件夹,然后进行复制。但是你真的需要用你的代码解决安全问题。我试着去做,我得到了,怎么做;。。。也许你很容易告诉我为什么我要做每件事…谢谢-这就是为什么你不应该使用旧的蹩脚易受攻击的代码。看这一点
//如果(移动上传的文件($\u FILES['photo']['tmp\u name'],$target))
你的代码中没有任何东西可以移动
第二个
图像。如果(move_上传的文件($tmp_name,$target./'.$photo)和($tmp_name,$target./'.$photo2))我把它放错了,但我们必须把它放进去;。。
<?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 />");
    }
?>