Sorry, I have one more question and instead of checkboxes it is with drop boxes.
Your solution for checking checkboxes is working flawlessly. I am using 1 and 0 in the database for true and false respectively and using the ternary operator and everything is working that way.
But, now I’ve implemented a drop box in one of the forms that gets populated by one of the tables in the database.
I’ve implemented a solution that is working, I just want to know if this is the proper way to do it.
Here is my code example of how it is:
Note: this is an example and not escaped strictly for readability here.
$sql = "SELECT contractid, contractname, cropid, cropname
FROM contracts
INNER JOIN crops ON contractcrop = cropid
WHERE contractid = $id";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
$contract = array('contractid' => $row['contractid']
'contractname' => $row['contractname']
'cropid' => $row['cropid']
'cropname' => $row['cropname']);
$sql = "SELECT cropid, cropname FROM crops";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result)) {
$crops[] = array('cropid' => $row['cropid'],
'cropname' => $row['cropname'],
'selected' => ($row['cropid'] == $contract['cropid']) ? ' selected="selected" ' : '');
}
So, with the above code I am getting two arrays. One is the single contract I’m requesting with the crop name from a relational link. However, I need to also pull the crop ID so that I can use it later in the next SQL statement. The second array is the crop table in its full which will populate the drop down box. Here I am checking if the cropid is equal to the relational link in the contract table to contractcrop and if it is, then $crops[‘selected’] is assigned a value of selected=“selected”.
Question: Is this the proper way to check if the item in the database is already selected?
My HTML template looks like this:
<tbody>
<tr>
<td><?php echo $contract['contractname']; ?></td>
<td><input type="hidden" name="contractid" value="<?php echo $contract['contractid']; ?>" /><?php
echo $contract['contractid']; ?></td>
<td><select name="cropid">
<?php foreach ($crops as $crop): ?>
<option value="<?php echo $crop['cropid']; ?>"<?php
echo $crop['selected']; ?>><?php
echo $crop['cropname']; ?></option>
<?php endforeach; ?>
</select></td>
</tr>
</tbody>
To process the form is quite simple:
$id = $_POST['contractid'];
$crop = $_POST['cropid'];
$sql = "UPDATE contracts SET contractcrop = {$crop} WHERE contractid = {$id}";
And here is a screenshot to help explain what it is I’m doing:
I really appreciate all the help. I’m just really OCD about doing things the right way, thanks.