2

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>";
}
Nicho247
  • 202
  • 1
  • 11
  • `$myRData = mysqli_query($con,$radiosql);` is failing. Therefore, you're passing false instead of a mysqli_result. Does `SELECT DISTINCT Breed FROM register` run if you manually run it? – Matt Feb 17 '16 at 03:59
  • Output `mysqli_error($con)` after every `mysqli_query` and see what they say. – Serge Seredenko Feb 17 '16 at 04:03
  • @mkaatman - no, this is query A and it is working correctly. I think this is true because that section of the page is displayed correctly. The error message is specific to the query called inside the if statement. – Nicho247 Feb 17 '16 at 04:13
  • @Nicho247 No one can look at your script and tell you exactly what's wrong. Line 59 doesn't even line up with your example. You're going to have to add output from `var_dump` for each of the variables that you're passing into `mysqli_fetch_array` for anyone to figure out what's going on. (myData, myRData) – Matt Feb 17 '16 at 04:17
  • @mkaatman thank you for your recommendations. i'm not attempting to submit an un-fixable puzzle. I just tried setting the first radio button not equal to null, but All instead, and then changed the condition of the if statement to be $choice = All. This made the error go away, but my table doesn't update when the radio buttons are clicked. :( – Nicho247 Feb 17 '16 at 04:23
  • Dupe: http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select – John Conde Feb 17 '16 at 12:38

2 Answers2

0

Change

 $sql = "SELECT * FROM register WHERE hounds = $choice";

to

 $sql = "SELECT * FROM register WHERE hounds = '". mysqli_real_escape_string($choice). "'";

Otherwise it's an SQL syntax error.

Sergey Vidusov
  • 1,342
  • 1
  • 7
  • 10
0

first change

if ($choice = '') {
    $sql = "SELECT * FROM register";
    $myData = mysqli_query($con,$sql);
    mysqli_query($con,$sql);
} 

into

if ($choice == '') { 
    $sql = "SELECT * FROM register";
    $myData = mysqli_query($con,$sql);
    mysqli_query($con,$sql);
} 

and before while loop you must check num_rows > 0

$rowcount=mysqli_num_rows($myData);
 if($rowcount > 0 )
  {
while ($row = mysqli_fetch_array($myRData,MYSQL_ASSOC)){
  $myRarray[] = $row;
}

} 
shubham715
  • 3,324
  • 1
  • 17
  • 27
  • thanks, I figured out the issue was with the $choice = ' '. Your answer attempts to fix it by == instead. Going to regard this as a working solution. thank you again. – Nicho247 Feb 17 '16 at 04:31