PHP MySQLi在表中插入了太多行

时间:2017-11-18 14:51:21

标签: php html mysqli

我需要使用PHP和MySQLi将HTML表单中的数据插入数据库。

但是我的代码工作不正常。它在表中插入了几行相同的数据。

我到处都在寻找答案,但没有任何帮助。我是新手,所以请帮助我找到问题所在。如果有更好的方法可以做某事,我也希望看到这一点。

谢谢。

connect.php

case class Distance(zip: String, id_5: Array[Int])
val dist = Seq(Distance("72712",Array(72713,72714,72715)))
val distDS=dist.toDS()

case class Customer (cust_id: Int, id: String)
val c = Seq(Customer(1,"72713"),Customer(2,"72714"),Customer(3,"72720"))
val custDS = c.toDS()

val res = distDS.joinWith(custDS,distDS.col("id_5"(??????)) === custDS.col("id"))`

form.php的

$link = mysqli_connect("localhost", "root", "", "tutorial");
// Check connection
if ($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
else {
    // echo "Connected. ";
}

insert.php

<?php
    // Connection
    include("connect.php");
?>

<form action="insert.php" method="post">
    <p>
        <label for="firstName">First Name:</label>
        <input type="text" name="firstname" id="firstName">
    </p>
    <p>
        <label for="lastName">Last Name:</label>
        <input type="text" name="lastname" id="lastName">
    </p>
    <p>
        <label for="Country">Country:</label>
        <select name="country_id" id="Country">
            <?php
                $sql = mysqli_query($link, "SELECT * FROM country");
                while ($row = $sql->fetch_assoc()) {
                    unset($country, $name);
                    $country = $row['country_id'];
                    $name = $row['name'];
                    echo '<option value="'.$country.'">'.$name.'</option>';
                }
            ?>
        </select>
    </p>
    <input type="submit" name="submit" value="Submit">
</form>

4 个答案:

答案 0 :(得分:1)

你正在调用mysqli_stmt_execute($ stmt)2次

答案 1 :(得分:1)

你两次调用mysqli_stmt_execute()2。

这是如何使用面向对象的风格

connect.php:

<?php
    $mysqli = new Mysqli("localhost", "root", "", "tutorial");
    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
?>

insert.php:

<?php
    // Connection
    require_once "connect.php";

    $query = "INSERT INTO user (country_id, firstname, lastname) VALUES (?,?, ?)";
    if($stmt = $mysqli->prepare($query)) {

        $country = $_REQUEST['country_id'];
        $firstname = $_REQUEST['firstname'];
        $lastname = $_REQUEST['lastname'];

        $stmt->bind_param('sss',$country,$firstname,$lastname);

        if($stmt->execute()) {

            echo 'Success ! ';

            $stmt->close();
            $mysqli->close();

        } else {
            echo "Could not execute query: " . $mysqli->error();
            $stmt->close();
            $mysqli->close();
        }

    } else {
       echo 'Error preparing statement : ' . $mysqli->error;
       $stmt->close();
       $mysqli->close();
    }

答案 2 :(得分:0)

不确定它是否会有所帮助,但请尝试更改:

1 - mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
2 - if(mysqli_stmt_execute($stmt)){
    echo "Records inserted successfully.";
} 

您可以删除数字1。

答案 3 :(得分:0)

<li *ngFor="let emp of employees"> <p>{{emp.id}}</p> <p>{{emp.name}}</p> </li> 返回true或false,将返回的值赋给var(更多信息http://php.net/manual/it/mysqli-stmt.execute.php

替换此

mysqli_stmt_execute

有了这个

mysqli_stmt_execute($stmt);

// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
    echo "Records inserted successfully.";
}