第45行mysqli给出错误

时间:2016-11-07 07:08:42

标签: php phpmyadmin

为什么第45行mysqli发生错误。我严格只想使用mysqli? 我试过没有" new"通过创建对象它不起作用。但是,当我在连接数据库行中跳过新的时,它仍然无法正常工作。 请参考我的最佳解决方案,使用mysqli检查声明进行插入,查看,编辑和删除? 使用

 <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['submit'])){

         $firstname     = $_POST['firstname'];
         $lastname      = $_POST['lastname'];
         $email          = $_POST['email'];



// prepare and bind
$prep_stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");

if($stmt = $conn->prepare($prep_stmt)){

$stmt->bind_param("sss", $firstname, $lastname, $email);
$stmt->execute();
            echo "<script>alert('data inserted')</script>"; 

        }
}

?>

1 个答案:

答案 0 :(得分:0)

  

我现在解决了我的代码。

<?php
//$servername = "localhost";
//$username = "root";
//$password = "";
//$dbname = "myDB";

// Create connection
//$conn = new mysqli($servername, $username, $password, $dbname);

$conn = mysqli_connect("localhost", "root", "", "myDB");




// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['submit'])){

         $firstname     = $_POST['firstname'];
         $lastname      = $_POST['lastname'];
         $email          = $_POST['email'];



// prepare and bind
$prep_stmt ="INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)";

if($stmt = $conn->prepare($prep_stmt)){

$stmt->bind_param("sss", $firstname, $lastname, $email);
$stmt->execute();
            echo "<script>alert('data inserted')</script>"; 

        }
}

?>