我正在尝试使用php将一些记录添加到数据库中。我使用jquery ajax post请求传递数据。 这是我的PHP代码将记录发布到数据库
<?php
session_start();
include_once 'db.php';
$data=$_POST['data'];
$data = json_decode(stripslashes($_POST['data']),true);
foreach($data as $d){
$lname=$d['last'];
$fname=$d['first'];
$email=$d['email'];
$classid=$_SESSION['classid'];
$records = $databaseConnection->prepare('INSERT INTO `Students` (LastName,FirstName,Email,classid,profile,confirmation)
VALUES (:LastName,:FirstName,:Email,:classid,:profile,:confirmation)');
//$records->bindParam(':LastName',$lname);
//$records->bindParam(':FirstName',$fname);
//$records->bindParam(':Email',$email);
//$url=addcslashes('studentimg/dimg.png',"/");
$urs='test';
//$records->bindParam(':profile',$url);
//$records->bindParam(':classid',$classid);
$confirmation=randomCode();
//$records->bindParam(':confirmation',$confirmation);
$link="www.example.com/studentpwdchange?code=$confirmation";
echo $link;
$records->debugDumpParams();
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($email,"You have been added to my class","Hello You have been added to my class. Go to $link",$headers);
$params = array(
':LastName' => $lname,
':FirstName' => $fname,
':Email' => $email,
':profile' => $link,
':classid' => $classid,
':confirmation' => $confirmation
);
var_dump($params);
$records->execute($params);
}
echo 'success';
function randomCode() {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
我使用jquery以下列方式传递数据
$("#stsubmit").on('click',function(){
var data=[];
var first=$("#first_name").val();
var last=$("#last_name").val();
var email=$("#email").val();
var std={"first":first,"last":last,"email":email};
data.push(std);
for(i=2;i<=counter;i++)
{
var fn=$("#first_name"+i).val();
var ln=$("#last_name"+i).val();
var em=$("#email"+i).val();
var s={"first":fn,"last":ln,"email":em};
data.push(s);
}
var jsonString = JSON.stringify(data);
$.ajax({
url: 'tryaddstudent.php',
data: {'data':jsonString},
type: 'POST',
error: function() {
console.log('Error');
},
success: function(msg) {
console.log(msg);
//window.location.href = "standings";
}
});
});
我尝试了很多方法,并在stackoverflow中咨询了man的其他答案。什么都没有帮助。 在控制台中,我收到以下类型的消息
[Object]
jquery.min.js:4 POST http://example.com/UML2/production/tryaddstudent.php 500 (Internal Server Error)send @ jquery.min.js:4ajax @ jquery.min.js:4(anonymous function) @ addStudents:270dispatch @ jquery.min.js:3r.handle @ jquery.min.js:3
addStudents:279 500
addStudents:280 www.example.com/studentpwdchange?code=agAb2lMQSQL: [153] INSERT INTO `Students` (LastName,FirstName,Email,classid,profile,confirmation)
VALUES (:LastName,:FirstName,:Email,:classid,:profile,:confirmation)
Params: 0
array(6) {
[":LastName"]=>
string(5) "Islam"
[":FirstName"]=>
string(10) "Md Johirul"
[":Email"]=>
string(19) "johirbuet@gmail.com"
[":profile"]=>
string(47) "www.example.com/studentpwdchange?code=agAb2lMQ"
[":classid"]=>
string(8) "o4xu1vPb"
[":confirmation"]=>
string(8) "agAb2lMQ"
}
addStudents:281 Internal Server Error
任何人都可以帮我解决一下。我不知道发生了什么