php mysql checkbox and textboxes values insert into database - php

I have a db table called 'ice_flavours' with the columns: 'id', 'flavours', 'date', 'selected'.
and another table called 'ice_today' like: 'id', 'flavours', 'date', 'selected'.
Now, on the left of the page I want to call from 'ice_flavours' and print the entries in a form together with a checkbox.
On the right of the page it shall say SHOWCASE where I want the todays selected flavours to be shown and they shall just stay in there until the end of today´s date and then automatically be discarded. Also when they were selected to show in SHOWCASE, i want them to not show on the left side.
So, when I select the checkbox next to a flavour, the values 'id', 'flavours', 'date', 'selected' ought to be entered into 'ice_today' but for some reason just always the last row of the ice_flavours table is entered and i get the message "Duplicate entry '0' for key 'PRIMARY'"
Maybe this can all be done by just using a single db table but I havent figured it yet. Can someone help please
edit.php:
<?php ob_start();
include_once("dbinfo.inc.php");
include_once("config.php");
if(isset($_POST['submit'])){
$selected = $_POST['selected'];
$id = $_POST['id'];
$flavour = $_POST['flavour'];
$date = $_POST['date'];
if($_POST["submit"] == "submit") {
for($i=0;$i<sizeof($selected);$i++) {
if(!empty($flavour)) {
echo"";
echo "<pre>";
print_r($_POST);
echo "</pre>";
$query="INSERT INTO ice_today VALUES('$id','$flavour','$date','$selected[$i]')";
mysql_query($query) or die(mysql_error());
}
}
echo "Entry added";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Edit</title>
</head>
<body>
<form name="form1" id="form1" method="POST" action="edit.php">
<table border="1" width="200px">
<td>
<table border="1" width="200px">
<?php
$result = mysql_query("SELECT * FROM ice_flavours WHERE ('selected' != '1') ORDER BY flavour ASC");
$results = mysql_num_rows($result);
if ($results > 0)
{
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
$id=mysql_result($result,$i,"id");
$flavour=mysql_result($result,$i,"flavour");
?>
<tr>
<td align="center" valign="middle">
<?php echo $flavour; ?>
<input type="text" name="id" id="id" value="<?php echo $id; ?>">
<input type="text" name="flavour" id="flavour" value="<?php echo $flavour; ?>">
<input type="text" name="datum" id="datum" value="<?php echo date('Y-m-d'); ?>">
<input type="checkbox" name="selected[]" id="selected" value="<?php echo '1'; ?>">
</td></tr>
<?php
$i++;
}
?>
<input type="submit" value="submit">
<?php } ?>
</td>
<td>
<table border="1" width="200px">
<tr><td align="center" valign="middle">
SHOWCASE
</td></tr>
<?php
include_once("dbinfo.inc.php");
include_once("config.php");
$result2 = mysql_query("SELECT * FROM ice_today ORDER BY flavour ASC");
$results2 = mysql_num_rows($result2);
if ($results2 > 0)
{
$num2=mysql_numrows($result2);
$j=0;
while ($j < $num2)
{
$id=mysql_result($result2,$j,"id");
$flavour=mysql_result($result2,$j,"flavour");
?>
<tr><td align="center" valign="middle">
<?php echo $flavour; ?>
</td></tr>
<?php
$j++;
}
}
echo '</td></tr></table>';
?>
</body>
</html>
<?php ob_end_flush(); ?>

Please take a look at the MySQL Reference Manual. You need to specify each column in your INSERT statement:
$query="INSERT INTO ice_today(id, flavours, date, selected) VALUES('$id','$flavour','$date','$selected[$i]')";
I also suggest you to not use the original MySQL-extension anymore, as it is deprecated as of PHP 5.5.x. When using another MySQL extension (MySQLi or PDO for instance: mysqli or PDO - what are the pros and cons?) you can also use prepared statements to become safe against SQL injections (which you aren't right now).
POST-values can be manipulated and as you just feed them into your query via string concatenation, you're not safe against them.

If the field for ID is an autoincrement field, you need to leave it off the insert because the db server will determine the value itself. You should list the fields being inserted, and leave that one off:
$query="INSERT INTO ice_today (flavours, date, selected) VALUES('$flavour','$date','$selected[$i]')";
Listing the fields you are inserting to also prevents insert statements from breaking when you add new fields later. So its good practice to start now.

There are lots of issues in your code.
You text boxes having same name with different values if there are two flavors.
Therefore flavor or id will be always from last row.
You can do something like this.
1.Replace your text boxes with this,
<input type="text" name="id_<?php echo $id; ?>" value="<?php echo $id; ?>">
<input type="text" name="flavour_<?php echo $id; ?>" value="<?php echo $flavour; ?>">
<input type="text" name="datum_<?php echo $id; ?>" value="<?php echo date('Y-m-d'); ?>">
<input type="checkbox" name="selected[]" value="<?php echo $id; ?>">
2.Replace your top if block (i.e. after submit)
if (isset($_POST['submit']) && $_POST["submit"] == "submit") {
$selected = $_POST['selected'];
for($i=0; $i < sizeof($selected); $i++) {
$id = $selected[$i];
$flavour = $_POST['flavour_' . $id];
$date = $_POST['date_' . $id];
$query="INSERT INTO ice_today VALUES('$id','$flavour','$date','$id')";
mysql_query($query) or die(mysql_error());
}
}

Related

looped list always inserts first in list using checkboxes

I have a list that is populated from a database table as below.
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td>
<input type="checkbox" name="name[]" value="<?php echo
$row['AnalysisID']; ?>">
<?php echo $row["CustomerName"]; ?>
<input type="hidden" name="Site[]" value="<?php echo $row["Site"]; ?>">
<input type="hidden" name="Unit[]" value="<?php echo $row["Unit"]; ?>">
</td>
<td><?php echo $row['Site']; ?></td>
<td><?php echo $row['Unit']; ?></td>
</tr>
<?php
}
?>
This displays the data perfectly, and in inspect all data is correct.
However I want to be able to check the various check-boxes and insert those rows to another table.
to achieve this I am using:
$counter = count($_POST["name"]);
for($x=0; $x<=$counter; $x++){
if(!empty($_POST["name"][$x])){
$PI_NO = mysql_real_escape_string($_POST["name"][$x]);
$CUSTOMER_NAME = mysql_real_escape_string($_POST["Site"][$x]);
$PI_ADDRESS = mysql_real_escape_string($_POST["Unit"][$x]);
$qry="INSERT INTO test1 (AnalysisID, Elem, Lube) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
}
}
The problem is it doesn't matter what rows I check it always inserts the first in the list, it inserts the correct number just from the start of the list.
For example if I check the last 4 in the list I get the first 3.
Any pointers would be a great help.
Checkbox values are only sent if the checkboxes are checked, so the index will be different in the $_POST["name"] and the other $_POST arrays.
(Unchecked checkbox inputs are seen as "unsuccessful" https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.2 )
Try adding the ID as index instead, then selected data will keep together.
(I renamed the name array to aid to better match the contents)
<td>
<input type="checkbox" name="aid[<?php echo $row['AnalysisID']; ?>]" value="1">
<?php echo $row["CustomerName"]; ?>
<input type="hidden" name="Site[<?php echo $row['AnalysisID']; ?>]" value="<?php echo $row["Site"]; ?>">
<input type="hidden" name="Unit[<?php echo $row['AnalysisID']; ?>]" value="<?php echo $row["Unit"]; ?>">
</td>
Then looping through the post data with foreach(). Only the checked ID's will be posted so no need for if (!empty() check.
if (isset($_POST["aid"])) { // to avoid php undefined notice if none selected
foreach ($_POST["aid"] as $id=>$value){ // the index is what we need, using $id
$PI_NO = mysql_real_escape_string($id);
$CUSTOMER_NAME = mysql_real_escape_string($_POST["Site"][$id]);
$PI_ADDRESS = mysql_real_escape_string($_POST["Unit"][$id]);
$qry="INSERT INTO test1 (AnalysisID, Elem, Lube) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
}
}
Also I strongly advice you to change from using deprecated mysql_* functions, use mysqli or PDO instead. Why shouldn't I use mysql_* functions in PHP?

Pass PHP array through Select Option Fields

I am writing a basic CMS system and have come across something which should be seemingly simple -but is beginning to frustrate me.!
I am trying to pass an array through a select option field to populate a list of categories in which I can save a post.
I have a 'posts' form which comprises of 3 fields. Title, content and Category ID (CatID).
When the user creates a post, they can select the category they wish to assign the post assigned to by using a drop down list - (this is populated by using a different form).
So the technical bit; -
MySQL DB:-
categories = catname (char60 PRIMARY), catid (INT10, AI)
posts = id (bigint20 PRIMARY), catid (int10 PRIMARY), title (text), content (varchar255)
Example of categories populates: catname = Home / catid = 1 ...etc
Output.php ;
<?php
function display_post_form($post = '') {
$edit = is_array($post);
?>
<form action="<?php echo $edit ? 'edit.php' : 'add.php' ; ?>" method="post">
<table border="0">
<tr>
<td> Title:</td>
<td> <input type="text" name="title" value="<?php echo $edit ? $post['title'] : '' ; ?>" size="60" /> </td>
</tr><tr>
<td> Content:</td>
<td> <textarea id="editor1" name="content" value="<?php echo $edit ? $post['content'] : '' ; ?>"> </textarea> </td>
</tr><tr>
<td> Category:</td>
<td><select name="catid">
<?php
$cat_array = get_categories($catid, $catname);
foreach($cat_array as $thiscat) {
echo "<option value=\"".$thiscat['catid']."\" ";
if (($edit) && ($thiscat['catid'] == $post['catid'])) {
echo " selected";
}
echo ">".$thiscat['catname']."</option>";
}
?>
</select>
</td>
</tr><tr>
<td> Button:</td>
<td <?php if (!$edit) { echo "colspan=2"; } ?> align="center">
<?php
if ($edit)
echo "<input type=\"hidden\" name=\"_id\" value=\"". $post['id'] ."\" />";
?>
<input type="submit" value="<?php echo $edit ? 'Update' : 'Add' ; ?> Post" />
</form></td>
</td>
</tr>
</table>
</form>
<?php
}
?>
Functions.php ;
function get_categories($catid, $catname) {
$conn = db_connect();
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL " .mysqli_connect_error();
}
$sql = "SELECT catname, catid FROM categories";
$result = mysqli_query($conn, $sql) or die(" Could not query database");
while($row = mysqli_fetch_assoc($result)) {
printf("\n %s %s |\n",$row["catname"],$row["catid"]);
}
mysqli_close($conn);
}
I am able to call in the 'get_cattegories()' function which generates a flat data of categories and their respective id's. I then combined this with the Select Option Field in the Output.php file and it doesn't generate anything.
Can anyone give some useful tips or advice? Many thanks :)
You are not returning the array but printing a string to the output. Change printf to return:
function get_categories($catid, $catname) {
$conn = db_connect();
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL " .mysqli_connect_error();
}
$sql = "SELECT catname, catid FROM categories";
$result = mysqli_query($conn, $sql) or die(" Could not query database");
$categories = array();
while($row = mysqli_fetch_assoc($result)) {
$categories[] = $row;
}
mysqli_close($conn);
return $categories;
}
Also I agree for the comments to your question. The arguments are useless.
You also may refactor the code, actually... alot. Move the mysql_connect() to the other place, probably at the beginning of your script.
I suggest to use some frameworks. I think KohanaPHP will be a good start. You will learn about architecture and some design patterns. Keep the good work and improve your skills ;-)

