简体   繁体   English

使用php将多个复选框中的值插入mysql数据库的多行中

[英]Inserting values from multiple checkboxes into multiple rows in a mysql database using php

I have stayed up two nights and I haven't been able to fix this. 我已经住了两个晚上,但无法解决这个问题。 I am new to the site as well as in PHP please forgive my inexperience. 我对网站以及PHP都是新手,请原谅我的经验。 The idea is that when a user selects several courses it should be sent to the database and stored in separate rows. 这个想法是,当用户选择多个课程时,应将其发送到数据库并存储在单独的行中。 what happens now is that it stores only the first value twice in the database. 现在发生的是它仅将第一个值存储在数据库中两次。 thanks. 谢谢。 code: 码:

<?php
include 'core/init.php';
protect_page();
include 'includes/overall/header.php';
$user_id=$_SESSION['user_id'];
?>

<h2>Register</h2>

<?php

if(isset($_GET['success']) && empty($_GET['success'])){
    echo 'You have successfully registered!';
}
else{
        if(empty($_POST)===false){
        $course[]=$_POST['course_code'];
        $user_id= $user_data['user_id'];
        $username=$user_data['username'];

        foreach($course as $c){

        $data= '\''.implode('\',\'',$c).'\'';

        mysql_query("INSERT INTO `lenroc_ssims`.`registercourses`(`user_id`, `username`, `course_code`) VALUE ('$user_id','$username', $data)");

            header('location:courses.php?success');
            exit();
            }
        }
    ?>
    <form action="" method="post">
    <?php

    $sql = "SELECT * FROM course";
    $result = mysql_query($sql)or die(mysql_error());

    echo "<table>";
    echo "<tr><th>COURSE CODE</th><th>COURSE TITLE</th><th>UNIT</th><th>SEMESTER</th><th>LEVEL</th></tr>";

    while($row = mysql_fetch_array($result)){
    $course_code        = $row['course_code'];
    $course_title       = $row['course_title'];
    $course_unit        = $row['course_unit'];
    $semester           = $row['semester'];
    $level              = $row['level'];
    echo "<tr><td style='width: 100px;'>".$course_code."</td><td style='width: 600px;'>".$course_title."</td><td>".$course_unit."</td><td>".$semester."</td><td>".$level."</td><td><input type=\"checkbox\" name=\"course_code[]\" value=".$course_code."></td></tr>";
    } // End our while loop
    echo "</table>";
    ?>
    <input type="submit" value="Register">
    </form>

<?php
}
include 'includes/overall/footer.php';
?>

Your code is dangerous. 您的代码很危险。 It is not resistant for sql injection. 它不能抵抗sql注入。 You should stop using mysql_ functions and switch to mysqli or PDO. 您应该停止使用mysql_函数,并切换到mysqli或PDO。

But just to fix the bug now you can change your code in this part: 但是,为了立即修复该错误,您可以在此部分中更改代码:

foreach($course as $c){
    mysql_query("INSERT INTO `lenroc_ssims`.`registercourses`(`user_id`, `username`, `course_code`) 
                 VALUES ('$user_id','$username', $c)");
}
header('location:courses.php?success');
exit();

redirection inside loop stopped the process so it did only once. 循环内的重定向停止了该过程,因此只执行了一次。 for good practice do not put sql query inside loop it makes slow process. 好的做法是,不要将sql查询放入循环中,这会使过程变慢。

$values = '';
foreach($course as $c){
    $values .= "('$user_id','$username', '$c'), ";
}

$values = rtrim($values, ',');
mysql_query("INSERT INTO `lenroc_ssims`.`registercourses`(`user_id`, `username`, `course_code`) VALUES {$values}");
header('location:courses.php?success');
exit();

if you don't agree, why you don't write some comment? 如果您不同意,为什么不写评论?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM