繁体   English   中英

致命错误:在非对象上调用成员函数 bind_param()

[英]Fatal error: Call to a member function bind_param() on a non-object

我不断收到此错误

致命错误:在第 46 行的 H:\\USBWebserver\\root\\GIPsite\\stagetoevoegen.php 中的非对象上调用成员函数 bind_param()

经过大量研究并尝试了许多可能的解决方案,它仍然给出错误这是代码

if (isset($_POST['cmdVerstuur']))
        {
            $invoegen=$link->prepare("insert into tblstagebedrijven (Naam,Begeleider, Locatie, Telefoon, E-mail, Opmerking) values(?,?,?,?,?,?)");
            $naam = $_POST['txtNaambedrijf'];
            $begeleider = $_POST['txtBegeleider'];
            $locatie = $_POST['txtLocatie'];
            $telefoon = $_POST['txtTelefoon'];
            $email = $_POST['txtEmail'];
            $opmerking = $_POST['txtOpmerking'];
            $invoegen->bind_param('ssssss', $naam, $begeleider, $locatie, $telefoon, $email, $opmerking);
            $resultaat=$invoegen->execute();

                if($resultaat)
                    echo "Het stagebedrijf is toegevoegd";
                else
                    echo "Er is een fout opgetreden";

            $link->close();

        }

我有一个包含在内的 connection.php 文件,其他页面可以使用它。

编辑:我测试了您的代码并成功(结合我的回答)。
请参阅下面的故障排除调试说明。

去掉E-mail列周围的反引号给出了与此类似的以下错误消息,并使用错误报告,例如:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

在开头<?php标签下的顶部

致命错误:未捕获的异常“mysqli_sql_exception”,消息为“您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,了解在 /home/user/public_html/ 中第 1 行的“-mail, Opmerking) values(?,?,?,?,?,?)”附近使用的正确语法dbtest1.php:17 堆栈跟踪:#0 /home/user/public_html/dbtest1.php(17): mysqli->prepare('insert into tbl...') #1 {main} 扔在 /home/user/第 17 行的 public_html/dbtest1.php


编辑前的原始答案:

用反引号包裹您E-mail列值或选择另一种方式, EmailE_mail

(Naam,Begeleider, Locatie, Telefoon, `E-mail`, Opmerking)

如果使用分隔标识符,则可以使用标点符号、空格、国际字符和 SQL 保留字。

我引用了这个答案:(我已经知道了)

反对使用连字符的主要原因是大多数引用必须引用字段名称。 否则,对于 MySQL 和人类来说,它们将看起来像一个减法运算符。

使用错误报告会发现这一点。

例子:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

错误检查方法(MySQL-PDO)

MySQL

// connect (create a new MySQLi object) with custom predefined constants
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_TABLE_NAME);

// $mysqli->connect_error is buggy until PHP 5.3.0
if (mysqli_connect_error())
{
    throw new Exception(mysqli_connect_error());
}

$result = $mysqli->query($sql);

if (!$result)
{
    throw new Exception($mysqli->error);
}

while($row = $mysqli->fetch_assoc($result))
{
    // your result handling code (print it)
}

PDO

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

脚注:(故障排除-调试)

放置var_dump($link->error); $invoegen->bind_param('ssssss'

a) 你也可以试试这个方法:

$invoegen = $mysqli->prepare("...");

    if( $invoegen !== FALSE ) {
        $invoegen->bind_param(...);
        $invoegen->execute();
    }
  • http://php.net/manual/en/mysqli.prepare.php

    b) 检查您的列类型。

    c) 放置var_dump($invoegen); 在您的准备电话之后。

    d) 检查您的列名称的拼写和/或实际存在。

    e) 检查您的表单元素是否已命名且没有拼写错误,还要检查字母大小写。 A!=a; 意味着大写Aa 例如: name="Animal"name="animal"相反,它们完全是两种不同的动物。


我用来测试它的代码: - 所有列都设置为VARCHAR(255)

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($link->connect_errno > 0) {
  die('Connection failed [' . $link->connect_error . ']');
}

    $invoegen=$link->prepare("insert into tblstagebedrijven (Naam,Begeleider, Locatie, Telefoon, `E-mail`, Opmerking) values(?,?,?,?,?,?)");

    /*
    $naam = $_POST['txtNaambedrijf'];
    $begeleider = $_POST['txtBegeleider'];
    $locatie = $_POST['txtLocatie'];
    $telefoon = $_POST['txtTelefoon'];
    $email = $_POST['txtEmail'];
    $opmerking = $_POST['txtOpmerking'];
    */

    $naam = "Fred";
    $begeleider = "txtBegeleider";
    $locatie = "txtLocatie";
    $telefoon = "txtTelefoon";
    $email = "txtEmail";
    $opmerking = "txtOpmerking";

    $invoegen->bind_param('ssssss', $naam, $begeleider, $locatie, $telefoon, $email, $opmerking);
    $resultaat=$invoegen->execute();

        if($resultaat)
            echo "Het stagebedrijf is toegevoegd - OK";
        else
            echo "Er is een fout opgetreden - NOT ok";

    $link->close();
... n, E-mail, Opmerking) v...
        ^---- seen as a mathematical subtraction operation

你需要引用它:

... n, `E-mail`, Opmerking) v...

另外,如果您在代码中进行了正确的错误处理,您会被告知准备操作失败并返回布尔值 false:

$stmt = $link->prepare(...);
if (!$stmt) {
   die($link->errorInfo); // or whatever your DB lib's error reporting method is
}

暂无
暂无

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

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