1

我有一个非常糟糕的学习 php 的一天。有人可以告诉我为什么这行不通吗?我在第 23 行对非对象和未定义变量调用成员函数()时出错,这是代码:

<?php
// connect to the MySQL server
$conn = new mysqli('localhost', 'username', 'password', 'Org_db');

// check connection
 if (mysqli_connect_errno()) {
 exit('Connect failed: '. mysqli_connect_error());
 }
 //for insert user info into users table columns
 $id = '';
 $name = 'Bob Grundy';
  $pass = md5("secure");
  $email = 'bob@acgs.org';
  $reg_date = date("m/d/Y");
 //line 23
 if (!$stmt = $mysqli->prepare("INSERT INTO `users` (`id`, `name`, `pass`, `email`, 
 `reg_date`)     VALUES ( ?,?,?,?,?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
 }
 /* Prepared statement, bind and execute */


if (!$stmt->bind_param("issss", $id, $name, $pass, $email, $reg_date)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
?>
4

1 回答 1

1

你定义了$conn = new mysqli(..,但是在你使用之后!$stmt = $mysqli->prepare("..,在这里,$mysqli变量应该被更改$conn为:

 if (!$stmt = $conn->prepare("INSERT INTO `users` (`id`, `name`, `pass`, `email`, 
 `reg_date`)     VALUES ( ?,?,?,?,?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
 }
于 2013-10-13T05:32:18.440 回答