<html>
<head>
<title>PHP test</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="formDiv">
<form action="testphp.php" method="post">
Email: <input type="text" name="email"><br>
<textarea name="comment" rows="10" cols="40">Your comment here.</textarea>
<input type="submit" value="submit">
</form>
<div id="show">
<?php
$con=mysqli_connect("","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Comments");
echo "<table border='1'>
<tr>
<th>Email</th>
<th>Comment</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</div>
</div>
<?php
$con=mysqli_connect("","root","","my_db");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_POST["comment"]))
{echo "You have to write something. <br>";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\">";
}
else
{
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(empty($_POST["email"]))
{
$sql="INSERT INTO Comments (Email, comment)
VALUES
('$_POST[email]', '$_POST[comment]')";
//echo "1 record added.";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\">";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
else{
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST["email"]))
{
echo "Invalid email format <br>";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\">";
}
else{
$sql="INSERT INTO Comments (Email, comment)
VALUES
('$_POST[email]', '$_POST[comment]')";
echo "<meta http-equiv=\"refresh\" content=\"2;URL=testphp.php\">";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
}
}
}
$db = mysql_selectdb('my_db', $con) or trigger_error("SQL", E_USER_ERROR);
//number of rows in table
$sql = "SELECT COUNT (*) FROM Comments";
$result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numRows = $r[0];
//number of rows to show per page
$rowsPerPage = 10;
$totalPages = ceil($numRows/$rowsPerPage);
//get current page or set default
if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])){
$currentpage = (int) $_GET['currentpage'];
}
else{
$currentpage = 1;
}
if($currentpage > $totalPages){
$currentpage = $totalPages;
}
if($currentpage < 1){
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsPerPage;
//get info from DB
$sql = "SELECT comment FROM Comments LIMIT $offset, $rowsPerPage";
$result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
while($list = mysql_fetch_assoc($result)){
echo $list['comment'];
echo "<br />";
}
//build pagination links
if($currentpage > 1){
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}
$range = 3;
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
}
else{
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if($currentpage != $totalpages){
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}
?>
</body>
</html>
这是我的代码,我从其他网站获取了分页代码,并尝试更改它以满足我的需求,但我无法使其工作。我收到了这行代码的警告
$db = mysql_selectdb('my_db', $con) or trigger_error("SQL", E_USER_ERROR);)
Warning: mysql_selectdb() expects parameter 2 to be resource.
这是我第一次使用php进行编程,我已经尝试了分页的所有代码,但它只是不起作用...