Insert record using php and access database - getting "Wrong parameter count for odbc_exec()" Everything works except I can't get it to insert into the database.
$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!');}
The second-last line
$result = odbc_exec($sql);
is the one that's giving the "Wrong parameter count" error. That second call is also extraneous.
I was also concerned about the date format, but I tried it on my Windows server the following worked for me
<?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";
The test run was successful...
C:\__tmp>\php\php odbcTest.php
INSERT INTO phpTest (TextCol, DateCol) VALUES ('testing', 'Apr 10, 2013')
Done.
...and the row was indeed inserted into the table
Still, date('Ym-d')
would be a safer choice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.