mySQL插入语句不起作用?

时间:2011-12-14 06:14:49

标签: php jquery mysql html ajax

我一直在寻找答案。我一直在与其他人交叉引用我的代码。我似乎没有看到任何明显的错误......

我的数据库没有更新,我的数据库什么也没做。哦,页面也没有做任何事......这令人沮丧,因为我只是使用记事本++并且无法查明错误。我也在使用XAmpp,并且所有的值都与我所做的相匹配。

    The .post statement (Using jQuery 1.7.1):
    //Make sure DOM is loaded before running jQuery based code: [STATIC CODE]
$(document).ready(function(){

    $('#uploadbtn').click(function() {

        //Parse name and song.
        var name = $('#songname').val();
        var song =  $('#songupload').val();

        $.ajax(
        {
            type: 'POST',
            data: 'db/upload.php?name=' + name + 'song=' + song,
            success: function(res) {
                $('#nav-playlist').html(res);
            }
        }   
        )

    });

});



Now here is my php file:

<?php

/*Connection to database.
   First with mysql_connect (to log in). Then selecting the master database.
*/
echo "Upload.php accessed...";
$connection = mysql_connect("localhost", "root", "root") or die ( mysql_error() );
$database = mysql_select_db("betadb") or die( mysql_error() );

//Properties (to be inserted into database).
$name = mysql_real_escape_string($_POST["name"]); 
$song = mysql_real_escape_string($_POST["song"]);

//Insertion formula for mySQL
$query = "INSERT INTO songs SET name= '$name' song='$song' ";

if (mysql_query($query)){
echo "Success";
} 
else {

}

?>

其他说明: 歌曲表由id,name,song(按此顺序)组成。 Song是BLOB数据类型,因为它用于存储.mp3s

7 个答案:

答案 0 :(得分:3)

问题在于以下一行

data    : 'db/upload.php?name='+name+'song='+song,

数据应该是包含值的数组,例如

var data
data["name"] = name
data["song"] = song

$ .ajax调用也缺少执行请求所需的url参数

url: 'db/upload.php'

答案 1 :(得分:2)

尝试指定列。

$query = "INSERT INTO songs (youcoulmn1,youcolumn2) VALUES ('$name', '$song')";

另见:

PHP MySQL Insert Into

此致

答案 2 :(得分:1)

为了调试它 1)做print_r($_POST);检查你有什么要插入的 2)然后代替
$result = mysql_query($query, $connection) or die ("Unsucessful");
  做
$result = mysql_query($query, $connection) or die (mysql_error());
获取确切的错误并搜索错误修复

答案 3 :(得分:1)

  $name = mysql_real_escape_string($_POST["name"]); //Assign to name & song variables.
    $song = mysql_real_escape_string($_POST["song"]);

//Insertion formula for mySQL
$query = "INSERT INTO songs VALUES ('$name', '$song')";

$result = mysql_query($query, $connection) or die ("Unsucessful");

答案 4 :(得分:1)

最好使用SET,对某些条件更容易,而对于Jquery Command使用$.ajax,你可以试试这个

for javascript / JQuery

$(function(){
    $('#uploadbtn').click(function(){
        var name = $('#songname').val();
        var song =  $('#songupload').val();
        $.ajax({
            type    :'POST',
            data    : 'db/upload.php?name='+name+'song='+song,
            success :function(res){
                $('#nav-playlist').html(res);
            }
        });
    });
});

和php中的Insert命令

$name = mysql_real_escape_string($_POST["name"]); 
$song = mysql_real_escape_string($_POST["song"]);

$query = "INSERT INTO songs SET name='$name' song='$song'";
if(mysql_query($query)){
    // some process if success
}else{
    // some proses if not
}

使用mysql_real_escape_string在插入数据库之前过滤数据

答案 5 :(得分:1)

围绕变量的单引号可能导致问题..检查它..

答案 6 :(得分:1)

据我记忆,行

$query = "INSERT INTO songs SET name= '$name' song='$song' ";

应该是

$query = "INSERT INTO songs SET name= '$name', song='$song' ";

注意逗号!

还有:

data: 'db/upload.php?name=' + name + 'song=' + song,

至少应该是:

data: 'db/upload.php?name=' + name + '&song=' + song,

因为现在字段之间没有分隔符。