0

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. 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';
?>
2
  • Try and do it bit by bit! First get the form showing with all the correct fields. Then try and get the post working (I suspect this is where we're stuck?). Try calling var_dump($_POST) to see what data is coming through. Then move onto the insert. Commented May 19, 2015 at 2:46
  • 1
    you redirect inside the foreach loop, so it should only add one row before be sent off to a new page. you want to move the header() call to after the loop ends Commented May 19, 2015 at 2:51

2 Answers 2

1

Your code is dangerous. It is not resistant for sql injection. You should stop using mysql_ functions and switch to mysqli or 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();
Sign up to request clarification or add additional context in comments.

Comments

0

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.

$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?

2 Comments

wont quite work, the multiple values need a comma between each set
Thanks Dragon. Updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.