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
https://forums.phpfreaks.com/topic/297198-saving-multiple-option-boxes-to-db/
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();
    }
}

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

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!

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.