它不会将数据插入数据库,但没有出现任何错误

时间:2019-02-07 18:31:43

标签: php mysql database mysqli

数据未插入数据库。 配置文件在我需要的另一个文件中,所以这不是连接详细信息问题,等等。 我已经尽力了,但还没有弄清楚。

<?php
if(isset($_POST['sub'])){
    $salary = $_POST["salary"];
    $phone = $_POST["phone"];
    $contact = $_POST["contact"];
    $andesc = $_POST["andesc"];
    $address = $_POST["address"];
    $anname = $_POST["anname"];
    $typeto = $_POST["typeto"];
    $sql = "INSERT INTO announcements (salary, phone, contact, andesc, address, anname, typeto)
    VALUES ('$salary','$phone','$contact','$andesc','$address','$anname','$typeto')";
    }
?>

我希望能够将数据插入数据库。

2 个答案:

答案 0 :(得分:0)

您的查询未运行,因此未将其添加到数据库的原因是此处添加代码的完整代码。

  <?php
    if(isset($_POST['sub'])){
        $salary = $_POST["salary"];
        $phone = $_POST["phone"];
        $contact = $_POST["contact"];
        $andesc = $_POST["andesc"];
        $address = $_POST["address"];
        $anname = $_POST["anname"];
        $typeto = $_POST["typeto"];
        $sql = "INSERT INTO announcements (salary, phone, contact, andesc, address, anname, typeto)
        VALUES ('$salary','$phone','$contact','$andesc','$address','$anname','$typeto')";

global $conn;
$conn->query($sql);

}
    ?>

replace $conn with your connection variable from connection file

答案 1 :(得分:0)

只需执行您的查询即可。

<?php

include 'config.php'; //include your database connection file

if(isset($_POST['sub'])){
$salary = $_POST["salary"];
$phone = $_POST["phone"];
$contact = $_POST["contact"];
$andesc = $_POST["andesc"];
$address = $_POST["address"];
$anname = $_POST["anname"];
$typeto = $_POST["typeto"];



$sql = "INSERT INTO announcements (salary, phone, contact, andesc, address, anname, typeto) VALUES ('$salary','$phone','$contact','$andesc','$address','$anname','$typeto')";

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

mysqli_close($conn);
}

?>