php + jquery + mysql 表單資料無法寫入資料庫
放上服務器submit後 出現
POST https://test.online/001.php 500 (Internal Server Error)
XHR finished loading: POST "https://chienweiluo.online/001.php".
在本地端調適(127.0.0.1) 是
POST http://127.0.0.1:63943/001.php 404 (Not Found)
我網上查都是說路徑的問題, 可我已經把路徑弄妥了, 直接與./index.html 在同一層, 還是報錯各位大神幫看看吧
001.php
$data = $_POST;
$dbhost = 'chienweiluo.online';
$dbuser = 'chienlxd_wei';
$dbname = 'chienlxd_reply';
$dbpasswd = 'chienwei0526';
$dsn = "mysql:host=".$dbhost.";dbname=".$dbname;
try
{
$conn = new PDO($dsn,$dbuser,$dbpasswd);
$conn->exec("SET CHARACTER SET utf8");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: ".$e->getMessage();
}
$sql = "INSERT INTO `users`(name, email, content,time) VALUES(:name, :email, :content,:time)";
$dataArr = array(
':name' => $data['name'],
':email' => $data['email'],
':content' => $data['content'],
':time' => $data['time']);
try
{
$sth = $conn->prepare($sql);
if($sth)
{
$result = $sth->execute($dataArr);
if($result)//true or false
{
$result = 'success';
echo json_encode($result);
return;
}
else
{
$result = 'failed';
echo json_encode($result);
return;
}
}
}
catch(PDOException $e)
{
echo "執行預存程序失敗. ".$e->getMessage();
}
?>
ajax& form
$(document).ready(function(){
$("input[type='submit']").on('click', function(){
$.ajax({
url: './001.php',
method : "POST",
dataType : 'json',
data : $("#form").serialize(),
complete : function(result) {
console.log("coplete"+result);
},
error: function(result) {
console.log("erroe"+result);
}
})
});
});