1
我試圖創建一個基本的登錄MySQL數據庫,但每次我嘗試登錄在我得到一個「沒有數據庫選擇「錯誤。我有一個數據庫設置與兩個表用戶(登錄)和評論(後)。代碼有點雜亂,不符合標準,但這不適合公衆使用。
下面是代碼:
<?php
error_reporting (0);
session_start();
if(!$_SESSION['username']){
header ('location: login.php');
}
else{
error_reporting(0);
require('connect.php');
echo "<a href=\"logout.php\">Logout</a><br /><br /><br />";
$name = $_SESSION['username'];
$today = date("c");
//$today = date("c", strtotime());
$comment = $_POST['comment'];
$submit=isset($_POST['submit']);
if($submit)
{
if($name&&$comment)
{
$query = mysql_query("INSERT INTO comment (name,comment,time) VALUES ('$name','$comment','$today')");
header("Location: success.php");
}
else
{
echo "Please fill out all the fields.";
}}}
?>
<html>
<head>
<title>Comment Box</title>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
</script>
</head>
<style>
body {
background-color: #DFDFDF;
}
</style>
<body>
<link type="text/javascript" href="localtime.js" />
<form action="#" method="POST">
<label>Name: </label>
<br />
<input type="text" disabled="disabled" name="name" value="<?php echo "$name" ?>" />
<br />
<br />
<label>Comment: </label>
<br />
<textarea name="comment" cols="25" rows="7"></textarea>
<br />
<br />
<input type="submit" name="submit" value="Comment" />
<br />
</form>
<hr width="1100px" size="5px" />
</body>
<?php
require('connect.php');
$query=mysql_query("SELECT * FROM comment ORDER BY id DESC");
while($rows=mysql_fetch_assoc($query))
{
$id=$rows['id'];
$dname=$rows['name'];
$dcomment=$rows['comment'];
$dtime=$rows['time'];
$atime="<abbr class='timeago' title='$dtime'></abbr>";
echo '<font color="red">Name:</font> ' . $dname . ' '. $atime . '<br />' . '<br />' . '<font color="red">Comments:</font> ' . '<br />' . $dcomment . ' ' . ' ' .
' ' . ' ';
if($_SESSION['username']=="admin"){
echo "<a href=\"delete.php?id=" . $rows['id'] . "\">Delete User</a>";
}
else
{
echo "";
}
echo '<br />' . '<br />' .
'<hr size="5px" width="500px" color="blue" />' . '<br />' . '<br />' ;
}
?>
</html>
[已經要求,並回答了很多次(http://stackoverflow.com/search?q= [MySQL的] +沒有+數據庫+選擇) – Jocelyn
請不要使用'mysql_ *'函數來編寫新的代碼。他們不再維護,社區已經開始[棄用程序](http://goo.gl/KJveJ)。查看[*紅色框*](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/vFWnC)。 –