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条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集