Jump to content

Saving Multiple option boxes to DB


dapcigar

Recommended Posts

Hello all,

Am trying to retrieve a table from the DB, allow user select multiple options and also put the level . i was able to save the Data of the selected checkboxes but i cant get the level to save.

 

my display code

<form action="save_comp.php" method="post">
<?php

include ('mysql_connect.php');

$sql = mysql_query("SELECT * FROM competency WHERE department = '$department'");


while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>";
echo"<input type='checkbox' name='comp[]' value= ".$row['id']." /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[]'>
            
				  <option></option>
				  <option>level 1</option> 
				  <option>level 2</option> 
				  <option>level 3</option> 
				  <option>level 4</option>
				  <option>level 5</option>  </select>                 </td> ";

}
echo "</tr>";
?>

<input name="submit" type="submit" value="submit" />
</form>
<?php
echo" </table>";

Code to save to DB

<?php
session_start();
$id = $_SESSION['user_id'];
//$id = 3;
$hobb = $_POST['comp'];
$level  = $_POST['level'];
include ('mysql_connect.php');


 $N = count($hobb);

        echo("<p>You selected $N Hobbies(s): ");
        for($i=0; $i < $N; $i++)
        {
			
			
            $var1=$hobb[$i];
			$var2 = $level[$i];
            
			include ('mysql_connect.php');
            $table = "INSERT INTO competency_result (user_id,competency_id,level) ".
                     "VALUES ('$id', '$var1', '$var2')";
            mysql_query($table) or die(mysql_error());
            $inserted_fid = mysql_insert_id();
            mysql_close();  
        }

Link to comment
Share on other sites

Here's how I'd do it.

 

Change your form to this:

echo"<input type='checkbox' name='comp[" . $row['id'] . "' value= '1' /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[" . $row['id'] . "]'>

        <option></option>
        <option value='1'>level 1</option>
        <option value='2'>level 2</option>
        <option value='3'>level 3</option>
        <option value='4'>level 4</option>
        <option value='5'>level 5</option>  </select>                 </td> ";
And then process as such:

<?php

session_start();
$id = $_SESSION['user_id'];
include ('mysql_connect.php');

$hobbies = $_POST['comp'];

if (count($hobbies) !== 0) {
    echo("<p>You selected " . count($hobbies) . " Hobbies(s): ");

    $insert = array();
    foreach (array_keys($hobbies) as $hobbyId) {
        $level = $_POST['level'][$hobbyId];
        
        $insert[] = "(" . intval($id) . ", " . intval($hobbyId) . ", '" . mysql_real_escape_string($level) . "')";
    }
    
    if (!empty($insert)) {
        $query = "INSERT INTO  competency_result (user_id, competency_id, level) VALUES " . implode(',', $insert);
        mysql_query($query) or die (mysql_error());
        $insertedId = mysql_insert_id();
    }
}
Edited by scootstah
Link to comment
Share on other sites

Here's how I'd do it.

 

Change your form to this:

echo"<input type='checkbox' name='comp[" . $row['id'] . "' value= '1' /> ".$row['competency']." <br /> </td>";
echo"<td> <select name='level[" . $row['id'] . "]'>

        <option></option>
        <option value='1'>level 1</option>
        <option value='2'>level 2</option>
        <option value='3'>level 3</option>
        <option value='4'>level 4</option>
        <option value='5'>level 5</option>  </select>                 </td> ";
And then process as such:

<?php

session_start();
$id = $_SESSION['user_id'];
include ('mysql_connect.php');

$hobbies = $_POST['comp'];

if (count($hobbies) !== 0) {
    echo("<p>You selected " . count($hobbies) . " Hobbies(s): ");

    $insert = array();
    foreach (array_keys($hobbies) as $hobbyId) {
        $level = $_POST['level'][$hobbyId];
        
        $insert[] = "(" . intval($id) . ", " . intval($hobbyId) . ", '" . mysql_real_escape_string($level) . "')";
    }
    
    if (!empty($insert)) {
        $query = "INSERT INTO  competency_result (user_id, competency_id, level) VALUES " . implode(',', $insert);
        mysql_query($query) or die (mysql_error());
        $insertedId = mysql_insert_id();
    }
}

Didn't work..

Link to comment
Share on other sites

When something "didn't work" it usually helps all concerned if the voicer of that meaningless comment provides some backup as to WTH he/she is describing. Someone who was nice enough to provide a goodly chunk of code to you deserves more of a response than this so that he/she can help possibly debug it or to further discuss with you the problem.

 

Jeez!

Link to comment
Share on other sites

When something "didn't work" it usually helps all concerned if the voicer of that meaningless comment provides some backup as to WTH he/she is describing. Someone who was nice enough to provide a goodly chunk of code to you deserves more of a response than this so that he/she can help possibly debug it or to further discuss with you the problem.

 

Jeez!

So sorry if that sounds like i didn't appreciate his effort. trust me i do. I should really apologize for my comment. it was harsh. i was only giving a feedback not to undermine him. i really need to fix this issue.

Link to comment
Share on other sites

So sorry if that sounds like i didn't appreciate his effort. trust me i do. I should really apologize for my comment. it was harsh. i was only giving a feedback not to undermine him. i really need to fix this issue.

And yet you still haven't told us what the problem is. We can't see your computer monitor. You need to tell us what you're seeing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.