尝试发送UPDATE查询时,无法将类mysqli_result的对象转换为字符串

时间:2016-06-16 21:16:40

标签: php mysql

我试图向我的表发送一个UPDATE查询,为用户设置一个值并且我正在

  

捕获致命错误:第19行的E:\ web \ nginx \ html \ b1is \ profile.php中无法将类mysqli_result的对象转换为字符串

除非可以进行查询,否则我不想返回任何值。

代码:

<?php
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
session_start();
$sqlcon = include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: login.php");
}
$res=mysqli_query($sqlcon,"SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);

if(isset($_POST['updprof']))
{
    $steamid = mysqli_real_escape_string($sqlcon,$_POST['steamid']);
    $discordid = mysqli_real_escape_string($sqlcon,$_POST['discordid']);
    if(isset($steamid))
    {
        if(!mysqli_query($sqlcon,"UPDATE users SET steamid = '$steamid' WHERE user_id = $res"))
        {
            ?> <script>alert("Update couldn't be made")</script> <?php
        }
    }

    if(isset($discordid))
    {
        if(!mysqli_query($sqlcon,"UPDATE users SET discordid = '$discordid' WHERE user_id = $res"))
        {
            ?> <script>alert("Update couldn't be made")</script> <?php
        }
    }
}
?>

2 个答案:

答案 0 :(得分:0)

$res=mysqli_query($sqlcon,"SELECT * FROM users WHERE user_id=".$_SESSION['user']);
^^^---mysqli object

    if(!mysqli_query($sqlcon,"UPDATE users SET steamid = '$steamid' WHERE user_id = $res"))
                                                                  string context----^^^

您不能将mysql结果对象/句柄直接传递给另一个查询。您必须先从结果中获取一个值,然后使用该值。

答案 1 :(得分:0)

$res上的...WHERE user_id = $res参数对应mysqli_query()而不是mysqli_fetch_array()。

第19行将WHERE user_id = $res更改为WHERE user_id = ".$userRow["user_id"]

查看:

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
session_start();
$sqlcon = include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: login.php");
}
$res=mysqli_query($sqlcon,"SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);

if(isset($_POST['updprof']))
{   
    $steamid = mysqli_real_escape_string($sqlcon,$_POST['steamid']);
    $discordid = mysqli_real_escape_string($sqlcon,$_POST['discordid']);
    if(isset($steamid))
    {
        if(!mysqli_query($sqlcon,"UPDATE users SET steamid = '$steamid' WHERE user_id = ".$userRow["user_id"]))
        {
            ?> <script>alert("Update couldn't be made")</script> <?php
        }
    }

    if(isset($discordid))
    {
        if(!mysqli_query($sqlcon,"UPDATE users SET discordid = '$discordid' WHERE user_id = $res"))
        {
            ?> <script>alert("Update couldn't be made")</script> <?php
        }
    }
}