2016-07-05 243 views
-1

不插入數據MySQL數據庫

$(document).ready(function(){ 
 
$("#submit").click(function(){ 
 
var name = $("#name").val(); 
 
var email = $("#email").val(); 
 
var password = $("#password").val(); 
 
var contact = $("#contact").val(); 
 
// Returns successful data submission message when the entered information is stored in database. 
 
var dataString = 'name1='+ name + '&email1='+ email + '&password1='+ password + '&contact1='+ contact; 
 
if(name==''||email==''||password==''||contact=='') 
 
{ 
 
alert("Please Fill All Fields"); 
 
} 
 
else 
 
{ 
 
// AJAX Code To Submit Form. 
 
$.ajax({ 
 
type: "POST", 
 
url: "ajaxsubmit.php", 
 
data: dataString, 
 
cache: false, 
 
success: function(result){ 
 
alert(result); 
 
} 
 
}); 
 
} 
 
return false; 
 
}); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<title>Submit Form Using AJAX and jQuery</title> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<link href="css/refreshform.css" rel="stylesheet"> 
 
<script src="script.js"></script> 
 
</head> 
 
<body> 
 
<div id="mainform"> 
 
<h2>Submit Form Using AJAX and jQuery</h2> <!-- Required div Starts Here --> 
 
<div id="form"> 
 
<h3>Fill Your Information !</h3> 
 
<div> 
 
<label>Name :</label> 
 
<input id="name" type="text"> 
 
<label>Email :</label> 
 
<input id="email" type="text"> 
 
<label>Password :</label> 
 
<input id="password" type="password"> 
 
<label>Contact No :</label> 
 
<input id="contact" type="text"> 
 
<input id="submit" type="button" value="Submit"> 
 
</div> 
 
</div> 
 
</div> 
 
</body> 
 
</html>

這是我在ajaxsubmit.php

$host = "myhost"; 
$user = "myusername"; 
$password = "******"; 
$database = "thisismydb"; 

$connection = mysqli_connect($host, $user, $password, $database); 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
//Fetching Values from URL 

$name2=$_POST['name1']; 
$email2=$_POST['email1']; 
$password2=$_POST['password1']; 
$contact2=$_POST['contact1']; 
//Insert query 
mysqli_query($connection,"SELECT * FROM databasetable"); 
mysqli_query($connection,"INSERT INTO databasetable (name, email, password, contact) 
VALUES ($name2', '$email2', '$password2','$contact2')"); 


mysqli_close($connection); 
?> 

但是每當我點擊提交,只給了我展示了一個警報代碼從ajaxsubmit.php,我不知道我在做什麼錯誤D:請幫助!

注:我使用bootstrap3

+1

檢查第四個參數的頂部寫<?php標籤()'http://php.net/manual/en /function.mysql-connect.php – Saty

+0

檢查mysql_connect中的第四個參數,如果您有練習,可以使用mysqli。單獨運行php文件進行調試。 –

+1

您的服務器不執行'php'代碼 – newage

回答

0

你需要在的mysql_connect的`ajaxsubmit.php