繁体   English   中英

如何使用构造函数在MySQL数据库中插入图像URL

[英]How to insert image url in mysql database using constructor function

我是php的初学者,我希望使用构造函数在数据库中发送图像url。 在我使用普通的php之前,它工作正常。

以下文件非常有用:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

    $image = $_POST['image'];
            $name = $_POST['name'];

    require_once('dbConnect.php');

    $sql ="SELECT id FROM uploadimagestoserver ORDER BY id ASC";

    $res = mysqli_query($con,$sql);

    $id = 0;

    while($row = mysqli_fetch_array($res)){
            $id = $row['id'];
    }

    $path = "uploads/$id.png";

    $actualpath = "https://localhost/GoodPriceApi/uploadImage/$path";

    $sql = "INSERT INTO uploadimagestoserver(image,name) VALUES ('$actualpath','$name')";

    if(mysqli_query($con,$sql)){
        file_put_contents($path,base64_decode($image));
        echo "Successfully Uploaded";
    }

    mysqli_close($con);
}else{
    echo "Error";
}
?>

但是,当我尝试在以下函数中添加脚本时,出现错误:警告:mysqli_fetch_array()期望参数1为mysqli_result

请帮助我将提及的代码集成到此函数createProfile中 ,该文件是我命名为DbHandler.php的文件,另一个是我命名为upload.php的文件

DbHandler.php

class DbHandler {

private $conn;

function __construct() {
    require_once dirname(__FILE__) . '/DbConnect.php';
    // opening db connection
    $db = new DbConnect();
    $this->conn = $db->connect();
}

public function createProfile($image, $name) {

 $response = array();

 $stmt =$this->conn->prepare ("SELECT id FROM uploadimagestoserver ORDER BY     id ASC") ;

   $id = 0;

    while($row = mysqli_fetch_array($response)){
            $id = $row['id'];
    }

    $path = "uploads/$id.png";
    $actualpath = "https://localhost/GoodPriceApi/uploadImage/$path";


        // insert query
    $stmt = $this->conn->prepare("INSERT INTO uploadimagestoserver(image,name) values(?, ?)");
     $stmt->bind_param("is", $actualpath, $name);

        $result = $stmt->execute();

        $new_user_id = $stmt->insert_id;

        $stmt->close();

        // Check for successful insertion
        if ($result) {

        file_put_contents($path,base64_decode($image));


            // User successfully inserted
            return USER_CREATED_SUCCESSFULLY;
        } else {
            // Failed to create user
            return USER_CREATE_FAILED;
        }


    return $response;



    }
}

upload.php

    <?php

include './DbHandler.php';
$db = new DbHandler();


$response = array();

if (isset($_POST['name']) && $_POST['image'] != '') {


$name = $_POST['name'];
 $image = $_POST['image'];




$res = $db->createProfile($name, $image);

if ($res == USER_CREATED_SUCCESSFULLY) {


    $response["error"] = false;
    $response["message"] = "access granted";
} else if ($res == USER_CREATE_FAILED) {
    $response["error"] = true;
    $response["message"] = "Sorry! Error occurred in registration.";
} 
   } else {
    $response["error"] = true;
     $response["message"] = "Sorry! phone number is not valid or missing.";
   }

    echo json_encode($response);

   ?>
 $response = array();

$stmt =$this->conn->prepare ("SELECT id FROM uploadimagestoserver ORDER BY       id ASC") ;

 $id = 0;

while($row = mysqli_fetch_array($response)){
        $id = $row['id'];
}

所以说它是一个数组,不要执行$stmt

暂无
暂无

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

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