PHP变量未插入数据库

时间:2017-06-16 14:04:04

标签: php mysql

<form action="main.php" method="POST">
    <input type="email" name="email" placeholder="Enter your email">
    <img type="image" id="send" src="bs.png" alt="Submit Form"/>
    <button type="sumbit">Submit</button>
</form>

<?php
$con = 
mysqli_connect("mysql.hostinger.co.uk",
"something","something","something");

$email = $_POST['email'];

$sql = "INSERT INTO emails (email) 
VALUES ('$email')";

$run = mysqli_query($conn, $sql);

header("Location: result.htm");    

(三个不同的数据库,它只是没有插入表中..) 我一直试图将它修复三个小时,如果不是更多的话。也许我只是个白痴,但我看不出有什么不对。

-table name是电子邮件

- 有2行:email varchar(255)和id自动递增

3 个答案:

答案 0 :(得分:0)

在执行查询时检查变量的名称 你写了$ conn并在初始化连接时使用了$ con

答案 1 :(得分:0)

可能会有所帮助的事情:

不要将个人凭据放在stackoverflow上!

可变检查!

$con = 
mysqli_connect("mysql.hostinger.co.uk",
"u394e","ere31","u159il");

$con。其中

$run = mysqli_query($conn, $sql);

$conn

<强>反引号

使用反引号可以使用插入数据库。如下例所示:

$sql = "INSERT INTO emails (`email`) 
VALUES ('$email')";

检查您的查询

尝试使用sql_error在查询周围添加if语句,如果它不起作用的话。示例如下:

if(isset($_POST['submit'])) {
    $email = $_POST['email'];

    $sql = "INSERT INTO emails (email) 
    VALUES ('$email')";

    if ($con->query($sql) === TRUE) {
        echo "<script>alert('Uw reactie is toegevoegd.')</script>";
    } else {
        echo "Error: " . $sql . "<br>" . $con->error;
    }
}

希望这有帮助!

答案 2 :(得分:0)

您的代码使用了错误的变量 $ conn 时出现错误,请将其替换为$ con,尝试使用此代码。它有效。

<form action="main.php" method="POST">
    <input type="email" name="email" placeholder="Enter your email">
    <img type="image" id="send" src="bs.png" alt="Submit Form"/>
    <button type="sumbit">Submit</button>
</form>

<?php
if(isset($_POST['email'])){
$con = mysqli_connect("mysql.hostinger.co.uk","u1e","1","u1il");

$email = $_POST['email'];

$sql = "INSERT INTO emails (email) VALUES ('$email')";

$run = mysqli_query($con, $sql);

header("Location: result.htm");  
}
?>