【发布时间】:2026-01-25 11:45:01
【问题描述】:
我是 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;
}
}
上传.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);
?>
【问题讨论】:
-
您正在混合 OOP 和程序风格。将
mysqli_fetch_array($response)更改为$stmt->fetch_array($response)。在你这样做之前,你需要实际执行查询。你只是在准备它。由于查询中没有占位符,您可以直接将prepare更改为query。 -
哦,把
function createProfile($image, $name)换成function createProfile($name, $image) -
谢谢让我这样做,如果它有效,我会告诉你
-
对不起,先生,我的问题。我将 mysqli_fetch_array($response) 更改为 $stmt->fetch_array($response) 并将函数 createProfile($image, $name) 更改为函数 createProfile($name, $image) 现在我收到此错误:致命错误:调用未定义方法 mysqli_stmt::fetch_array() 在 C:
-
您是否将
prepare更改为query(循环前的行)?
标签: php oop constructor