简体   繁体   English

mySQL插入语句不起作用?

[英]mySQL insert statement NOT working?

I've been looking around for an answer. 我一直在寻找答案。 I've been cross-referencing my code with others. 我一直在与其他人交叉引用我的代码。 And I don't seem to see any blatant errors... 我似乎没有看到任何明显的错误......

My database does not update, my database does nothing. 我的数据库没有更新,我的数据库什么也没做。 Oh and the page also does nothing too...... It's frustrating because, I'm just using notepad ++ and can't pinpoint the error. 哦,页面也没有做任何事......这令人沮丧,因为我只是使用记事本++并且无法查明错误。 I'm using XAmpp as well, and, all the values match the ones I have made. 我也在使用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 {

}

?>

ADDITIONAL NOTES: the song table consists of id, name, song (in that order). 附加说明:歌曲表由id,name,song(按此顺序)组成。 Song is the BLOB datatype, as it is used to store .mp3s Song是BLOB数据类型,因为它用于存储.mp3s

The problem is with the following line 问题出在以下几行

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

data should be an array containing the values, such as data应该是包含值的数组,例如

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

The $.ajax call is also missing the url parameter which is needed to carry out the request $ .ajax调用也缺少执行请求所需的url参数

url: 'db/upload.php'

Try to specify your column. 尝试指定您的列。

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

See also: 也可以看看:

PHP MySQL Insert Into PHP MySQL插入

Regards 问候

in order to debug it 为了调试它
1) do print_r($_POST); 1)做print_r($_POST); to check do you have anything to insert 检查你有什么要插入
2) then instead of 2)然后而不是
$result = mysql_query($query, $connection) or die ("Unsucessful");
do
$result = mysql_query($query, $connection) or die (mysql_error());
to get the exact error and search for the error fix 获取确切的错误并搜索错误修复程序

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

had better using SET , is more easier for some conditions, and for Jquery Command use $.ajax , you can try this one 最好使用SET ,对某些条件更容易,而对于Jquery Command使用$.ajax ,你可以尝试这个

for javascript / JQuery 对于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);
            }
        });
    });
});

and for Insert command in the php 并在php中插入命令

$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
}

use mysql_real_escape_string to filtering data before insert to database 在插入数据库之前使用mysql_real_escape_string过滤数据

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

As far as I remember, the line 据我记忆,这条线

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

should be 应该

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

Pay attention to commas! 注意逗号!

And also: 并且:

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

should be at least: 至少应该是:

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

because there is no delimiter between fields right now. 因为现在字段之间没有分隔符。

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

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