简体   繁体   中英

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.

    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). Song is the BLOB datatype, as it is used to store .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

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

url: 'db/upload.php'

Try to specify your column.

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

See also:

PHP MySQL Insert Into

Regards

in order to debug it
1) do print_r($_POST); to check do you have anything to insert
2) then instead of
$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

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

and for Insert command in the 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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