PHP到MySQL中的未定义变量错误

时间:2018-09-05 07:42:07

标签: php html mysql database mysqli

我有一个HTML输入,我想连接到MySQL数据库。我收到两个

  

insert.php中的“未定义的变量:sql”错误(第17和20行)。

我正在阅读其他文章,但找不到解决方法。

index.php

<form action="insert.php" method="POST">
Firstname: <input type="text" name="fname" required /><br>
Lastname: <input type="text" name="lname" require /><br>
<input type="submit" />
</form>

insert.php

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

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $query  = "INSERT INTO MyGuests (fname, lname)
                VALUES ('".$_POST["fname"]."','".$_POST["lname"]."')";

    if ($conn->multi_query($sql) === TRUE) {
        echo "New records created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
  ?>

3 个答案:

答案 0 :(得分:0)

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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql  = "INSERT INTO MyGuests (fname, lname)
            VALUES ('".$_POST["fname"]."','".$_POST["lname"]."')";

if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

答案 1 :(得分:0)

更改

  $query  = "INSERT INTO MyGuests (fname, lname)
                VALUES ('".$_POST["fname"]."','".$_POST["lname"]."')";

  $sql  = "INSERT INTO MyGuests (fname, lname)
                VALUES ('".$_POST["fname"]."','".$_POST["lname"]."')";

答案 2 :(得分:0)

尝试一下:

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

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) 
    VALUES (:firstname, :lastname, :email)");
    $stmt->bindParam(':firstname', $firstname);
    $stmt->bindParam(':lastname', $lastname);
    $stmt->bindParam(':email', $email);

    // insert a row
    $firstname = "John";
    $lastname = "Doe";
    $email = "john@example.com";
    $stmt->execute();

    // insert another row
    $firstname = "Mary";
    $lastname = "Moe";
    $email = "mary@example.com";
    $stmt->execute();

    // insert another row
    $firstname = "Julie";
    $lastname = "Dooley";
    $email = "julie@example.com";
    $stmt->execute();

    echo "New records created successfully";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
?>