sonyangelhan sonyangelhan - 3 months ago 10
PHP Question

Inserting record to MS Access with PHP

so straight to the problem. I'm working with php on a local DSN MS Access database. But the insert function doesnt work, no record added after running the page.

Here are the code.

<?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");--*/

?>


Thanks in advance!

Answer

I'm not sure but the SQL command seems messy. Try something like this:

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

Use your correct field names, of course.

Comments