0

I want to have a form that allows the user to choose what data to display from a table through checking the checkboxes. If the user wants only 2 columns to be shown, should only 2 columns be shown. I have my codes, but after I submit, it displays nothing.Here's my code:

<form name="form1" method="post" action="view_emp.php">
<p>Select Option 
<input type="checkbox" name="number[]" value="name" />Name
<input type="checkbox" name="number[]" value="hired"  />Date Hired 
<input type="checkbox" name="number[]" value="basic" />Basic Pay
<input type="checkbox" name="number[]" value="incentives">Incentives
</p> 
<input type="submit" name="Submit" value="Submit"> 
</form> 

here's my php:

<?php 
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('eis', $db) or die (mysql_error());

$employee = array();

foreach ($_POST['number'] as $employee) {
    $number = mysql_real_escape_string($number);
    $employee[] = "'{$number}'";
}
$sql = "select * from employees where type in (" .implode(", ", $number). ")";
$result = mysql_query($sql); 
while ($row = mysql_fetch_array($result)) { 
 print $row['name']; 
} 
?>

i am a beginner in php and i need help from gurus and experts. thank you...

Clfc Developer
  • 15
  • 1
  • 1
  • 7
  • Where is your `$sql` that you are using in `$result = mysql_query($sql);` – Sean Mar 05 '13 at 02:12
  • [Please, don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [_prepared statements_](http://en.wikipedia.org/wiki/Prepared_statementhttp://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) – TNK Mar 05 '13 at 02:35
  • oh i forgot to paste my query... – Clfc Developer Mar 05 '13 at 02:45

1 Answers1

0

PHP's implode() and explode() functions might come in handy. You can easily turn your POST or GET attribute, 'number', into a comma-separated list by using implode($_POST['number']). This would be easy to store in one MySQL field, maybe a column in your user table.

If you want users to edit the form later, render the checkboxes with a loop and add a "checked" attribute to each checkbox element whose name exists in 'exploded' list (array) retrieved from your database.

This is basically serialization/deserialization. There are other ways to do it including serialize(), unserialize() or json_encode(), json_decode(). But since your data seems to be easily modeled as a basic list, you can keep it even simpler.