how to populate a dropdown box in a edit form in php

UPDATE TO Below:
I changed the code a bit and am having a bit of success but I am still off on one thing. I changed the line
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
to
<option value="<?php echo $brokerlistrow[name]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
Now it shows the correct select name and actually saves a changed value in the database however, instead of saving the name id number, it saves the actual name. a print_r of $row gives me:
Array ( [0] => 4 [id] => 4 [1] => 6 [brokername] => 6 [2] => Kims Boat [boatname] => Kims Boat )
The correct brokerlist.id for that selected user is 6 but instead of saving 6 to the boatlist.name it save the brokerlist.name field instead of the brokerlist.id.
What can I change to have it save the brokerlist.id as opposed to the physical name? :)
Thank you in advance!
(here is the new edit page code just in case:
<?php
include_once('db.php');
if(isset($_GET['edit']))
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM boatlist WHERE id='$id'");
$row= mysql_fetch_array($res);
$brokerlistquery = mysql_query("SELECT * FROM brokerlist");
print_r($row);
}
if( isset($_POST['newbrokername']) && isset($_POST['newboatname']))
{
$newbrokername = $_POST['newbrokername'];
$newboatname = $_POST['newboatname'];
$id= $_POST['id'];
$sql = "UPDATE boatlist SET brokername='$newbrokername', boatname='$newboatname' WHERE id='$id'";
$res= mysql_query($sql) or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=testaddboat.php'>";
}
?>
<table class="centerwithroom">
<form action="testeditboat.php" method="POST">
<tr>
<td>New Broker Name </td><td>
<select name='newbrokername'>
<?php
while ($brokerlistrow = mysql_fetch_array($brokerlistquery)) {
?>
<option value="<?php echo $brokerlistrow[name]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
</td>
<tr>
<td>Boat Name:</td><td><input type="text" name="newboatname" value="<?php echo $row[boatname]; ?>"/></td>
</tr>
</tr>
<td></td><td><input type="submit" value=" Update "/></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row[id]; ?>"/>
</form>
</table>
(ORIG POST Starts Here)
This is my first post and I will try to be as thorough as possible. This is just a rudimentary form and I have not added any security in it at all as I am just trying to make it work, then get to the rest :) Being new to programming my code may look bad to some of you experts and I would love any examples you could give.
On to the project. I have two mysql tables, one called brokerlist with two fields id and name. the second table is called boatlist with three fields id, brokername, and boatname. The brokername field contains the value of the brokerlist.id field and a text entered boatname.
I have created php add forms to add data to both tables that are working properly. I have created a php edit form to edit the brokerlist table that works properly. My problem lies in the form I call testeditboat.php. What I am trying to do is allow the user to change the field brokername in the testeditboat.php form with a dropdown box that populates from the mysql table brokerlist but also uses the select selected of the current entry.
Where my form currently stands is it submits the request and will change any value in the boatname field, but will not update the brokername field.
Thank you all in advance very much for your help, I hope I have been descriptive enough.
The code for my testeditboat.php form is as follows:
<?php
include_once('db.php');
if(isset($_GET['edit']))
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM boatlist WHERE id='$id'");
$row= mysql_fetch_array($res);
$brokerlistquery = mysql_query("SELECT * FROM brokerlist");
}
if( isset($_POST['newbrokername']) && isset($_POST['newboatname']))
{
$newbrokername = $_POST['newbrokername'];
$newboatname = $_POST['newboatname'];
$id= $_POST['id'];
$sql = "UPDATE boatlist SET brokername='$newbrokername', boatname='$newboatname' WHERE id='$id'";
$res= mysql_query($sql) or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=testaddboat.php'>";
}
?>
<table class="centerwithroom">
<form action="testeditboat.php" method="POST">
<tr>
<td>New Broker Name </td><td>
<select name='newbrokername'>
<?php
while ($brokerlistrow = mysql_fetch_array($brokerlistquery)) {
?>
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
</td>
<tr>
<td>Boat Name:</td><td><input type="text" name="newboatname" value="<?php echo $row[boatname]; ?>"/></td>
</tr>
</tr>
<td></td><td><input type="submit" value=" Update "/></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row[id]; ?>"/>
</form>
</table>
Change the following line:
$row= mysql_fetch_assoc($res); //this fetches the array in associative mode
Change the following line:
<option value="<?php echo $row[brokername]; ?>" <?php if ($row[brokername] == $brokerlistrow[name]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
You where comparing $row[brokername] == $brokerlistrow[id] which seems to be incorrect. You have to compare $row[brokername] == $brokerlistrow[name]
Note: Stop using mysql_* extensions use mysqli_ instead*
by changing the row to
<option value="<?php echo $brokerlistrow[id]; ?>" <?php if ($row[brokername] == $brokerlistrow[id]) { echo selected; } ?> ><?php echo $brokerlistrow[name]; ?></option>
<?php } ?> </select>
it now saved the correct value and shows the correct focus in the edit dropdown box.
Thank you all for your guidance! :)

