douxuexiao1748 2015-10-24 00:57
浏览 29
已采纳

使用Ajax和mysql检索数据

I'm trying to retrieve data from multiple table. So when the user clicks and select an item php file to retrieve the data from the corresponding table.

this is my first try on Ajax used w3 school code and trying to modify, I guess the way I'm using if condition is the problem? Before I try with the multiple table it worked.

My Script

 <script>
function showUser(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "Please Select type of users to view";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","get_users.php?q="+str,true);
    xmlhttp.send();
  }
}
</script>

My Form

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Staff</option>
<option value="2">Admin</option>
<option value="3">Customers</option>
</select>
</form>

My PHP Page

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','X','X','X');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con);

if ($q='Admin')
{

$sql="SELECT * FROM admin";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Email</th>
<th>Name</th>
<th>Mobile</th>
<th>Address</th>
<th>Password</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";

else if ($q='Staff')
 {
echo "This is from Database B
}

else
{
echo "This is from database C";
}

mysqli_close($con);
?>
  • 写回答

1条回答 默认 最新

  • douluhaikao93943 2015-10-24 01:09
    关注

    You're using $q = intval($_GET['q']); with a string value for your assignments.

    intval() "Get the integer value of a variable"

    <option value="1">Staff</option>
    <option value="2">Admin</option>
    <option value="3">Customers</option>
    

    and using an assignment rather than a comparison for both if ($q='Admin') and else if ($q='Staff') both being strings, as opposed to integers in your options.

    which should read as and with 2 equal signs if ($q=='2') and else if ($q=='1') in order to match your select's numerical value options.

    Or, change them accordingly with your options to read as Staff and Admin (and Customers) in the values, while removing the intval() from the GET array; the choice is yours.

    You also don't need mysqli_select_db($con); since you've declared your 4 parameters in your initial connection and would fail for another reason; technically you didn't select a database for it.

    You're also missing a quote and semi-colon in (a parse syntax error)

    echo "This is from Database B
    

    which should read as

    echo "This is from Database B";
    

    Footnotes:

    The page which I believe you based yourself on, seems to be http://www.w3schools.com/php/php_ajax_database.asp

    If so, then I suggest you follow their example completely and modify it slowly to fit your needs. I myself have used that demo (in the distant past) before with success.

    Their example uses a WHERE clause also.

    $sql="SELECT * FROM user WHERE id = '".$q."'";
    

    Edit: Just for the record, there is a missing brace for this condition:

    if ($q='Admin')
    {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败