Uploading blob files/images into Mysql

前端 未结 2 638
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 21:55

I had this php code that take values from an html form (name, file, photo, address,.....), and try to insert or update them in sql database.



        
相关标签:
2条回答
  • 2021-01-24 22:14

    Try to initialise the variable. I have added the entire code. You don't need to get $_POST['file'].

    <?php 
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="root"; // Mysql password
    $db_name="clinic"; // Database name
    $tbl_name="patients"; // Table name
    session_start();
    $con=mysqli_connect("localhost","root","root","clinic");
    
    $id=$_REQUEST['id'];
    
     $name = '';
     $remarcs = '';
     $address = '';
     $test_res = '';
     $date = '';
     $phone = '';
      $new_path = '';
    
    if (isset ($_POST['name'])) {
     $name = $_POST['name'];
     }
    if (isset ($_POST['remarcs'])) {
     $remarcs = $_POST['remarcs'];
     }
    if (isset ($_POST['test_res'])) {
    $test_res = $_POST['test_res'];
    }
    if (isset ($_POST['address'])) {
      $address = $_POST['address'];
    }
    
    if (isset ($_POST['date'])) {
      $date = $_POST['date'];
    }
    
    if (isset ($_POST['phone_num'])) {
     $phone = $_POST['phone_num'];
    }
    
    if(isset($_FILES['file'])){ //Check file is uploaded or not
    
      $path = "../uploads/".$_FILES['file']['name'];
      if(move_uploaded_file($_FILES["file"]["tmp_name"], $path)){
      $new_path = $path;
      $sql=" update patients set 
      name = '$name',
      echo_photo = 'NULL',
      echo_file = '$new_path',
      remarcs = '$remarcs',
       test_res = '$test_res',
      date = '$date',
      address = '$address',
      phone_num = '$phone'
      WHERE id = ".$id;
    
      $result=mysqli_query($con,$sql) or die('Unable to execute query. '.   mysqli_error($con));
    
    if($result){
     echo $name."<p>\n</p>";
     echo $remarcs."<p>\n</p>";
     echo $test_res."<p>\n</p>";
     echo $address."<p>\n</p>";
     echo $phone."<p>\n</p>";
     }
      echo "Uploaded";
    } else {
      echo "Not uploaded";
     }
    }
    mysqli_close($con);
    
    ?> 
    
    0 讨论(0)
  • 2021-01-24 22:14

    This is the final working code:

    if (isset ($_POST['name'])) { 
    $name = $_POST['name']; 
    } 
    if (isset ($_POST['remarcs'])) { 
    $remarcs = $_POST['remarcs']; 
    } 
    if (isset ($_POST['test_res'])) { 
    $test_res = $_POST['test_res']; 
    } 
    if (isset ($_POST['address'])) { 
    $address = $_POST['address']; 
    } 
    
    if (isset ($_POST['date'])) { 
    $date = $_POST['date']; 
    } 
    
    if (isset ($_POST['phone_num'])) { 
    $phone = $_POST['phone_num']; 
    } 
    
    if(isset($_FILES['file'])){ //Check file is uploaded or not 
    
    $path = "../uploads/".$_FILES['file']['name'];
    //$path2 = "../uploads/".$_FILES['echo_photo']['name']; 
    
    if(move_uploaded_file($_FILES["file"]["tmp_name"], $path)){ 
    $new_path = $path; 
    $sql="UPDATE $tbl_name SET  
    name = '$name', 
    echo_files = '$new_path', 
    remarcs = '$remarcs',
    test_res = '$test_res', 
    date = '$date', 
    address = '$address', 
    phone_num = '$phone' 
    WHERE id = '$id'"; 
    
    $result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con)); 
    
    0 讨论(0)
提交回复
热议问题