I've made an attempt at using radio buttons to control my mysql query, however I have hit some beginner roadblocks. I've either got minor typos or setup this up all wrong.
(1) I've set the displayed value of the radio buttons to be the result of query A by using an array. This is working fine.
(2) I've also set the array value to be the value of that radio button, while all radio buttons have the same name. I'm under the assumption this will allow the radio button name to serve as a variable to pass into query B, however I set the radio button name ('rbreed') to be equal to a variable and then used the variable($choice) in query B.
I noticed that when I view the page for the first time, no buttons are checked by default and it's likely the variable is null and therefore the query is null and the page doesn't work. Additionally, if I click on one of the buttons, it still yields no result and the selected button becomes un-selected.
I've created an if else statement to deal with this and change the query slightly, but am stuck with "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\NS\view.php on line 59"
Here is my code
<?php
$con = mysqli_connect("localhost","Nibbs","password");
if (!$con) {
die ("You have a connect error: " . mysqli_connect_error());
}
mysqli_select_db($con,"Dogs");
$choice = 'rbreed';
if ($choice = '') {
$sql = "SELECT * FROM register";
$myData = mysqli_query($con,$sql);
mysqli_query($con,$sql);
} else {
$sql = "SELECT * FROM register WHERE hounds = $choice";
$myData = mysqli_query($con,$sql);
mysqli_query($con,$sql);
}
$radiosql = "SELECT DISTINCT Breed FROM register";
$myRData = mysqli_query($con,$radiosql);
$myRarray = array();
while ($row = mysqli_fetch_array($myRData,MYSQL_ASSOC)){
$myRarray[] = $row;
}
echo "Select your type of hound:";
echo "<br />";
echo "<br />";
echo "<form action='' method='post'>";
echo "<input type='radio' name='rbreed' value=''>All";
echo "<input type='radio' name='rbreed' value=" . $myRarray[0]['Breed'] . ">" . $myRarray[0]['Breed'];
echo "<input type='radio' name='rbreed' value=" . $myRarray[1]['Breed'] . ">" . $myRarray[1]['Breed'];
echo "<input type='radio' name='rbreed' value=" . $myRarray[2]['Breed'] . ">" . $myRarray[2]['Breed'];
echo " "."<input type='submit' name='submit' value='Select' />";
echo "</form>";
echo "<br />";
echo "<table border=1>
<tr>
<th>Register ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Breed</th>
<th>Weight</th>
<th>Age</th>
<th>Sex</th>
</tr>";
while($record=mysqli_fetch_array($myData)){
echo "<tr>";
echo "<td><input type='text' name='reg_id' value='" . $record['reg_id'] . "'/> </td>";
echo "<td><input type='text' name='first_name' value='" . $record['First_Name'] . "'/> </td>";
echo "<td><input type='text' name='last_name' value='" . $record['Last_Name'] . "'/> </td>";
echo "<td><input type='text' name='breed' value='" . $record['Breed'] . "'/> </td>";
echo "<td><input type='int' name='weight' value='" . $record['Weight'] . "'/> </td>";
echo "<td><input type='int' name='age' value='" . $record['Age'] . "'/> </td>";
echo "<td><input type='text' name='sex' value='" . $record['Sex'] . "'/> </td>";
echo "</tr>";
}