PHP评论源码是一种用于在网页上实现用户评论功能的代码。
<?php // 连接数据库 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn>connect_error) { die("连接失败: " . $conn>connect_error); } // 用户注册 if (isset($_POST['register'])) { $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')"; if ($conn>query($sql) === TRUE) { echo "注册成功"; } else { echo "Error: " . $sql . "<br>" . $conn>error; } } // 用户登录 if (isset($_POST['login'])) { $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = $conn>query($sql); if ($result>num_rows > 0) { session_start(); $_SESSION['username'] = $username; echo "登录成功"; } else { echo "用户名或密码错误"; } } // 发表评论 if (isset($_POST['submit_comment']) && isset($_SESSION['username'])) { $comment = $_POST['comment']; $username = $_SESSION['username']; $sql = "INSERT INTO comments (username, comment) VALUES ('$username', '$comment')"; if ($conn>query($sql) === TRUE) { echo "评论成功"; } else { echo "Error: " . $sql . "<br>" . $conn>error; } } // 显示评论 $sql = "SELECT * FROM comments"; $result = $conn>query($sql); if ($result>num_rows > 0) { while($row = $result>fetch_assoc()) { echo "<div><strong>" . $row["username"] . "</strong>: " . $row["comment"] . "</div>"; } } else { echo "暂无评论"; } $conn>close(); ?>
以上就是关于“php评论源码”的问题,朋友们可以点击主页了解更多内容,希望可以够帮助大家!
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1175436.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复