问题
could I get some assistance with this example code please, trying to add an image to the file directory while storing the link location in the database, but using PDO instead of the old way.
It is based on an example I found online which comes with a dbconnect.php, save.php, addstudent.php and some others not needed for this query.
<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>
This is the code that saves to the database
<?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>";
?>
Any insights on how to do this would be much appreciated, Thanks.
回答1:
First of all start by fixing the HTML
input for the image
<tr>
<td>Image</td><td>:</td>
<td><input type="file" name="image" required="required" value=""/></td>
</tr>
then expand on your PHP
code:
<?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";
}
?>
回答2:
To people who can't get Meda's answer to work, just add enctype="multipart/form-data"
to the <form>
element.
来源:https://stackoverflow.com/questions/25139911/uploading-image-location-to-db-and-image-file-to-directory-using-pdo