You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP代码报错Result could not be converted to string,寻求排查解决

解决 "Result could not be converted to string" 错误

这个报错的核心原因很清晰:你直接把mysqli_query()返回的结果集对象当成字符串/数值塞进了SQL语句里,但对象本身是无法直接转换为字符串的,这才触发了这个错误。

问题出在哪?

看你代码里这两行:

$hotelid = mysqli_query($con, "select HoteID from hotel where hotelname='$hotel'");
$usernameid = mysqli_query($con, "select UserID from user where UserID='$username'");

mysqli_query()执行成功后返回的是一个mysqli_result对象,不是你要的HoteID或UserID具体值。你得从这个结果集里提取出实际的ID数据,才能用在INSERT语句里。

另外还有个逻辑小错误:你的$username是从SESSION取的用户名,但查询用户ID时用的条件是UserID='$username'——这大概率是写错了,应该是根据用户名(比如字段叫username)来查UserID,而不是用UserID等于用户名。

修正步骤

  1. 从结果集中提取实际ID值
    执行查询后,需要用mysqli_fetch_assoc()或mysqli_fetch_row()从结果集里取出数据,还要加个判断避免找不到数据的情况:

    // 获取酒店ID
    $hotelResult = mysqli_query($con, "select HoteID from hotel where hotelname='$hotel'");
    if ($hotelResult && mysqli_num_rows($hotelResult) > 0) {
        $hotelRow = mysqli_fetch_assoc($hotelResult);
        $hotelid = $hotelRow['HoteID'];
    } else {
        echo "指定的酒店不存在";
        exit;
    }
    
    // 获取用户ID(修正查询条件为用户名)
    $userResult = mysqli_query($con, "select UserID from user where username='$username'");
    if ($userResult && mysqli_num_rows($userResult) > 0) {
        $userRow = mysqli_fetch_assoc($userResult);
        $usernameid = $userRow['UserID'];
    } else {
        echo "当前用户不存在";
        exit;
    }
    
  2. 用预处理语句避免SQL注入(强烈推荐)
    你原代码直接把用户输入拼进SQL,存在严重的SQL注入风险。改用预处理语句不仅能解决安全问题,还能避免类型转换的麻烦:

    <?php
    if(isset($_POST["submit"])){ 
        $hotel = $_POST["hotel"]; 
        $username = $_SESSION["username"]; 
        $score = $_POST["score"]; 
        $explain= $_POST["explain"]; 
    
        // 获取酒店ID(预处理版本)
        $hotelStmt = $con->prepare("select HoteID from hotel where hotelname=?");
        $hotelStmt->bind_param("s", $hotel);
        $hotelStmt->execute();
        $hotelResult = $hotelStmt->get_result();
        if ($hotelResult->num_rows === 0) {
            echo "指定酒店不存在";
            exit;
        }
        $hotelRow = $hotelResult->fetch_assoc();
        $hotelid = $hotelRow['HoteID'];
        $hotelStmt->close();
    
        // 获取用户ID(预处理版本,修正查询条件)
        $userStmt = $con->prepare("select UserID from user where username=?");
        $userStmt->bind_param("s", $username);
        $userStmt->execute();
        $userResult = $userStmt->get_result();
        if ($userResult->num_rows === 0) {
            echo "用户不存在";
            exit;
        }
        $userRow = $userResult->fetch_assoc();
        $usernameid = $userRow['UserID'];
        $userStmt->close();
    
        // 插入评论(预处理版本,注意:原代码表名写的是user,这肯定错了,改成评论表比如review)
        $insertStmt = $con->prepare("INSERT INTO review (description, score, userID, HoteID) VALUES (?, ?, ?, ?)");
        $insertStmt->bind_param("siii", $explain, $score, $usernameid, $hotelid);
        if($insertStmt->execute()){ 
            header('Location: review.php'); 
            exit; // 跳转后必须exit,防止后续代码执行
        } else { 
            echo "错误信息: " . $insertStmt->error; 
        } 
        $insertStmt->close();
    } 
    ?>
    

    特别提醒:原代码里INSERT的目标表是user,这明显不对——你是要插入评论,应该是类似review的表,记得修正表名!

为什么直接类型转换没用?

因为你转换的是结果集对象本身,而不是对象里存储的实际ID值。对象不管怎么转都没法变成你要的字符串/数值,必须先提取出值再处理。

内容的提问来源于stack exchange,提问作者user8764736

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:42:31