我试图将数据插入到mysql中,尝试了一切但没有任何效果
这是我的代码:
使用Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#rating-btn").click( function(){
var teaching=$("#teaching").val;
var marking=$("#marks").val;
var helpfulness=$("#helpfulness").val;
var difficulty=$("#difficulty").val;
var grade=$("#grade").val;
var com=$("#com").val;
$.ajax({
type: "POST",
url:"db/ajax.php",
data:"teaching=" + teaching +"&marking="+ marking +"&helpfulness="+ helpfulness
+"&difficulty="+difficulty+"&grade="+grade+"&com="+com,
dataType: "dataString",
cache: "true",
success: function(msg,string,jqXHR){
$("#results").html(msg+string+jqXHR);
}
});
});
});
ajax.php
<?php
error_reporting(0);
require 'db/connect.php';
$teaching = $_POST['teaching'];
$teaching = mysql_real_escape_string($teaching);
$marking = $_POST['marking'];
$marking = mysql_real_escape_string($marking);
$helpfulness = $_POST['helpfulness'];
$helpfulness = mysql_real_escape_string($helpfulness);
$difficulty = $_POST['difficulty'];
$difficulty = mysql_real_escape_string($difficulty);
$grade = $_POST['grade'];
$grade = mysql_real_escape_string($grade);
$com= $_POST['com'];
$sql = "INSERT INTO ratings VALUES ( '', '{$teaching}', '{$marking}' ,'{$helpfulness}', '{$difficulty}' ,'{$grade}' , '2' , '{$com}')";
mysqli_query($sql);
?>
connect.php
<?php
$db= new mysqli('localhost','root','','instructors');
if($db->connect_errno){
die("we are having some problems");
}
?>
我尝试使用sql代码,它在 phpmyadmin 页面中工作。
那么缺少什么阻止数据进入数据库?
更新: 当我试图回应所有的变量和他们正常的值 我也尝试过这样做:
$sql = "INSERT INTO `ratings` VALUES ( '', '3.5', '2.5' ,'4.5', '2.5' ,'1' , '2' , 'hello how are you')";
它不会将此值插入数据库
但是当我在phpmyadmin中放入相同的sql代码时,它会完全添加一行
答案 0 :(得分:1)
看起来,你的Js代码有一些缺失的paranthesis。 &gt; ou应该替换&#34; val&#34;使用函数调用&#34; val()&#34;
var teaching=$("#teaching").val();
var marking=$("#marks").val();
var helpfulness=$("#helpfulness").val();
var difficulty=$("#difficulty").val();
var grade=$("#grade").val();
var com=$("#com").val();
之后,你应该在PHP-land中获得一些可以插入的值。
此外,您正在混合程序和OOP代码。
mysqli_query($sql);
...至少缺少连接作为第一个参数。但是,由于您已经在$ db中保存了一个mysqli_connection实例,请尝试将其替换为:
$db->query($sql);
答案 1 :(得分:0)
大家对mysql和mysqli的评价。另外你必须添加&amp;在vars之间。
data:"teaching=" + teaching +"&marking="+ marking +"&helpfulness="+ helpfulness
+"&difficulty="+difficulty+"&grade="+grade+"&comment="+comment,
答案 2 :(得分:0)
通过使用我已经在ajax.php中的connect.php中创建的对象$ db来解决我的问题
而不是写
query(&sql)
解决方案是:
$db->query($sql);
感谢大家的帮助。