简体   繁体   English

在SQL查询中插入问题

[英]Issue with insert into sql query

$.ajax({
    url: 'http://localhost/xampp/htdocs/ajax.php',

    contentType: 'application/json',
    type: 'post',
    data: {

//score is a new variable and count2 is an integer variable which i want to send it to the database "score":"count2" }, // score是一个新变量,count2是一个整数变量,我想将其发送到数据库“ score”:“ count2”},

    success: function () {
        alert("ok");

    },
    error: function () {
       alert("error");
    }
});    

The above code alerts me "ok". 上面的代码提醒我“确定”。

function connect(){
    $connect = mysql_connect('localhost','root','') or die("ERROR");
    $connect2Database = mysql_select_db('fypdb', $connect);
    return $connect;
}

if(isset($_POST)){

    if($connect = connect()){

        $query = "INSERT INTO fypdbtable (score) VALUES ('".$_POST['score']."');";
        $completeQuery = mysql_query($query, $connect);
    }
}

I think the problem is on "insert into" query because in ajax.php prompts me Notice: Undefined index: score in C:\\xampp\\htdocs\\xampp\\htdocs\\ajax.php on line 14 where it is the query line 我认为这个问题是在“插入”查询,因为在ajax.php提示我注意:未定义指数:得分在C:\\ XAMPP \\ htdocs中\\ XAMPP \\ htdocs中\\上线14,在那里它是查询行ajax.php

Any help would be appreciate 任何帮助将不胜感激

Problem here is in your ajax code. 问题出在您的ajax代码中。 You are using contentType: 'application/json' but you try to acces param in your php script with $_POST , but post works with form encoded content types. 您正在使用contentType: 'application/json'但是您尝试使用$_POST php脚本中的param,但是post可以使用表单编码的内容类型。 So you can remove contentType from your ajax call and it will use default which is 因此,您可以从ajax调用中删除contentType,它将使用默认值(即
contentType:'application/x-www-form-urlencoded; charset=UTF-8'

EDIT Make your ajax code like that: 编辑使您的ajax代码像这样:

$.ajax({
url: 'http://localhost/xampp/htdocs/ajax.php',
type: 'post',
data: {
    "score":"count2"
},

success: function () {
    alert("ok");

},
error: function () {
   alert("error");
}
});

The "error" message tells you, that $_POST['score'] is not defined. “错误”消息告诉您,未定义$ _POST ['score']。 But thats just a warning. 但这只是一个警告。 If you want to know if your variable is set, use isset() function or turn off warnings in the configuration. 如果您想知道是否设置了变量,请使用isset()函数或在配置中关闭警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM