【问题标题】:having trouble in mysql if statement在 mysql if 语句中遇到问题
【发布时间】:2010-04-22 18:16:08
【问题描述】:

我只是想简化我之前所做的事情,为要列出的所有数据设置多个 php 文件。 这是我的html表单:

                                     <table   border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D3D3D3">
<tr>
<form name="formcheck" method="post" action="list.php" onsubmit="return formCheck(this);">
<td>
<table  border="0" cellpadding="3" cellspacing="1" bgcolor="">
<tr>

<td  colspan="16" height="25"  style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="3">List Students</td>
</tr>
<tr>
<td width="30" height="35"><font size="3">*List:</td>
<td width="30"><input name="specific" type="text" id="specific" maxlength="25" value="">
</td>

<td><font size="3">*By:</td>
<td>
    <select name="general" id="general">
        <font size="3">
        <option>Year</option>
        <option>Address</option>


    </select></td></td>
    </tr>
    <tr>
   <td width="10"><input  align="right" type="submit" name="Submit" value="Submit" > </td>
</tr>
</form>
</table>

这是表单操作:

<?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("school", $con);

$gyear= $_POST['general'];



if ("YEAR"==$_POST['general']) {
$result = mysql_query("SELECT * FROM student WHERE YEAR='{$_POST["specific"]}'");


echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
<th>LASTNAME</th>
<th>FIRSTNAME</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
   echo "<tr>";
   echo "<td>" . $row['IDNO'] . "</td>";
  echo "<td>" . $row['YEAR'] . "</td>";
  echo "<td>" . $row['LASTNAME'] . "</td>";
    echo "<td>" . $row['FIRSTNAME'] . "</td>";


  echo "</tr>";
  }
echo "</table>";
}

mysql_close($con);
?>

请帮忙,我如何将 YEAR(mysql 数据库中的列)和选项框(一般)等同起来。

if ("YEAR"==$_POST['general']) 

如果我错了,请纠正我。

【问题讨论】:

  • 你期望 $_POST['general'] 是什么?举一些恰当的例子。如果您正在寻找日期,请执行谷歌搜索“date function php”。
  • 希望你的 mysql_connect 细节不是真实的
  • 你应该永远在你的脚本中使用 root MySQL 帐户进行查询,你应该为此创建一个单独的用户帐户,并且只使用 root 帐户来维护数据库。跨度>

标签: php mysql html


【解决方案1】:
<option>Year</option>

你只需要把它改成

<option value="YEAR">Year</option>

也一定要阅读这个页面:http://en.wikipedia.org/wiki/SQL_injection,否则你的代码很容易受到讨厌的攻击。

[编辑:还请注意您最初问题的 cmets,在网站上发布您的真实 root 用户名/密码不是一个好主意。]

【讨论】:

    【解决方案2】:
    if ("YEAR"==$_POST['general']) {
      $result = mysql_query("SELECT * FROM student WHERE date_format(YEAR, '%Y')='{$_POST["specific"]}'");
    

    【讨论】:

    • 您可以只使用YEAR(`YEAR`) 而不是DATE_FORMAT()。更具可读性,但在其他方面完全等效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多