简体   繁体   English

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

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

I keep getting this error我不断收到此错误

Fatal error: Call to a member function bind_param() on a non-object in H:\\USBWebserver\\root\\GIPSite\\stagetoevoegen.php on line 46致命错误:在第 46 行的 H:\\USBWebserver\\root\\GIPsite\\stagetoevoegen.php 中的非对象上调用成员函数 bind_param()

After a lot of research and tried many possible solutions, it still gives the error Here is the code经过大量研究并尝试了许多可能的解决方案,它仍然给出错误这是代码

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();

        }

I have a connection.php file that is included and other pages do work with it.我有一个包含在内的 connection.php 文件,其他页面可以使用它。

Edit: I tested your code and was successful ( in conjunction with my answer ).编辑:我测试了您的代码并成功(结合我的回答)。
See my troubleshooting-debugging notes below.请参阅下面的故障排除调试说明。

Taking out the backticks around the E-mail column gave the following error message similar to this and using error reporting such as:去掉E-mail列周围的反引号给出了与此类似的以下错误消息,并使用错误报告,例如:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

at the top under the opening <?php tag在开头<?php标签下的顶部

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax;致命错误:未捕获的异常“mysqli_sql_exception”,消息为“您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '-mail, Opmerking) values(?,?,?,?,?,?)' at line 1' in /home/user/public_html/dbtest1.php:17 Stack trace: #0 /home/user/public_html/dbtest1.php(17): mysqli->prepare('insert into tbl...') #1 {main} thrown in /home/user/public_html/dbtest1.php on line 17检查与您的 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


Original answer before edit:编辑前的原始答案:

Wrap your E-mail column value in backticks or choose another way, Email or E_mail用反引号包裹您E-mail列值或选择另一种方式, EmailE_mail

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

You can use punctuation, white space, international characters, and SQL reserved words if you use delimited identifiers.如果使用分隔标识符,则可以使用标点符号、空格、国际字符和 SQL 保留字。

I quote from this answer : (which I already knew)我引用了这个答案:(我已经知道了)

The major reason against use of hyphen is that most references must then quote the field names.反对使用连字符的主要原因是大多数引用必须引用字段名称。 Otherwise they will look like a subtraction operator, both to MySQL and humans.否则,对于 MySQL 和人类来说,它们将看起来像一个减法运算符。

Using error reporting would have caught that.使用错误报告会发现这一点。

Example:例子:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Error checking methods (MySQL-PDO)错误检查方法(MySQL-PDO)

MySQL 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

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

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

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

a) You can also try this method: a) 你也可以试试这个方法:

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

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

    b) Check to see what your column types are. b) 检查您的列类型。

    c) Place var_dump($invoegen); c) 放置var_dump($invoegen); right after your prepare call.在您的准备电话之后。

    d) Check your column names for spelling and/or actual existence. d) 检查您的列名称的拼写和/或实际存在。

    e) Check to see if your form elements are named and with no typos, checking for letter-case also. e) 检查您的表单元素是否已命名且没有拼写错误,还要检查字母大小写。 A!=a; meaning that an uppercase A is not treated the same as a .意味着大写Aa For example: name="Animal" as opposed to name="animal" those are two different animals altogether.例如: name="Animal"name="animal"相反,它们完全是两种不同的动物。


The code I used to test it with: - All columns set to VARCHAR(255)我用来测试它的代码: - 所有列都设置为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

You need to quote it:你需要引用它:

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

Plus, if you'd had proper error handling in your code, you'd have been told that your prepare operation failed and returned a boolean false:另外,如果您在代码中进行了正确的错误处理,您会被告知准备操作失败并返回布尔值 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.

相关问题 致命错误:在非对象错误上调用成员函数bind_param() - Fatal error: Call to a member function bind_param() on a non-object error PHP致命错误:在非对象上调用成员函数bind_param() - PHP Fatal error : Call to a member function bind_param() on a non-object PHP - 致命错误:在非对象上调用成员函数 bind_param() - - PHP - Fatal error: Call to a member function bind_param() on a non-object - PHP致命错误:在非对象上调用成员函数bind_param() - PHP Fatal error: Call to a member function bind_param() on a non-object PHP致命错误:在非对象上调用成员函数bind_param()(PHP-MySQL PreparedStatement) - PHP Fatal error: Call to a member function bind_param() on a non-object (PHP-MySQL PreparedStatement) SQL-致命错误:在非对象上调用成员函数bind_param() - SQL - Fatal error: Call to a member function bind_param() on a non-object MySQL —致命错误:在非对象上调用成员函数bind_param() - MySQL — Fatal error: Call to a member function bind_param() on a non-object Mysqli PHP致命错误:在非对象上调用成员函数bind_param() - Mysqli PHP Fatal error: Call to a member function bind_param() on a non-object 致命错误:使用更新查询在非对象上调用成员函数bind_param() - Fatal error: Call to a member function bind_param() on a non-object using Update Query 致命错误:在非对象上调用成员函数bind_param() - Fatal error: Call to a member function bind_param() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM