0

使用 php 插入记录并访问数据库 - 获取“odbc_exec() 的参数计数错误” 一切正常,但我无法将其插入数据库。

$R1=rand(1,20);
$db=odbc_connect('Eightball','','');
 $sql  = "SELECT * FROM theAnswers";
 $sql .= " WHERE id = " . $R1;
 $rs=odbc_exec($db,$sql);
while (odbc_fetch_row($rs))
{       
 $FinalAnswer=odbc_result($rs,"Answer");
 $newdate = date('M j, Y');
 $newQuestion = $_POST["Question"];
 if(isset($_SERVER['HTTP_REFERER'])) {
 $Thereferrer=$_SERVER['HTTP_REFERER'];
 }
$theIPAddress=$_SERVER['REMOTE_ADDR'];

 echo $theIPAddress;
 echo $Thereferrer;
 echo $newQuestion;
 echo $FinalAnswer;
 echo $newdate;
}


$sql = "INSERT INTO theQuestions (ipaddress, referrer, Question, Answer, theDate)     VALUES ('$theIPAddress', '$Thereferrer', '$newQuestion', '$FinalAnswer', newdate)";
$rs=odbc_exec($db,$sql);
$result = odbc_exec($sql);
if (!$result) {exit('Execution failed!');} 
4

1 回答 1

0

倒数第二行

$result = odbc_exec($sql);

是给出“错误参数计数”错误的那个。第二个电话也是无关紧要的。

我也担心日期格式,但我在我的 Windows 服务器上尝试过,以下对我有用

<?php
$db = odbc_connect('db1', '', '');
$dateValue = date('M j, Y');
$sql = 
        "INSERT INTO phpTest (TextCol, DateCol) " .
        "VALUES ('testing', '" . $dateValue . "')";
echo $sql . "\r\n";
odbc_exec($db, $sql);
echo "Done.\r\n";

试运行成功...

C:\__tmp>\php\php odbcTest.php
INSERT INTO phpTest (TextCol, DateCol) VALUES ('testing', 'Apr 10, 2013')
Done.

...并且该行确实已插入表中

phpTest

不过,date('Y-m-d')这将是一个更安全的选择。

于 2013-04-10T09:39:29.840 回答