使用PHP将记录插入MS Access

时间:2013-12-02 13:09:03

标签: php sql ms-access insert odbc

如此直接的问题。我在本地DSN MS Access数据库上使用php。但插入功能不起作用,运行页面后没有添加任何记录。

以下是代码。

<?php

$conn=odbc_connect('rewardscard','','');
if (!$conn) {
    exit("Connection Failed: " . $conn);
}

$username=$_POST['username']; 
$birthday=$_POST['birthday'];
$gender=$_POST['gender'];
$phone=$_POST['phone'];
$address=$_POST['address'];
$password=$_POST['password'];

$sql="SELECT * FROM UserInfo";
$user=odbc_exec($conn,$sql);
if (!$user) {
    exit("Error in SQL");
}

$newid=0;
while (odbc_fetch_row($user)) {
    $lastuser=odbc_result($user, "UserID");
    if ($newid < preg_match_all('!\d+!', $lastuser, $lastuserint)) {
        $newid=$lastuserint+1;
    }
}

$userid="$newid".'U';

$comid=001;
$balance=0;

$sql1="INSERT INTO UserInfo (UserID, UserName, Password, Birthday, Gender, Phone, Address, CompanyID, Balance) VALUES ('$userid','$username','$password','$birthday','$gender','$phone','$address','$comid','$balance')";
$result=odbc_exec($conn,$sql1);
echo $result;

session_start();
$_SESSION['username']=$username;
/*--    header("Location: home.php");--*/

?>

提前致谢!

1 个答案:

答案 0 :(得分:0)

我不确定但SQL命令似乎很乱。尝试这样的事情:

`INSERT INTO UserInfo (userid, username, ...) VALUES ('$userid','$username','$birthday','$gender','$phone','$address','$password','$comid','$balance')`

当然,请使用正确的字段名称。