Need help for database update query in php form - Joomla database

Let's say I have a table with 10 records and I want to take name, lastname and rank from those 10 records. First I do something like this:
<?php // DATABASE SELECT QUERY
$db =& JFactory::getDBO();
$query="SELECT name, lastname, rank
FROM table
ORDER BY rank ASC";
$db->setQuery($query);
$rows = $db->loadObjectList(); ?>
Then, I add some fields in to my form that contain table's values, so I can edit them through form:
<form action="#" method="post" name="form">
<table><?php $count = 0; while ($count < 10){
$name = $rows[$count]->name;
$lastname = $rows[$count]->lastname;
$rank = $rows[$count]->rank; ?>
<tr>
<td><input name="name" value="<?php echo $name ?>" type="text" /></td>
<td><input name="lastname" value="<?php echo $lastname ?>" type="text" /></td>
<td><select name="rank">
<option value="<?php echo $rank ?>"><?php echo $rank ?></option>
<option disabled="disabled" value="...">...</option>
<?php for ($i = 0; $i <= 100; $i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select></td>
</tr><?php $count++;}?>
</table>
<input class="something" name="updatemod" type="submit" value="UPDATE" />
</form>
Next, before Select query, I have add an update query using this method below, so when I press the update button, update my DB:
// DATABASE UPDATE QUERY
if (isset($_POST['updatemod']) or isset($_GET['updatemod'])){
$db =& JFactory::getDBO();
$query = "UPDATE table
SET name = '".$_POST["name"]."',
SET lastname = '".$_POST["lastname"]."',
SET rank = '".$_POST["rank"]."'
";
$db->setQuery($query);
$db->query();}
But... Nothing is working!!! I have done exactly the same thing for an other form and it's working perfect! The only difference between those two forms, is that I am not using this while loop at the other form. So, maybe it has to do with this or something??? I don't know, at this point is where my knowledge confused, so I need your help!
I think you are trying to update all the rows in your table.
If that is the case,You need to do something like this.
<?php // DATABASE SELECT QUERY // also you should select your unique id field
$db =& JFactory::getDBO();
$query="SELECT name, lastname, rank,id
FROM table
ORDER BY rank ASC";
$db->setQuery($query);
$rows = $db->loadObjectList(); ?>
In your form you are placing element name as single but your requirement is to edit all the rows at one click you should use input names as array.Also here you have to introduce a new id field too for update condition
<form action="#" method="post" name="form">
<table><?php $count = 0; while ($count < 10){
$name = $rows[$count]->name;
$lastname = $rows[$count]->lastname;
$rank = $rows[$count]->rank;
$id = $rows[$count]->id; ?>
<tr>
<td><input name="uid[]" value="<?php echo $id?>" type="hidden" />
<input name="name[]" value="<?php echo $name ?>" type="text" /></td>
<td><input name="lastname[]" value="<?php echo $lastname ?>" type="text" /></td>
<td><select name="rank[]">
<option value="<?php echo $rank ?>"><?php echo $rank ?></option>
<option disabled="disabled" value="...">...</option>
<?php for ($i = 0; $i <= 100; $i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select></td>
</tr><?php $count++;}?>
</table>
<input class="something" name="updatemod" type="submit" value="UPDATE" />
</form>
Also in your Update Query also required loop and where cluase
// DATABASE UPDATE QUERY
if (isset($_POST['updatemod']) or isset($_GET['updatemod'])){
$db =& JFactory::getDBO();
$total_rows = sizeof($_POST["uid"]);
for($i =0; $i<$total_rows;$i++){
$query = "UPDATE table
SET name = '".$_POST["name"][$i]."',
lastname = '".$_POST["lastname"][$i]."',
rank = '".$_POST["rank"][$i]."'
WHERE id = '".$_POST["uid"][$i]."'
";
$db->setQuery($query);
$db->query();
}
}
I think this will solve your problem.
Here i just mentioned the "id" as your unique key of the table that may differ.But you will get the idea , i hopes
But this is a worst case update condition Bcoz you just imagine the table with 1000 rows.
It will take too long to get the result.
Try to update single row with proper method.It is the best method.
Hope this may help you.

Check a check box if it's value is in DB. PHP

I have a loop of checkboxes from a MySql table "exercise" in a form as shown below:
<form id="form1" name="form1" method="POST" action="insertDrills.php">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td>
<input type="checkbox" name="drillSelect[]" value="<?php echo $row_Recordset1['drillID']; ?>" id="drillSelect" />
<?php echo $row_Recordset1['rubrik']; ?>
</td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<tr>
<td colspan="2">
<input name="spelarID" type="hidden" id="spelarID" value="<?php echo $row_rsSpelare['id']; ?>" />
<input name="date" type="hidden" id="date" value="<? print date("Y-m-d h:i:s"); ?>" />
<input type="submit" name="submit" id="submit" value="Välj övningar" />
<input type="button" value="Avbryt" onclick="window.close();"></button>
</td>
</tr>
</table>
</form>
The value from the hidden fied "spelarID" along with the value from the checkbox array are inserted to a look-up table "exercise_spelare_ref" with this:
<?php
$id = $_POST['spelarID'];
$drills = $_POST['drillSelect'];
$date = $_POST['date'];
$inserts = array();
foreach ($drills as $drills)
$inserts[] = "('$id','$drills','','$date')";
$query = "REPLACE INTO exercise_spelare_ref VALUES ". implode(", ", $inserts);
//echo "query = $query"; // for debugging purposes, remove this once it is working
mysql_query($query) or die(mysql_error());
?>
How can I make values that are in the look-up table marked as "checked" in the form?
There are a solution here https://stackoverflow.com/a/4069477 written by Martin Bean but I cant get it to work?
I have been stuck here for ever, hope anyone can help me out here!
I tried Martin Beans script like:
$uid = $row_rsSpelare['id']; // your logged in user's ID
$exercise = array();
// get an array of exercise
$sql = "SELECT drillID FROM exercise";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
$exercise[$row->id] = $row->drillID;
}
// get an array of exercise user has checked
$sql = "SELECT DISTINCT drillID FROM exercise_spelare_ref WHERE id = '$uid'";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
$checked[] = $row->drillID;
}
// this would be templated in a real world situation
foreach ($exercise as $id => $drillID) {
$checked = "";
// check box if user has selected this exercise
if (in_array($checked, $id)) {
$checked = 'checked="checked" ';
}
echo '<input type="checkbox" name="drillSelect[]" value="'.$id.'" '.$checked.'/>';
}
But getting a warning:
Warning: in_array() expects parameter 2 to be array, string given in /customers/b/d/e/teeview.se/httpd.www/analys_kund/selectDrills.php on line 158
Line 158 is:
if (in_array($checked, $id)) {
Well, you will need to SELECT the items from the table, store them into an array, and if the current checkbox's ID matches something in the array, add the checked attribute to it in the HTML.

Categories