Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么这个sql代码不工作_Php - Fatal编程技术网

Php 为什么这个sql代码不工作

Php 为什么这个sql代码不工作,php,Php,$ss是字符串吗?你要这个吗 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content=

$ss是字符串吗?你要这个吗

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>اول اسكربت باذن الله</title>
</head>

<body>
<table width="100%" border="1">
 <tr>
    <td>name</td>
    <td>number</td>
    <td>math</td>
    <td>arab</td>
    <td>history</td>
    <td>geo</td>

  </tr>


<?php


require_once "conf.php";

$sql2=("SELECT * FROM student WHERE snum = $ss");
$rs2 = mysql_query($sql2) or die(mysql_error());
$num = mysql_num_rows($rs2);
$ss= $_POST["ss"];

if (empty($ss))
{ echo "please write your search words";}
else if ($num < 1 )   {
   echo  "not found any like ";

}else {
$sql=("SELECT * FROM student WHERE snum = $ss ");
$rs = mysql_query($sql) or die(mysql_error());



while($data=mysql_fetch_array($rs)){
$name=$data["sname"];
$number=$data["snum"];
$math=$data["math"];
$arab=$data["arab"];
$history=$data["history"];
$geo=$data["geo"];





echo"
  <tr>
    <td>$name</td>
    <td>$number</td>
    <td>$math</td>
    <td>$arab</td>
    <td>$history</td>
    <td>$geo</td>
  </tr>
";


}
 };
?>
 </table>
</body>
</html>

在查询退出之前,将不存在的变量$ss传递给查询:

$sql2=("SELECT * FROM student WHERE snum = '$ss'");
试试这个:

$sql2=("SELECT * FROM student WHERE snum = $ss"); // <-- problem here
$rs2 = mysql_query($sql2) or die(mysql_error());
$num = mysql_num_rows($rs2);
$ss= $_POST["ss"];

不能使用未初始化的变量。在您的例子中,当您使用$ss在$sql2中构建查询时,它可能是未定义的。这将导致无效的SQL语句,因为在=运算符之后没有任何内容

请尝试以下方法:

require_once "conf.php";

$ss= $_POST["ss"];

if (empty($ss))
{ echo "please write your search words";}
else if ($num < 1 )   {
   echo  "not found any like ";

}else {
$sql=("SELECT * FROM student WHERE snum = $ss ");
$rs = mysql_query($sql) or die(mysql_error());

// and more code...
我认为$ss=$_POST[ss]; 应该先走

$sql2=从学生中选择*,其中snum=$ss

是认真的吗

第一:php是一种简单的编程语言。它按照你给它的顺序执行你给它的任何东西。这就是为什么您尝试在查询中使用的$ss变量在尝试使用它时不存在的原因。您应该在使用它之前指定它的值

现在,让我们开始成为一个混蛋。 $\u POST['ss']由您的用户提供。不要相信它。永远不要相信用户的输入!他们想控制你的服务器,这样他们就可以找到你并绑架你以索取赎金。 因此,在未检查其值的情况下,不要在查询中使用它。想象一下,如果用户发送$_POST['ss']='1或1';
处理这类问题的最佳方法是使用参数化查询和或。

no它是num$sql2=SELECT*FROM student,其中snum=$ss;将SQL\u MODE=NO\u AUTO\u VALUE\u设置为0;-数据库:自定义-表的表结构学生创建表如果不存在学生id int11 NOT NULL自动递增,sname varchar100 NOT NULL,snum int15 NOT NULL,数学int10 NOT NULL,阿拉伯int10 NOT NULL,历史int10 NOT NULL,地理int10 NOT NULL,主键id ENGINE=MyISAM DEFAULT CHARSET=cp1256 auto_递增=3;你看过Windows 1256中的字符了吗?@Gumbo:编辑我的答案,我想它也支持阿拉伯语。感谢+1看到SQL注入易受攻击的代码,我很恼火。
require_once "conf.php";
if (!isset($_POST["ss"])) {
    echo "please write your search words";
} else {
    $ss = $_POST["ss"];
    $query = "SELECT * FROM student WHERE snum = '".mysql_real_escape_string($ss)."'";
    $result = mysql_query($query) or die(mysql_error());
    if (mysql_num_rows($result) == 0) {
        echo  "not found anything like ".htmlspecialchars($ss);
    } else {
        while ($data=mysql_fetch_array($result)) {
            // …
        }
    }
}