2尝试使用mysql将数据插入数据库的错误[关闭]

I'm sorry this is probably a stupid question, but I'm new to this and I couldn't find an answer on google. This code is giving me two errors:

Warning: mysql_query() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\Music Collection\submitmusic.php on line 27

Warning: mysql_error() expects parameter 1 to be resource, string given in C:\xampp\htdocs\Music Collection\submitmusic.php on line 29

Not sure how to fix this please help me.

<html>
    <head>
        <title> Music Collection </title>
    </head>

    <body>
    <?php
    $con = "mysql_connect ('localhost','root','','music')";
    // Check Connection
    if (mysql_errno())
    {
        echo "Failed to connect: " . mysql_error();
    }
    else
    {
        $title = $_POST['title'];
        $artist = $_POST['artist'];
        $album = $_POST['album'];
        $location = $_POST['location'];
        $media = $_POST['media'];

        $sql = mysql_query("INSERT INTO entries (Title, Artist, Album, Location, Media) VALUES ('$title','$artist','$album','$location','$media')");

        if (!mysql_query($con,$sql))
        {
            die ('Error: ' . mysql_error($con));
        }
        else
        {
            echo "record added!";
        }
    }
    mysql_close($con);
    ?>

    </body>
</html>
  1. only use mysqli_. mysql_ is deprecated and will probably removed from php soon
  2. your connect is false. Should look like this:

    $con = mysql_connect("localhost","root","","music");

Remove the double quotes around:

$con = "mysql_connect ('localhost','root','','music')";

You are also calling mysql_query twice, unless you want two inserts you probably want to do something like this:

$sql = mysql_query("INSERT INTO entries (Title, Artist, Album, Location, Media) VALUES ('$title','$artist','$album','$location','$media')");
if (!sql)
...

change your statment to

$sql = mysql_query("INSERT INTO entries (Title, Artist, Album, Location, Media) VALUES ('$title','$artist','$album','$location','$media')",$con);

First remove double quote around:

$con = "mysql_connect ('localhost','root','','music')";

i.e

$con = mysql_connect ('localhost','root','','music');

And then change the following line

if (!mysql_query($con,$sql))

to

if (!mysql_query($sql,$con))

because mysql_query's first parameter needs to be sql query and second database identifier.

rest of the code is fine