简体   繁体   English

如何使用 PHP 将 blob 图像更新到 mysql

[英]How to update blob images into mysql using PHP

I'm experiencing some difficulties when it comes to updating images/blog files on my database using PHP.在使用 PHP 更新我的数据库上的图像/博客文件时,我遇到了一些困难。 I can update text inputs successfully but I'm having troubles when it's images.我可以成功更新文本输入,但是当它是图像时我遇到了麻烦。

Here's my form for uploading:这是我的上传表格

<?php
$userid = $_SESSION['admin'];
include 'connect.php';
$users= "SELECT * FROM `staff` WHERE `username`='$userid'";
$data= mysqli_query($conn,$users) or die("Connection Failed!");
while($row = mysqli_fetch_array($data, MYSQLI_ASSOC)){
?>
<form method="POST" enctype='multipart/form-data' action="staffs/updatestaff.php">
<label><h4>Image:</h4></label>    
<input type="file" name="cons_image" required>
<br />
<label><h4>First Name:</h4></label>
<input class="form-control" name="fname" type="text" placeholder="<?php echo $row['username']?>" required="required"/>
<small>This is also your username.</small>
<br />
<label><h4>Last Name:</h4></label>
<input class="form-control" name="lname" type="text" placeholder="<?php echo $row['lname']?>" required="required"/>
 <br />
 <label><h4>Password:</h4></label>
  <input id="password_show" type="password" placeholder="Password" name="pwd" class="form-control" required/>
  <a id="password_show_button" type="btn btn-default" class="input-group-addon"><i class="far fa-eye-slash"></i></a>
  <script>                         
    $(document).ready(function(){
    $("#password_show_button").mouseup(function(){
     $("#password_show").attr("type", "password");
     });
     $("#password_show_button").mousedown(function(){
     $("#password_show").attr("type", "text");
     });
     });                        
      </script>
      <input type="hidden" name="a_id" value="<?php echo $row['a_id'];?>" />        </div>
       <div class="modal-footer">
       <button type="submit" class="btn btn-success btn-md" value="submit">Update</button> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
        <?php
         }
         ?>
</form>

and here's my action code:这是我的操作代码:

<?php           
include 'connect.php';
define ("MAX_SIZE","500"); 
define ("WIDTH","150");
define ("HEIGHT","120");
function make_thumb($img_name,$filename,$new_w,$new_h)
{
$ext=getExtension($img_name);
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);        
if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);     
if(!strcmp("gif",$ext))
$src_img=imagecreatefromgif($img_name); 
//gets the dimmensions of the image
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);   
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else    {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);     
if(!strcmp("png",$ext))
imagepng($dst_img,$filename); 
else
imagejpeg($dst_img,$filename);          
if (!strcmp("gif",$ext))
imagegif($dst_img,$filename); 
//destroys source and destination images. 
imagedestroy($dst_img); 
imagedestroy($src_img); 
 }
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['cons_image']['name'];
if ($image) 
{
$filename = stripslashes($_FILES['cons_image']['name']);                
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg")  && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
{
echo '<h1>Unknown extension!  Please use .gif, .jpg or .png files only.</h1>';
$errors=1;
}
else
{
$size=getimagesize($_FILES['cons_image']['tmp_name']);
$sizekb=filesize($_FILES['cons_image']['tmp_name']);    
if ($sizekb > MAX_SIZE*4096)
{
echo '<h1>You have exceeded the 4MB size limit!</h1>';
$errors=1;
}               
$rand= rand(0, 1000);
$image_name=$rand.'.'.$extension;
$consname="Styles/img/userimgs/thumb".$image_name; 
$consname2="Styles/img/userimgs/thumb".$image_name;
$copied = copy($_FILES['cons_image']['tmp_name'], $consname);
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['pwd'];
$a_id = $_POST['a_id'];
$image1 = $_POST['cons_image'];
$sql = "UPDATE `staff` SET `image`='$image1', `fname`='$fname', `lname`='$lname', `username`='$fname', `password`='$password'  WHERE `a_id`='$a_id'";
$data= mysqli_query($link,$sql);
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}
else
{
$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
}
}   
}
}           
if(isset($_POST['Submit']) && !$errors) 
{         
echo "Record Updated":
}
?>

It's not even showing an error, it's only showing a blank page.它甚至没有显示错误,它只显示一个空白页。 Is there a more efficient way to do this?有没有更有效的方法来做到这一点? I'm open for suggestions.我愿意接受建议。 Thank you谢谢

Found the problem on my code.在我的代码中发现了问题。

Added name="Submit" inside the <button> </button> in the form .表单<button> </button>中添加了name="Submit"

Updated the $consname="Styles/img/userimgs/thumb".$image_name;更新了$consname="Styles/img/userimgs/thumb".$image_name; and $consname2="Styles/img/userimgs/thumb".$image_name;$consname2="Styles/img/userimgs/thumb".$image_name; to $consname="../Styles/img/userimgs/thumb".$image_name;$consname="../Styles/img/userimgs/thumb".$image_name; and $consname2="../Styles/img/userimgs".$image_name;$consname2="../Styles/img/userimgs".$image_name; because they are linked to the wrong place.因为它们链接到错误的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM