mysql解決新增失敗主鍵自加問題
<?php
function login(){
$user=$_POST["user"];
$birthday=$_POST["birthday"];
$userpass=$_POST["userpass"];
$username=$_POST["username"];
$telno=$_POST["telno"];
$sex=$_POST["sex"];
$result = "";
$password = "";
$response = "";
$link = mysqli_connect('127.0.0.1','root','password');
if(! $link ){
die('error:'.mysqli_connect_error());
}
echo 'success<hr>';
mysqli_select_db($link,'test01') or die("error");
$sql = "select * from users";
$result = mysqli_query($link,$sql);
$id=mysqli_num_rows($result)+1;
if($sex!='男' && $sex!='女'){
$result = "性別有誤";
$password = "";
$response = "2002";
}else{
$sql="insert into users values('$id','$user','$birthday','$userpass','$username','$telno','$sex')";
if ($result = mysqli_query($link,$sql)){
$result = "請求成功";
$password = $userpass;
$response = "3000";
$id=$id+1;
}else if(mysqli_error($link) == "Incorrect date value: '1' for column 'birthday' at row 1"){
$result = "生日格式錯誤";
$password = "";
$response = "2001";
}else{
$result = mysqli_error($link);
$password = "";
$response = "2000";
}
}
$output = array(
"result"=>$result,
"password"=>$password,
"response"=>$response
);
echo json_encode($output);
}
login();
?>