无法将数据插入数据库000webhost

时间:2018-05-01 02:48:30

标签: database

我连接到000webhost数据库,它全部正在运行但无法插入数据,它给我的不是未插入! (我写的回声) 我真的不知道为什么我不能上传到数据库我已连接到它但不会将数据发送到数据库我有所有的登录信息隐藏其他一切正常!

<?php
    $con=mysqli_connect("localhost", "************", "*********", "*************");

    if (!$con) {
        echo "Not Connected!";
    } else {
        echo "Connection Succeful.";
    }

    if (!mysqli_select_db($con, 'id5456491_emails')) 
    {
        echo 'Database not selected!!';
    }

    $Name = $_POST['username'];
    $Email = $_POST['email'];

    $sql_store = "INSERT INTO emails1 (ID,Name,Email) VALUES (NULL,'$Name','$Email')";
    $sql = mysqli_query($con, $sql_store);

    if (!mysqli_query($con,$sql_store)) {
        echo 'Not Inserted!';
    } else {
        echo "INSERTED!ss";
    }
    //header("refresh:2; url=referal.php");
 ?>
<!DOCTYPE html>
<html>
<head>

    <link href="https://fonts.googleapis.com/css?family=Do+Hyeon" rel="stylesheet">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>B.F.B Hvac</title>
    <style type="text/css">

    #logo {
        width: 50%;
        margin: auto;
        border:5px solid black;
        border-radius: 50%
    }

    #content {
        text-align: center;    
    }

    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: rgb(61, 60, 59);
    }

    li {
        float: left;
    }

    li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
        font-family: 'Do Hyeon', sans-serif;        
    }

    li a:hover {
        background-color: #111;
    }

    #about {
        background-color:rgb(61, 60, 59);
        font-family: 'Do Hyeon', sans-serif;
        color:white;
    }

    #cool {
        font-family: 'Do Hyeon', sans-serif;
        color: blue;
        font-size: 30px;
    }

    #heat {
        font-family: 'Do Hyeon', sans-serif;
        color: red;
        font-size: 30px; 
    }
    #p1 {
        font-family: 'Do Hyeon', sans-serif;
        color: black;
        font-size: 30px; 
    }


    #login-form {
        background-color: black;
        width: 100%;
    }

    .ll {
        background-color: grey;
        height: 20px;
    }

    ::placeholder {
        color:black;
    }

    .ll:hover {
        border-color: grey;
        height: 25px;
        color: white;
        ::placeholder {
            color:white;
        }
    }

    #btn {
        background-color: grey;
    }

    #btn:hover {
        border-color: black;
        color:white;
    }
</style>
</head>
<body>

    <div id="head">
        <ul>

            <li><a href="index.php">Home</a></li>
            <li><a href="quote.php">Quote</a></li>
            <li><a href="gallery.html">Gallery</a></li>
            <li><a href="referal.php">Referal</a></li>
            <li><a href="https://www.goodmanmfg.com/product-registration">Register</a></li>
            <li><a href="specials.html">Specials</a></li>
        </ul>
    </div>

    <div id="content">

        <a href="https://www.facebook.com/BFB-Heat-Air-703964289638814/">
            <img src="BFBlog.jpg" id="logo">
        </a>

        <h1 id="about">Referal</h1>

        <form action="dbh.php" method="POST">

            <input type="text" name="username" placeholder="Your email">
            <br><br>
            <input type="text" name="email" placeholder="Referal email">
            <br><br>
            <input type="submit" value="Submit">

        </form>

        <br>
        <br>
        <br>
        <br>
    </div>

    <div id="foot"></div>

    <script type="text/javascript"></script>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

从下面的代码中,您看起来两次都在执行相同的INSERT

$sql_store = "INSERT INTO emails1 (ID,Name,Email) VALUES (NULL,'$Name','$Email')";

// the first INSERT
$sql = mysqli_query($con, $sql_store);

// the second same INSERT again!!
if (!mysqli_query($con,$sql_store)) {
    echo 'Not Inserted!';
} else {
    echo "INSERTED!ss";
}

桌上有UNIQUE constraint,例如:姓名,电子邮件?如果是,我怀疑第一个INSERT成功,但第二个因UNIQUE constraint违规而失败。您可以像这样更改您的代码。重试:

$sql_store = "INSERT INTO emails1 (ID,Name,Email) VALUES (NULL,'$Name','$Email')";
// only 1 INSERT
$success = mysqli_query($con, $sql_store);

// check for INSERT return status
if (!success) {
    echo 'Not Inserted!';
} else {
    echo "INSERTED!";
}

答案 1 :(得分:0)

不要将auto_increment用作主键。 手动添加,使用max(primary_key)+1 对我有用

相关问题