我可以在这个示例代码中获得一些帮助,尝试将图像添加到文件目录,同时将链接位置存储在数据库中,但使用PDO而不是旧方法。
它基于我在网上找到的一个示例,它带有dbconnect.php,save.php,addstudent.php以及此查询不需要的其他一些示例。
<form method="post" name="frmStudent" action="save.php">
<input type="hidden" name="pid" value="<?php echo $ppid; ?>"/>
<table>
<tr><td>First Name</td><td>:</td><td><input type="text" name="fname" required="required" value="<?php echo $pfname; ?>"/></td></tr>
<tr><td>Last Name</td><td>:</td><td><input type="text" name="lname" required="required" value="<?php echo $plname; ?>"/></td></tr>
<tr><td>Contact No.</td><td>:</td><td><input type="tel" name="contact" required="required" value="<?php echo $pcontact; ?>"/></td></tr>
<tr><td>Email</td><td>:</td><td><input type="email" name="email" required="required" value="<?php echo $pemail; ?>"/></td></tr>
<tr><td>Image</td><td>:</td><td><input type="file" name="email" required="required" value="<?php echo $pimg_url; ?>"/></td></tr>
<tr><td></td><td></td><td><input type="submit" class="myButton" value="Save"/></td></tr>
</table>
</form>
这是保存到数据库的代码
<?php
error_reporting(0);
include ("dbconnection.php");
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$contact=$_POST['contact'];
$email=$_POST['email'];
$img_url=$_POST['img_url'];
$id=$_POST['pid'];
if($id==null){
$sql="INSERT INTO student(fname,lname,contact,email,img_url)values(:fname,:lname,:contact,:email,:img_url)";
$qry=$db->prepare($sql);
$qry->execute(array(':fname'=>$fname,':lname'=>$lname,':contact'=>$contact,':email'=>$email,':img_url'=>$img_url));
}else{
$sql="UPDATE student SET fname=?, lname=?, contact=?, email=?, img_url=? where id=?";
$qry=$db->prepare($sql);
$qry->execute(array($fname,$lname,$contact,$email,$img_url,$id));
}
echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>";
echo "<script language='javascript' type='text/javascript'>window.open('index.php','_self')</script>";
?>
非常感谢任何关于如何做到这一点的见解,谢谢。
答案 0 :(得分:5)
对于无法获得Meda工作答案的人,只需将enctype="multipart/form-data"
添加到<form>
元素即可。
答案 1 :(得分:2)
首先,修复图片的HTML
输入
<tr>
<td>Image</td><td>:</td>
<td><input type="file" name="image" required="required" value=""/></td>
</tr>
然后展开您的PHP
代码:
<?php
error_reporting(0);
include ("dbconnection.php");
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$folder = "upload/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo "succesful upload, we have an image!";
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$contact=$_POST['contact'];
$email=$_POST['email'];
$img_url= $full_path;
$id=$_POST['pid'];
if($id==null){
$sql="INSERT INTO student(fname,lname,contact,email,img_url)values(:fname,:lname,:contact,:email,:img_url)";
$qry=$db->prepare($sql);
$success = $qry->execute(array(':fname'=>$fname,':lname'=>$lname,':contact'=>$contact,':email'=>$email,':img_url'=>$full_path));
}else{
$sql="UPDATE student SET fname=?, lname=?, contact=?, email=?, img_url=? where id=?";
$qry=$db->prepare($sql);
$success = $qry->execute(array($fname,$lname,$contact,$email,$full_path,$id));
}
if($success){
echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>";
echo "<script language='javascript' type='text/javascript'>window.open('index.php','_self')</script>";
}else{
echo 'db transaction failed';
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
?>