将单选按钮值传递给mysql查询

时间:2016-02-17 03:55:25

标签: javascript php html mysql ajax

我尝试使用单选按钮来控制我的mysql查询,但是我遇到了一些初学者的障碍。我要么犯了小错误,要么设置错误。

(1)我通过使用数组将单选按钮的显示值设置为查询A的结果。这很好。

(2)我还将数组值设置为该单选按钮的值,而所有单选按钮都具有相同的名称。我假设这将允许单选按钮名称作为变量传递到查询B,但是我将单选按钮名称('rbreed')设置为等于变量然后使用变量($ choice) )在查询B中。

我注意到,当我第一次查看页面时,默认情况下不会检查任何按钮,并且可能是变量为null,因此查询为空,页面不起作用。此外,如果我单击其中一个按钮,它仍然不会产生任何结果,并且所选按钮将被取消选中。

我已经创建了一个if else语句来处理这个并略微更改查询,但我坚持使用“警告:mysqli_fetch_array()期望参数1为mysqli_result,在C:\ wamp \ www \ NS \中给出布尔值第59行的view.php“

这是我的代码

<?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>";
}

2 个答案:

答案 0 :(得分:0)

更改

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

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

否则,这是一个SQL语法错误。

答案 1 :(得分:0)

首先改变

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

进入

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

在while循环之前,你必须检查num_rows&gt; 0

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

}