2尝试使用mysql将数据插入数据库的错误

时间:2013-11-12 13:48:23

标签: php mysql database

对不起,这可能是一个愚蠢的问题,但我是新手,我在谷歌找不到答案。这段代码给了我两个错误:

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

不确定如何解决这个问题,请帮助我。

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

5 个答案:

答案 0 :(得分:2)

删除左边的双引号:

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

答案 1 :(得分:0)

  1. 仅使用mysqli_。 mysql_已被弃用,可能很快就会从php中删除
  2. 您的连接是假的。应该是这样的:

    $ con = mysql_connect(“localhost”,“root”,“”,“music”);

答案 2 :(得分:0)

你也在两次调用mysql_query,除非你想要两个插件你可能想做这样的事情:

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

答案 3 :(得分:0)

将您的陈述更改为

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

答案 4 :(得分:0)

首先删除双引号:

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

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

然后更改以下行

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

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

因为mysql_query的第一个参数需要是sql查询和第二个数据库标识符。

其余代码很好