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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