Jump to content

Recommended Posts

Hi, 

Im new here, I had read some of your post in order to find what im looking for...

here is my issue:

 

Im using php and mysql

 (the mysql table have 2 foreign keys)

I have a form with some checkboxes, this section is filled with a select because the items are from another table so my problem is when I try to do the insert in the database, the problem are the checkboxes and I really dont know how can i get the values or how can i solve my problem.

This Is how I print the list of items in checkboxes

<p><label>career:</label></p>
	<table border="0"><td><p><?	
		$sql = "SELECT * FROM career ORDER BY id_career asc";
		$res = mysql_query($sql);
		$c=0;
		while($row = mysql_fetch_assoc($res)){
			$vector[$c] = $row['career'];
			$c++;
		}
		for($f=0;$f<$c;$f++){
		?>
	<input type="checkbox" name="career[]" id="career[]" value="<? echo $career; ?>">
	<?
	$career= $vector[$f];
	echo $vector[$f];
	echo "<br>";}?></p>
	</td></table>

Then my form have an action=" " but is conected to 'function save' (is where i have my php code)

<?
function save_career($name,$gender,$status,$id_client,$id_career){
        $name=$_POST['name'];
	$gender=$_POST['gender'];
	$status="inactive";
	$query = "insert into career(id_career,name,gender,status,id_client,id_career) values(0,
	'$name','$gender','$status',$id_client,$id_career)";
	mysql_query($query);
	echo $query;
}
?>

I hope someone can help me because i tried lots of ways to solve this...

Greetings!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/298284-help-with-checkboxes-filled-by-mysql/
Share on other sites

Well, let's start with the form creation.

 

1. Do NOT use PHP short tags for code blocks.

2. Do NOT use the mysql_ library - it has been deprecated for many years. Either use mysqli_ or, better yet, PDO

3. You create a loop to store the DB results in an array and then immediately iterate over the array to use those values. Why not just create the output when reading the DB results? There are reasons why separating the logic to get the data vs. using the data makes sense. But, in this instance is it a waste of code

4. There is no need to create the variable $c to use as the index in the array. You can simply append a new value to an array without defining the index and the next numeric index will be used automatically

5. The checkbox inputs have duplicate IDs. This is invalid code. They can have the same name - but not the same ID

6. The variable $career is getting written AFTER the input field is output. So, each input has the value for the previous checkbox.

7. The value of the checkboxes should be the ID - not the name

 

You state "Then my form have an action=" " but is conected to 'function save'". What does that mean. You can't connect a PHP function to a form. The form is submitted and the PHP code determines what to do with it. A function would not get executed automatically. There's no way to tell if that function is even getting called based on what you've provided. But, I am assuming it is not getting called. The reason is the function expects certain parameters to be passed, but you are then defining the parameters within the function. If those parameters are not passed the function would fail and an error would be displayed on the page.

 

Try this for the form

 

<p><label>career:</label></p>
<table border="0">
    <td>
    <p>
<?php 
    $sql = "SELECT * FROM career ORDER BY id_career asc";
    $res = mysql_query($sql);
    while($row = mysql_fetch_assoc($res))
    {
        $id = $row['id_career'];
        $label = $row['career'];
        echo "<input type=\"checkbox\" name=\"career[$id]\" id="career[$id]" value=\"$id\">$label<br>\n";
    }
?>
    </p>
    </td>
    </table>

 

On the page that receives the form you will need to show the code that calls the function.

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.