我需要创建一个可以检查填充的表单;如果为true,则在单击“提交”按钮后自行刷新
viewing.php
帖子最早在提交时显示,时间戳按降序排列
我已将此连接到本地数据库以存储和显示输入的数据。
我已尝试过论坛和youtube中的所有代码,但没有任何效果。
以下是代码:
的index.php:
<style rel="stylesheet" type="text/css">
body {
left:20px;
font-size:15pt;
}
label, input, textarea{
left:20px;
margin:5px 5px 5px 5px;
font-size:15pt;
}
div {
width:50%;
background-color:lightgrey;
}
</style>
</head>
<body>
<div> <!--id="auto"-->
<form name="ajax" action="insert.php" method="post" id="contact">
<br>
<label>Please Enter Your Comments :</label>
<br><br>
<label>Name :<span>*</span>
<input type="text" name="Name" id="Name" value="" placeholder="Name" autocomplete="off"/>
</label>
<br>
<label>Enter Your Wishes : <span>*</span>
<br>
<textarea name="Comment" rows="10" cols="30" id="Comment" placeholder="Message" autocomplete="off"></textarea>
</label>
<br>
<input type="submit" value="Submit" onclick="aa();"/>
<input type="reset" value="Clear"/>
</form>
</div>
<script type="text/javascript">
location.refresh(true);
$('#contact').submit(function () {
sendContactForm();
return false;
});
</script>
</body>
</html>
insert.php:
<?php
session_start();
require 'config.php';
if(isset($_POST['Name'], $_POST['Comment'])){
$fields = [
'Name' => $_POST['Name'],
'Comment' => $_POST['Comment']
];
foreach ($fields as $field => $data) {
if (empty($data)){
$errors[] = 'The '.$field . ' field is required.';
}
}
}
else {
$errors[] = 'Error.';
}
// This function will run within each post array including multi-dimensional arrays
function ExtendedAddslash(&$params)
{
foreach ($params as &$var) {
// check if $var is an array. If yes, it will start another ExtendedAddslash() function to loop to each key inside.
is_array($var) ? ExtendedAddslash($var) : $var=addslashes($var);
unset($var);
}
}
// Initialize ExtendedAddslash() function for every $_POST variable
ExtendedAddslash($_POST);
$ID = $_POST['ID'];
$Name = $_POST['Name'];
$Comment = $_POST['Comment'];
/*$db_host = 'localhost:8889';
$db_username = 'root';
$db_password = 'root';
$db_name = 'Event';*/
mysql_connect( $db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name);
// search submission ID
$query = "SELECT * FROM 'demo' WHERE 'Name' = '$ID'";
$sqlsearch = mysql_query($query);
$resultcount = mysql_numrows($sqlsearch);
if ($resultcount > 0) {
mysql_query("UPDATE `demo` SET
`Name` = '$Name',
`Comment` = '$Comment',
WHERE `ID` = '$ID'")
or die(mysql_error());
} else {
mysql_query("INSERT INTO `demo` (ID, Name, Comment) VALUES ('$ID','$Name', '$Comment') ")
or die(mysql_error());
}
header('Location: index.php');
?>'
viewing.php:
<style rel="stylesheet" type="text/css">
body {
font-size:20pt;
}
</style>
<body>
<?php
require 'config.php';
$conn = mysqli_connect( $db_host, $db_username, $db_password, $db_name);
// Create connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Name, Comment FROM demo";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//echo "<table><tr><th>Name</th><th>Comment</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "<tr><td>".$row["Name"]." ".$row["Comment"]."</td></tr>";
echo "<fieldset>From : ".$row["Name"]."<br>".$row["Comment"]."<br></fieldset><br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
的config.php:
<?php
/*Configuration Settings*/
$db_username = 'root';
$db_password = 'root';
$db_name = 'Event';
$db_host = 'localhost';
$port = 8889;
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
?>