因此,当我将信息放入我的注册表单时,该信息将不会转到phpmyadmin数据库...这是代码:
这是index.php文件:
<?php
date_default_timezone_set('Europe/Copenhagen');
include 'dbh.inc.php';
include 'comments.inc.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!--Ovo je za login -->
<?php
echo "<form method='POST' action='".getLogin($conn)."'>
<input type='text' name='uid'>
<input type='password' name='pwd'>
<button type='submit' name='loginSubmit'>Login</button>
</form>";
echo "<form method='POST' action='".userLogout()."'>
<button type='submit' name='logoutSubmit'>Log out</button>
</form>";
if (isset($_SESSION['id'])) {
echo "You are logged in";
} else {
echo "You are NOT logged in!";
}
?>
<!-- Ovo je za register -->
<?php
echo "<form action='signup.php' method='POST'>
<input type='text' name='uid' placeholder='Username'><br>
<input type='password' name='pwd' placeholder='Password'><br>
<button type='submit'>SIGN UP</button>
</form>";
?>
<br><br>
<!-- OVDE SE ZAVRSAVA -->
<iframe src="https://www.youtube.com/embed/N6sB0-jKS6c" frameborder="0" allowfullscreen></iframe>
<?php
echo "<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea><br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
getComments($conn);
?>
</body>
</html>
这是comments.inc.php文件
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])){
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysqli_query($conn, $sql);
}
}
function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()){
echo "<div class='comment-box'><p>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo nl2br($row['message']);
echo "</p>
<form class='delete-form' method='POST' action='".deleteComments($conn)."'>
<input type='hidden' name='cid' value='".$row['cid']."'>
<button type='submit' name='commentDelete'>Delete</button>
</form>
<form class='edit-form' method='POST' action='editcomment.php'>
<input type='hidden' name='cid' value='".$row['cid']."'>
<input type='hidden' name='uid' value='".$row['uid']."'>
<input type='hidden' name='date' value='".$row['date']."'>
<input type='hidden' name='message' value='".$row['message']."'>
<button>Edit</button>
</form>
</div>";
}
}
function editComments($conn) {
if (isset($_POST['commentSubmit'])){
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "UPDATE comments SET message='$message' WHERE cid='$cid'";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
}
}
function deleteComments($conn) {
if (isset($_POST['commentDelete'])){
$cid = $_POST['cid'];
$sql = "DELETE FROM comments WHERE cid='$cid'";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
}
}
function getLogin($conn) {
if (isset($_POST['loginSubmit'])) {
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE uid='$uid' AND pwd='$pwd'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
if ($row = $result->fetch_assoc()){
$_SESSION['id'] = $row['id'];
header("Location: index.php?loginsuccess");
exit();
}
}else{
header("Location: index.php?loginfailed");
exit();
}
}
}
function userLogout() {
if (isset($_POST['logoutSubmit'])) {
session_start();
session_destroy();
header("Location: index.php");
exit();
}
}
这是signup.php文件
<?php
include 'dbh.php';
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (uid, pwd)
VALUES ('$uid', '$pwd')";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
dbh.php文件仅用于连接数据库
<?php
$conn = mysqli_connect("localhost", "root", "", "logintest");
if (!$conn){
die("Connection failed: ".mysqli_connect_error());
}
//
<?php
date_default_timezone_set('Europe/Copenhagen');
include 'dbh.inc.php';
include 'comments.inc.php';
?>
以及editcomment.php文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
echo "<form method='POST' action='".editComments($conn)."'>
<input type='hidden' name='cid' value='".$cid."'>
<input type='hidden' name='uid' value='".$uid."'>
<input type='hidden' name='date' value='".$date."'>
<textarea name='message'>".$message."</textarea><br>
<button type='submit' name='commentSubmit'>Edit</button>
</form>";
?>
</body>
</html>
好的,我认为问题可能在于放置了注册表单的index.php文件或signup.php文件,但我不知道如何修复它。