我正在制作一个基本的歌曲推荐网站,我有一个表单设置,通向一个页面,上面有这个确切的代码:
<?php
ini_set('display_errors',1);
ob_start();
session_start();
$host = "localhost";
$user = "root";
$pass = "MYPASS";
$db = "tts";
$conn = mysqli_connect($host, $user, $pass, $db);
$song = $_POST['song'];
$artist = $_POST['artist'];
$album = $_POST['album'];
$linkitunes = $_POST['linkitunes'];
$artwork = $_POST['artwork'];
$song = stripslashes($song);
$artist = stripslashes($artist);
$album = stripslashes($album);
$linkitunes = stripslashes($linkitunes);
$artwork = stripslashes($artwork);
$sql = "INSERT INTO recommendation (user_id, song, artist, album, linkitunes, artwork, rating)";
$sql = $sql . "VALUES ($_SESSION['id'], '$song', '$artist', '$album', '$linkitunes', '$artwork', '$rating');";
print "Hello.";
$result = mysqli_query($sql) or die("Fail");
ob_flush();
?>
它总是显示“你好”。字符串,直到我添加$ sql值。我认为代码语法有问题,但不确定。尝试了很多变化。为了以防万一,我也添加了表单代码:
<form action="recommend-action.php" method="POST">
<div id="noP" align="center">
<h2>Make a new Recommendation</h2>
<p>Please <a href="song-search.php">search</a> for your song before you recommend it.</p>
</div>
<div align="center">
<input required name="song" type="text" placeholder="Song" maxlength="50"></input>
<input required name="artist" type="text" placeholder="Artist" maxlength="50"></input>
<input name="album" type="text" placeholder="Album" maxlength="50"></input>
<input name="artwork" type="url" placeholder="Artwork" maxlength="500"></input>
<input name="linkitunes" type="url" placeholder="Link in iTunes" maxlength="500"></input>
<input id="submit" type="submit" value="Recommend"></input>
</div>
答案 0 :(得分:0)
你应该执行这个......
$result = mysqli_query($con,$sql); or die("Fail");
答案 1 :(得分:0)
我对您的代码做了一些修改,试试这个:
<?php
ini_set('display_errors',1);
ob_start();
session_start();
$host = "localhost";
$user = "root";
$pass = "MYPASS";
$db = "tts";
$conn = mysqli_connect($host, $user, $pass, $db);
$song = $_POST['song'];
$artist = $_POST['artist'];
$album = $_POST['album'];
$linkitunes = $_POST['linkitunes'];
$artwork = $_POST['artwork'];
$song = stripslashes($song);
$artist = stripslashes($artist);
$album = stripslashes($album);
$linkitunes = stripslashes($linkitunes);
$artwork = stripslashes($artwork);
$sql = "INSERT INTO recommendation (user_id, song, artist, album, linkitunes, artwork, rating)";
$sql = $sql . "VALUES (".$_SESSION['id'].", '$song', '$artist', '$album', '$linkitunes', '$artwork', '$rating');";
print "Hello.";
$result = mysqli_query($conn,$sql) or die("Fail");
ob_flush();
?>
答案 2 :(得分:0)
您在PHP中没有任何转换为HTML的行...它将如何在屏幕上显示任何内容。在代码末尾写下这个。
echo "<br> Inserted Successfully";
mysqli_query的语法错误。 使用以下一个。
$result=mysqli_query($conn,$sql) or die ("Fail");
其他可能的支票:
首先检查您的用户名和密码,并使用这段代码检查与数据库的连接是否成功。
if (!$conn) {
die('Could not connect to MySQL: ' . mysql_error());
}
使用session_start()作为代码的第一行,否则您的会话将无效。这不是必需的,但可能会在将来导致问题。