dougui4325 2017-11-18 14:51
浏览 58
已采纳

PHP MySQLi在表中插入了太多行

I need to insert data from a HTML form into a database using PHP and MySQLi.

But my code is not working properly. It inserts a few rows of the same data into the table.

I was searching for an answer everywhere but nothing helps. I am new to this so please help me to find the problem. If there is a better way to do something, I would like to see that too.

Thank you.

connect.php:

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

form.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>

insert.php:

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

    // Prepare an insert statement
    $sql = "INSERT INTO user (country_id, firstname, lastname) VALUES (?,?, ?)";

    if ($stmt = mysqli_prepare($link, $sql)) {
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "sss", $country, $firstname, $lastname);

        // Set the parameters
        $country = $_REQUEST['country_id'];
        $firstname = $_REQUEST['firstname'];
        $lastname = $_REQUEST['lastname'];
        mysqli_stmt_execute($stmt);

        // Attempt to execute the prepared statement
        if (mysqli_stmt_execute($stmt)) {
            echo "Records inserted successfully.";
        }
        else {
            echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
        }
    }
    else {
        echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
    }

    // Close statement
    mysqli_stmt_close($stmt);

    // Close connection
    mysqli_close($link);
?>
  • 写回答

4条回答 默认 最新

  • dongshou6041 2017-11-18 15:08
    关注

    mysqli_stmt_execute return true or false, assign the returned value to a var (more info http://php.net/manual/it/mysqli-stmt.execute.php)

    Replace this

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

    With this

    $execution = mysqli_stmt_execute($stmt);
    
    // Attempt to execute the prepared statement
    if($execution){
        echo "Records inserted successfully.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