根据表单提交值获取Mysql表行数据

时间:2013-03-31 11:17:25

标签: php sql forms

我需要根据表单提交值获取行。

这是我的表格

<form name="choose" method "post" t" action="search.php">
    <table>
    <tr>
        <tr>
            <td height="3">
            </td>
        </tr>
        <td width="60">
            <font1>Prof</font1>
        </td>
        <td>
            <select name proffession on>
                <option value=""></option>
                <option value="doctor"><font4>Doctor</font></option>
                <option value="designer">Designer</option>
            </select>
        </td>
    </tr>
    <tr>
        <tr>
            <td height="3">
            </td>
        </tr>
        <td width="60">
            <font1>Source</font1>
        </td>
        <td>
            <select name source>
                <option value=""></option>
                <option value="x"><font4>X</font></option>
                <option value="y">Y</option>
                <option value="z">Z</option>
            </select>
        </td>
    </tr>
    <tr>
        <tr>
            <td height="3">
            </td>
        </tr>
        <td width="60">
            <font1>Location</font1>
        </td>
        <td>
            <select name location on>
                <option value=""></option>
                <option value="bangalore">Bangalore</option>
                <option value="delhi">Delhi</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            <input name=look type=submit value=submit>
        </td>
    </tr>
</form>

如果有任何空字段提交,我需要获取除该列之外的行。 这是我的search.php

 <?php
mysql_connect("localhost","root","");//database connection
mysql_select_db("alldata");
$qry = "SELECT * FROM data WHERE location LIKE '" . mysql_escape_string($_POST['location']) . "' And proffession LIKE '" . mysql_escape_string($_POST['proffession']) . "' And source LIKE '" . mysql_escape_string($_POST['source']) . "'";

$res = mysql_query($qry);
function mysql_fetch_all($res) {
   while($row=mysql_fetch_array($res)) {
       $return[] = $row;
   }
   return $return;
}
function create_table($dataArr) {
    echo "<tr>
"; for($j = 0; $j < count($dataarr); $j++) { echo "<td>".$dataArr[$j]."
</td>
"; } echo "
</tr>
"; } $all = mysql_fetch_all($res); echo "
<table class='data_table'>
"; for($i = 0; $i < count($all); $i++) { create_table($all[$i]); } echo "</table>";
?>

但是这个脚本无法为我提供解决方案。 请帮忙

3 个答案:

答案 0 :(得分:1)

 1.Correct your function mysql_fetch_all($res). There is no query
           inside the function. 

 2. Deprecated: mysql_escape_string(): This
           function is deprecated; use mysql_real_escape_string()

 3. Correct: <select name source> to: <select name="source"> and <select name location on> to <select name="location"> and <input name=look type=submit value=submit> to <input name="look" type="submit" value="submit"> and delete t" from choose form AND <form name="choose" method="post"  action="search.php">

答案 1 :(得分:0)

首先,您不应该使用mysql_*函数,请参阅大红框here。请考虑改为使用PDOMySQLi

其次,您似乎缺少某些=,例如method="post"您可能希望通过var_dump($_POST)

检查您是否正确接收了POST值

第三,要排除未提交的值,您可以根据这些值构造查询字符串。类似的东西:

$qry = "SELECT * FROM data WHERE ";
if($_POST['location']) {
   $qry .= 'LIKE "%'. mysql_real_escape_string($_POST['location']) .'%"';
}
// etc...

答案 2 :(得分:0)

$qry = "SELECT * FROM data WHERE location LIKE '%" . mysql_escape_string($_POST['location']) . "%' And proffession LIKE '%" . mysql_escape_string($_POST['proffession']) . "%' And source LIKE '%" . mysql_escape_string($_POST['source']) . "%'";