使用php和ajax从数据库表中选择单个字段会将错误显示为未定义的索引

时间:2015-04-06 10:08:50

标签: php jquery mysql web-services

 $(文件)。就绪(函数(){     $( '#显示')。点击(函数(){         $就({             键入: “GET”,             网址:“new1.php”             数据类型: “HTML”,         异步:“假”,             成功:功能(响应)         {                 $( “#秀”)HTML(响应)。           }        });     }); });
</head>

<input type="text" name="mono" method="post" class="tbox" >
<input type="button" name="submit" id="display" value="check">

<div id="show"> </div>

new1.php

<?php

    $username = "root";
    $password = "";
    $hostname = "localhost";


      $vah = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
 echo "Connected MySQL ";

  mysql_select_db("students",$vah);

 $pin =$_POST['mono'];
    //  $pin = isset($_POST['mono']) ? $_POST['mono']: '';


  $qry= "SELECT *FROM shop WHERE pin='$pin'";
  $res= mysql_query($qry);

  echo "<table border='1' >
        <tr>
        <td align=center> <b>pin</b></td>
        <td align=center><b>service</b></td>
        <td align=center><b>days</b></td>           
        </td> ";
        echo "</tr>";

        while($data = mysql_fetch_array($res))

        {   
            echo "<tr>";
            echo "<td align=center>$data[0]</td>";
            echo "<td align=center>$data[1]</td>";
            echo "<td align=center>$data[2]</td>";
            echo "</tr>";
        } 
        echo "</table>";
        ?>

1 个答案:

答案 0 :(得分:0)

我希望您在更新new.php之后解决错误,如下所示:

<?php

    $username = "root";
    $password = "";
    $hostname = "localhost";


      $vah = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
 echo "Connected MySQL ";

  mysql_select_db("students",$vah);

 $pin =$_POST['mono'];
    //  $pin = isset($_POST['mono']) ? $_POST['mono']: '';


  $qry= "SELECT *FROM shop WHERE pin='$pin'";
  $res= mysql_query($qry);

  $html = "<table border='1' >
        <tr>
        <td align=center> <b>pin</b></td>
        <td align=center><b>service</b></td>
        <td align=center><b>days</b></td>           
        </td> ";
        echo "</tr>";

        while($data = mysql_fetch_array($res))

        {   
            $html += "<tr>";
            $html += "<td align=center>$data[0]</td>";
            $html += "<td align=center>$data[1]</td>";
            $html += "<td align=center>$data[2]</td>";
            $html += "</tr>";
        } 
        $html += "</table>";
        echo $html;
        ?>