Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
另一个“致命错误:”问题中的非对象上调用成员函数prepare(),但是给出的答案并不能帮助解决我的问题!

我试图写通过表单接收到的信息,然后使用php将其写到mysql数据库。

错误:

Fatal error: Call to a member function prepare() on a non-object in /home/.../myform.php on line 17


我的PHP代码:

<?PHP

session_start();


require("DBconnect.php");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
// data sent from contactForm


// prepare and bind
$stmt = $conn->prepare("INSERT INTO Final (user_name, first_name, last_name) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $user_name, $first_name, $last_name);

// set parameters from POST request and execute
$user_name = $_POST['user_name'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$stmt->execute();


echo "New records created successfully";

$stmt->close();
$mysqli->close();

?>


我的DBconnect.php包含以下内容:

<?php
  $mysqli = new mysqli('localhost', 'user', 'pasword', 'dbname');
?>

最佳答案

您试图在错误的对象上调用prepare()。不是$conn,而是$mysqli

10-07 19:53
查看更多