doubi8383 2014-09-09 16:58
浏览 64
已采纳

使用ajax将数据插入mysql

I'm trying to insert data to mysql, tried everything but nothing worked

here is my code :

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");
}

?>

I tried to the sql code and it worked in the phpmyadmin page.

So what is missing that is preventing the data from going into the database?

UPDATE: when i try to echo all the variables and thier values apears normally i also tried to do this :

$sql = "INSERT INTO `ratings` VALUES ( '', '3.5', '2.5' ,'4.5', '2.5' ,'1' , '2' , 'hello how are you')";

it does not insert this values to the database

but when i put the same sql code in the phpmyadmin its adds a row perfectly

  • 写回答

3条回答 默认 最新

  • dou91855 2014-09-09 19:26
    关注

    It seems, your Js-code has some missing paranthesis. >ou should replace "val" with the function call "val()"

      var teaching=$("#teaching").val();
      var marking=$("#marks").val();
      var helpfulness=$("#helpfulness").val();
      var difficulty=$("#difficulty").val();
      var grade=$("#grade").val();
      var com=$("#com").val();
    

    Afterwards, you should get some values in PHP-land, which can be inserted.

    Additionally, you are mixing procedural and OOP-code.

     mysqli_query($sql);
    

    ... is at least missing the connection as first parameter. But since you saved an instance of mysqli_connection already in $db try replacing it with:

    $db->query($sql);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog