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 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab