2011-09-15 74 views
0

我得到错误:错误在PHP中的搜索功能

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> 
+0

'$ VIN'的价值是多少?它应该表示为一个字符串:'“选择*从汽车WHERE vin ='$ VIN'”'?顺便说一句:取决于'$ VIN'来自哪里,一定要正确消毒。 – jensgram

回答

0

它会帮助,如果你想告诉什么linenumbers这些地方,但如果你谷歌的错误你this page

看来,如果您的查询是不好的,你得到这个错误?现在你有这样的:

"SELECT * from cars WHERE vin=$VIN" 

举例来说,我从来没有看到漫天的$VIN,所以这可能转化为

SELECT * from cars WHERE vin= 

这是无效的SQL。此外,什么@jensgram说,在注释:如果它是一个字符串,你应该附上它像这样:

SELECT * from cars WHERE vin='$VIN' 
+0

感谢您的回复,http://pastebin.com/0LJ84yVA – David

+0

我附上我的html代码,如果你可以看看它。 $ VIN应该从搜索框中抓取 – David

+0

那么,什么是'$ VIN'?它是一个ID(int)?因为如果不是,你应该使用'''。你尝试过吗? – Nanne

2

如果$ VIN为空或没有设置,你的查询将变得无效。如果$ vin包含一个包含非数字字符的字符串,查询很可能也是无效的。

您可以在值的周围添加引号,但在这种情况下,您也需要自行转义值。任何带有引号的搜索字符串都会使您的查询再次失效,并且可能会损坏您的数据库!如果我要搜索volvo'; delete from cars; --,那么您的查询会正常运行,但也会从您的表中删除所有值。这叫做sql注入

解决这个问题的最佳方法,尤其是在Oracle中,是使用绑定参数进行查询。在PHP.net上有关于oci_bind_by_name的主题中有一些例子可以帮助你。

+0

针对SQL注入技巧的+1 – Ollie