我是一个javascript新手,我在服务器端使用javascript和php编写应用程序,我试图使用AJAX将数据发送到我的php脚本。这是我的代码
使用Javascript:
$(document).on("click", ".uib_w_18", function(evt)
{
var lecturer = document.getElementById("reg_name").value;
//var lecturer = $("#reg_name").val();
var dept = document.getElementById("reg_dept").value;
var level = document.getElementById("reg_level").value;
var course = document.getElementById("reg_course").value;
var start = document.getElementById("reg_time_1").value;
var ade = 2;
window.alert(lecturer);
var dataString = '?ade=' + ade+'&lecturer='+lecturer+'&dept='+dept +'&level='+level+'&course='+course+'&start='+start;
$.ajax({
type: "GET",
url: 'http://localhost/my_queries.php',
data: dataString,
success: window.alert ("I've been to localhost.")
});
window.alert(dataString);
});
并在服务器端:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbname = "myDatabase";
$dbpass = null;
//Connect to MySQL Server
echo "yo";
$con = mysqli_connect($dbhost, $dbuser,$dbpass,$dbname);
$level = $_GET['level'];
$lecturer = $_GET['lecturer'];
$sql = "INSERT INTO level1(message, department)
VALUES ($level,'Jane')";
$sql2 = "INSERT INTO level1(message, department)
VALUES ($lecturer,'Jane')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
?>
现在的问题是&#39; $ sql1&#39;成功执行但是&#39; $ sql2&#39;没有按&#39;吨。我已经有一段时间了,发现脚本中的$ _GET只适用于数值数据。我已经确认问题不是来自我的表的数据类型,我可以直接从PHP插入文字字符串,我还确认&#34; dataString&#34;像我想要的那样收集数据。 (window.alert(dataString);)显示正确的输出。 我觉得我错过了一些非常基本的东西,但我无法弄清楚它是什么。我觉得额外的双眼会有所帮助,任何帮助都会受到赞赏,谢谢。
答案 0 :(得分:0)
通过&#34;动态&#34;的正确方法SQL查询是这样的:
$sql = "INSERT INTO level1(message, department)
VALUES ('".$level."','Jane')";
$sql2 = "INSERT INTO level1(message, department)
VALUES ('".$lecturer."','Jane')";