0

I am not sure the best way to do this or if it's even possible. Basically I have a checkbox that looks like this:

php

foreach($clients as $client){
    echo'
    <input type="checkbox"  name="client_data[]" value="'.$class_id.'">
    '.$client['first_name'].' ('.$client['nickname'].') '.$client['last_name'].'
            <br />';
   } // foreach($client

HTML looks like this

<form method="post" action="">
<input type="checkbox" value="?" name="client_data[]">
Dwayne (The Rock) Johnson<br>

<input type="checkbox" value="?" name="client_data[]">
Steve (Puddin) Robinson<br>

<input type="submit" value="Add" name="exist_to_class">
</form>

When the form is submitted I want to insert the

$first_name, $nickname, $lastname

into the db with a query that looks like this:

mysql_query("INSERT INTO `clients` (`user_id`, `first_name`, `last_name`, `nickname` `class_id`) 
VALUES ('$user_id', '$first_name, '$last_name', '$nickname', '$class_id')");

Is this possible or am I even close on how I am attempting to set this up? I have not had much luck so far.

My db table looks like this:

Client Table

I need to be able to enter the client multiple time with different class_id's.

What is the best way to accomplish this?

Here is the code that call the function to insert data into db:

if (isset($_POST['exist_to_class'])){
if (empty($_POST['client_data']) === true){
    $errors [] = 'You much select a client to be added to the class.';
} else {
    if (isset($_POST['client_data']) && !empty($_POST['client_data']));
     list($first_name, $nickname, $last_name) = explode('|', $_POST['client_data']);
     exist_client_to_class($class_id);
     header('Location: view_class.php?class_id='.$class_id.' ');

}

} //isset

And here is my query:

function exist_client_to_class($class_id, $user_id){
$class_id = (int)$class_id;
$user_id = (int)$user_id;


mysql_query("INSERT INTO `clients` (`user_id`, `first_name`, `last_name`, `nickname` `class_id`) 
VALUES ('$user_id', '$first_name, '$last_name', '$nickname', '$class_id')");
}

What am I doing wrong?

3
  • You should put the first and last name in the value attribute, because that attribute is the only information you get server side. Commented Nov 8, 2012 at 20:10
  • 2
    Since you're building that info from what looks to be DB-based information, don't round-trip anything through the client you don't have to. By all means display the full names, but your checkbox values should contain ONLY the id of the record being displayed for that checkbox. easier to pass around a simple int than some complicated string you'll never reliably pull apart again later on anyways. Commented Nov 8, 2012 at 20:12
  • @MarcB I would do that but a 'Client' in the db can have multiple entries or be a part of multiple classes so the client_id will be different for each class they attend. I plan to work with this info based on an int that pertains to a certain class and the user_id. Does that make sense? Commented Nov 8, 2012 at 20:19

2 Answers 2

1

You can't pass more than one variable through a single checkbox. Marc B is right, in that if this is a database-backed application then the right way to do it would be to have the checkbox send the ID for the person who's selected, and use the ID to look up whatever information you need about them.

If you're not using a database, a quick-and-dirty way to do this would be to put the information about the person into an array and then run it through serialize() to turn it into a string and use that as the value attribute. On the other end you can run it through unseialize() to get back the array with the values you wanted.

Remember that if you do this, you need to either escape your sql query or (very strongly preferred) use a prepared query.

Sign up to request clarification or add additional context in comments.

2 Comments

I added a pic of db table and a little more info of what Im trying to do.
I am using a database backed app but there will be possible multiple entries for the same client with only the class_id being different. Really not sure if the way I am trying to do this is the best way.
0

okay, you may path string in value as Asad suggested and than split it on the server, like this

foreach($clients as $client){
    echo'
    <input type="checkbox"  name="client_data" value="'.$class_id.'|'.$client['first_name'].'|'.$client['nickname'].'|'.$client['last_name'].'">
    '.$client['first_name'].' ('.$client['nickname'].') '.$client['last_name'].'
    <br />';
} // foreach($client

and on the server have something like

list($class_id, $first_name, $nickname, $last_name) = explode('|', $_POST['client_data'])
mysql_query("INSERT INTO `clients` (`user_id`, `first_name`, `last_name`, `nickname` `class_id`) 
VALUES ('$user_id', '$first_name, '$last_name', '$nickname', '$class_id')");

I am not sure where from $class_id variable in template and $user_id in the model, so you have to figure it out, also drawback here is that if in some variable will be placed delimiter | data will be split wrong. To avoid it you may use hidden inputs associated somehow with the main checkbox (javascript, logic or whaterver will play best in your case)

UPD: oh yea you may serialize/unserialize data as array https://www.php.net/serialize as octern suggestion

4 Comments

Does the list statement need to be called with the function or inside the sql query?
not necessary at all, this is just to simplify variables assignment us3.php.net/list
I posted an edit above of my code that calls the function as well as my sql query. Can't figure out what to do next? I know I am using a deprecated method but this is how I learned PHP and once all the functions are performing properly Im going go back and rewrite functions.
that's fine, just go on. in fact things not such complicated when you get on them. for a while I would suggest you read on more php.net/manual/en/reserved.variables.post.php post variable in PHP and as well how pure HTML forms work for getting big picture

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.