【问题标题】:Catchable fatal error: Object of class mysqli_result could not be converted to string on line 12可捕获的致命错误:无法在第 12 行将类 mysqli_result 的对象转换为字符串
【发布时间】:2017-04-20 04:02:39
【问题描述】:

我在第 12 行收到以下 php 代码的错误。我正在尝试将数据插入表中,如果成功,则在警报后重定向到另一个页面。

<?php
session_start();
include 'dbconn.php';
$name = $_POST["name"];
$hof = $_POST["hof"];
$tier = $_POST["tier"];
$services = $_POST["services"];
$proced  = $_POST["proced"];
$addr = $_POST["addr"];
$phone = $_POST["phone"];
$depname = $_SESSION['depname'];
$qry = "INSERT INTO '.$depname.'(name,hof,tier,services,method,address,phone) VALUES ('$name','$hof','$tier','$services','$proced','$addr','$phone')"; //This is where the problem is;
if(mysqli_query($conn,$qry) === TRUE) {
echo "<script type='text/javascript'>alert('Success');
window.location='welcome.php';
</script>";
}
else{
echo "<script type='text/javascript'>alert('Error');
window.location='welcome.php';
</script>";
}
?>

【问题讨论】:

  • 强制性警告:您的代码极易受到攻击。使用准备好的 SQL 语句。
  • 既然你使用 PHP 为什么不使用header() 来执行重定向?
  • 又出现了那个糟糕的连接。您不需要在双引号字符串中连接句点。并且表名应该是反引号,而不是引号。这在 Carcigenicate 的评论之上。
  • 那么第 12 行无法生成您指定的错误。它只是一个变量赋值,与mysqli_result 对象无关
  • 请不要将代码转储到 cmets。如果您有更改,请编辑您的原始问题以包含更新。

标签: php mysqli xampp


【解决方案1】:

除了其他人所说的之外,这应该可以解决您的错误。您仍然会遇到需要修复的安全问题。

另外,我不使用 mysqli 我使用 PDO,所以如果语法稍有错误,请原谅我。

您的问题是 mysqli_query() 没有返回一行。您需要从结果中获取一行,然后将其分配给$_SESSION['depname']

Login.php 应该是这样的

// Note we are using prepared statements to prevent SQL injections
// Also note the use of backticks `, which are used for identifiers
$mysqli = new mysqli('host', 'user', 'password', 'database');
$stmt = $mysqli->prepare('SELECT `id`,`depname` FROM `admin` WHERE `username` = ? and password = ?');
$stmt->bind_param('ss', $myusername, $mypassword);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows == 1) {
    session_start();
    $row = $result->fetch_assoc();
    $_SESSION['depname'] = $row['depname'];
    header("location: welcome.php");
    exit;
}

其他脚本

<?php 
session_start();
include 'dbconn.php';
$name = $_POST["name"];
$hof = $_POST["hof"];
$tier = $_POST["tier"];
$services = $_POST["services"];
$proced  = $_POST["proced"];
$addr = $_POST["addr"];
$phone = $_POST["phone"];
$depname = $_SESSION['depname'];

$qry = "INSERT INTO `{$depname}` (`name`,`hof`,`tier`,`services`,`method`,`address`,`phone`) VALUES (?,?,?,?,?,?,?)";

// prepare our query to prevent sql injections 
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('sssssss', $name, $hof, $tier, $services, $proced, $addr, $phone);
$stmt->execute();

// not sure why you aren't using header here like @JayBlanchard said, but whatever
if($stmt->affected_rows == 1) {
    echo "<script type='text/javascript'>alert('Success');
window.location='welcome.php';
</script>";
}
else
{
echo "<script type='text/javascript'>alert('Error');
window.location='welcome.php';
</script>";
}

【讨论】:

  • 非常感谢您的支持。尝试使用 header 代替 window.location='welcome.php' 但警报功能不起作用,页面在此之前被重定向。
猜你喜欢
  • 1970-01-01
  • 2012-08-02
  • 2015-03-08
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
相关资源
最近更新 更多