php – 使用PDO将图像位置上传到DB和图像文件到目录?
作者:互联网
我可以获得这个示例代码的一些帮助,尝试将图像添加到文件目录,同时将链接位置存储在数据库中,但使用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>";
?>
任何有关如何做到这一点的见解将非常感谢,谢谢.
解决方法:
首先,修复图像的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";
}
?>
标签:image-uploading,php,mysql,pdo 来源: https://codeday.me/bug/20190728/1561684.html