问题
I am trying to access MySQL via PHP but I get these error messages.
Notice: Undefined variable: stmt in /var/www/contact.php on line 60
Fatal error: Call to a member function bind_param() on a non-object in /var/www/contact.php on line 63
My code:
<?php
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// LINE 60
if(!$stmt = $conn->prepare("INSERT INTO MyGuests VALUES(?, ?, ?)") || !is_object($stmt)){
die( "Error preparing: (" .$conn->errno . ") " . $conn->error);
}
//LINE 63
$stmt->bind_param("sss", $firstname, $lastname, $email) ;
回答1:
The object is the $conn, so if you want to include it in the if statement you can but not necessary. Am assuming that the MyGuests table as an id column and lastname, fisrtname, email column. You need to specify the actual column in the SQL, "INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"
The variable $stmt is not defined yet. So you cant say !$stmt, the function not won't work. Do something like:
$stmt = "";
if(!($stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"))){
die( "Error preparing: (" .$conn->errno . ") " . $conn->error);
}
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
if(!($stmt->bind_param("sss", $firstname, $lastname, $email))){
die( "Error in bind_param: (" .$conn->errno . ") " . $conn->error);
}
$stmt->execute();
回答2:
Simplify your error detection. Wrap the assignment statement in parentheses. Forget is_object(). Try this.
if( !( $stmt = $conn->prepare( "INSERT INTO MyGuests VALUES(?, ?, ?) " ) ) {
die( "Error preparing: (" .$conn->errno . ") " . $conn->error);
}
It's usually best in an INSERT
statement to spell out the column names, like so
INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)
If the table's columns aren't exactly what you thought, your INSERT can't work correctly without enumerating the columns. This happens to be the case with the example on w3schools. They define the MyGuests
table with some more columns than you use in your INSERT.
Do error detection on bind_param, in the same manner as on prepare.
Define the variables you use in bind_param() before using them. Try this:
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
if (!$stmt->bind_param("sss", $firstname, $lastname, $email)) {
die( "Error in bind_param: (" .$conn->errno . ") " . $conn->error);
}
$stmt->execute();
w3schools? Really? Friends don't let friends .... etc.
回答3:
The first error
Notice: Undefined variable: stmt in /var/www/contact.php on line 60 You have to declare variables before using it. Like those variables:
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
define stmt variable
$stmt = " ";
or remove it from the if statement
$stmt = $conn->prepare("INSERT INTO MyGuests VALUES(?, ?, ?)")
This will solve your errors
来源:https://stackoverflow.com/questions/26567281/php-bind-param-not-working