【问题标题】:Error in PHP search functionPHP搜索功能出错
【发布时间】:2011-09-15 06:18:18
【问题描述】:

我收到错误:

Warning: ociexecute() [function.ociexecute]: ORA-00936: missing expression in     /home/sjrem/public_html/ssss.php on line 31

Warning: ocifetch() [function.ocifetch]: ORA-24374: define not done before fetch or execute and fetch in /home/sjrem/public_html/ssss.php on line 49

我想在 oracle 的数据库中搜索 VIN 号。我做错了什么?

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php

/* Set oracle user login and password info */
$dbuser = "sjrem"; /* your deakin login */
$dbpass = "shn"; /* your oracle access password */
$db = "SSID";
$connect = OCILogon($dbuser, $dbpass, $db);

if (!$connect) {
echo "An error occurred connecting to the database";
exit;
}

/* build sql statement using form data */
$query = "SELECT * from cars WHERE vin=$VIN";

/* check the sql statement for errors and if errors report them */
$stmt = OCIParse($connect, $query);
//echo "SQL: $query<br>";
if(!$stmt) {
echo "An error occurred in parsing the sql string.\n";
exit;
}
OCIExecute($stmt);?>



<h1 class="green">PHP and Oracle databases</h1>
<h4>Table: <em>Cars</em></h4>
<div align="center">
<table width="850" border="0" bgcolor="#339933" cellpadding="5" cellspacing="1">
<tr bgcolor="#006633">
<td width="75" style="color:#ffff99">Vin Number</td>
<td width="75" style="color:#ffff99">Car</td>
<td width="100" style="color:#ffff99">Colour</td>
<td width="75" style="color:#ffff99">Drivetrain</td>
<td width="75" style="color:#ffff99">Location</td>
</tr>
  <?php


while(OCIFetch($stmt)) {
// Start a row for each record
echo("<tr valign=top bgcolor=#ccffcc>");

$fg1 = OCIResult($stmt,"VIN"); 
echo("<td width=75>");  
echo ($fg1);
echo("</td>");

$fg2 = OCIResult($stmt,"CAR");
echo("<td width=75>");
echo ($fg2);
echo("</td>");

$fg3 = OCIResult($stmt,"COLOUR");
echo("<td width=75>");
echo ($fg3);
echo("</td>");

$fg4 = OCIResult($stmt,"DRIVETRAIN");
echo("<td width=75>");
echo ($fg4);
echo("</td>");

$fg5 = OCIResult($stmt,"LOCATION");
echo("<td width=75>");
echo ($fg5);
echo("</td>");

// End the row
echo("</tr>");
}
// Close the connection
OCILogOff ($connect);
?>

 </table>
</div>


</body>

</html>

【问题讨论】:

  • $VIN 的值是多少?是否应该将其表示为字符串:"SELECT * from cars WHERE vin='$VIN'"?顺便说一句:根据$VIN 的来源,一定要对其进行适当的消毒。

标签: php oracle


【解决方案1】:

如果 $vin 为空或未设置,则查询无效。如果 $vin 包含非数字字符的字符串,则查询也很可能无效。

您可以在值周围添加引号,但在这种情况下,您也需要对值本身进行转义。任何带有引号的搜索字符串都会使您的查询再次无效,并可能损坏您的数据库!如果我要搜索volvo'; delete from cars; --,您的查询会运行良好,但也会从您的表中删除所有值。这称为 sql 注入

解决此问题的最佳方法(尤其是在 Oracle 中)是为您的查询使用绑定参数。 PHP.net 上关于oci_bind_by_name 的主题中有一些示例应该可以帮助您进行操作。

【讨论】:

    【解决方案2】:

    如果你能告诉哪些行号在哪里会有帮助,但如果你用谷歌搜索错误,你会得到this page

    如果您的查询不好,您似乎会收到该错误?现在你有了这个:

    "SELECT * from cars WHERE vin=$VIN"
    

    例如,我从来没有看到 $VIN 被填满,所以这可能会转化为

    SELECT * from cars WHERE vin=
    

    这是无效的 SQL。此外,@jensgram 在评论中所说的:如果它是一个字符串,你应该像这样将它括起来:

        SELECT * from cars WHERE vin='$VIN'
    

    【讨论】:

    • 我附上了我的html代码,如果你可以看一下。 $VIN 应该从搜索框中抓取
    • 那么,$VIN 是什么?它是一个 ID(整数)吗?因为如果不是,您应该使用'。你试过了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2019-08-04
    • 2015-04-11
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多