Php 插入到不工作的数据库中

Php 插入到不工作的数据库中,php,html,mysql,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'

我在插入表格时遇到问题。连接文件正确,来自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>

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

已测试

$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);
    }

}
$servername=“localhost”;
$username=“username”;
$password=“password”;
$dbname=“myDB”;
//创建连接
$conn=mysqli\u connect($servername、$username、$password、$dbname);
//检查连接
如果(!$conn){
die(“连接失败:”.mysqli_connect_error());
}
如果(isset($_POST['done'])){
$title=$_POST['title'];
$description=$_POST['description'];
$link=$_POST['link'];
$company=$_POST['company'];
$sql=“插入位置(标题、说明、链接、公司)
值(“$title”、“$description”、“$link”、“$company”)”;
if(mysqli_查询($conn,$sql)){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.
“.mysqli_Error($conn); } }
您只有一条注释,其中您应该有执行查询的代码。您的代码易受攻击。您应该使用或准备带有绑定参数的语句,如中所述。(1)在变量替换后打印出
$sql
,答案可能很明显。(2) 学习使用参数。将变量强行插入查询字符串只会在查询中带来麻烦和意外的语法错误。您从未执行过查询,实际上也没有运行过insert查询。