插入数据库不工作

时间:2017-04-14 15:31:38

标签: php html mysql

我在插入表格时遇到问题。连接文件是正确的,来自header.php。没有错误但是当我进入表格时没有插入任何记录。

<?php

include('header2.php');

if(isset($_POST['done'])) {
    $title = $_POST['title'];
    $description = $_POST['description'];
    $link = $_POST['link'];
    $company = $_POST['company'];

    $sql = "INSERT INTO placements (title, description, link, company)
    VALUES ('$title', '$description', '$link','$company')";
    // use exec() because no results are returned
    echo "New record created successfully";
}

?>

<html>
<head>
    <title> Add a Placement </title>
</head>
<body>

<form method="post">
    <input type="text" name="title" placeholder="title">
    <input type="text" name="description" placeholder="description">
    <input type="text" name="company" placeholder="company">
    <input type="text" name="link" placeholder="link">
    <input type="submit" name="done">

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您根本没有执行查询。我假设您的数据库连接如下,并运行您的查询。它应该工作。

<强>测试

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if(isset($_POST['done'])) {
    $title = $_POST['title'];
    $description = $_POST['description'];
    $link = $_POST['link'];
    $company = $_POST['company'];

    $sql = "INSERT INTO placements (title, description, link, company)
    VALUES ('$title', '$description', '$link','$company')";

    if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

}