mysqli_query给我无法解决的消息

时间:2015-01-28 18:46:08

标签: php sql

我正在尝试创建一个基本的歌曲信息页面,我唯一的问题是SQL。我一直收到这条消息:

  

警告:mysqli_query()要求参数1为mysqli,第33行/var/www/tts/recommend-action.php中给出的字符串

这是我的代码:

<?php

session_start();

ini_set("display_errors",true);

ob_start();

$host = "localhost"; 
$user = "root";
$pass = "[MYPASSWORD]";
$db = "[MYDATABASE]";
$tb = "recommendation";

$link = mysqli_connect($host, $user, $pass, $db) or die("Failed to connect.");

$song = $_POST['song'];
$album = $_POST['album'];
$artist = $_POST['artist'];
$linkitunes = $_POST['linkitunes'];
$artwork = $_POST['albumPic'];

$song = stripslashes($song);
$album = stripslashes($album);
$artist = stripslashes($artist);
$link = stripslashes($linkitunes);
$artwork = stripslashes($artwork);

print "<br /><br /><b>User ID: </b>" . $_SESSION['user_id'] . "<br /><b>Song: </b>$song<br /><b>Album: </b>$album<br /><b>Artist: </b>$artist<br /><br />";

$sql = "INSERT INTO recommendation (user_id, artist, song, album, artwork, linkitunes) VALUES (" . $_SESSION['user_id'] . ", $artist, $song, $album, $artwork, $linkitunes);";

$postrec = mysqli_query($link, $sql);

if ($postrec == true) {
    print "sucess";
}
else {
    print "<br /><br />failed";
}

ob_flush();

?>

我找不到解决方案。非常感谢帮助。

1 个答案:

答案 0 :(得分:4)

你连接正常,$link很好:

$link = mysqli_connect($host, $user, $pass, $db) or die("Failed to connect.");

但后来重新定义为字符串:

$link = stripslashes($linkitunes);

然后你尝试使用字符串:

$postrec = mysqli_query($link, $sql);