我制作了一个简单的php应用程序,它可以从数据库中检索数据并在表格中显示。但问题是,它根本不起作用!我的意思是每当我提交表单时,由于某些原因,它不会向数据库插入新查询。此外,屏幕上没有出现错误,所以我不知道是什么问题。这是我的页面的完整代码:
<?php
error_reporting(E_ALL);
require 'connect.php';
require 'security.php';
$records = array();
if(!empty($_POST)){
if(isset($_POST['site_name'], $_POST['site_admin'], $_POST['site_desc'], $_POST['site_url'], $_POST['made_by'])){
$sitename = trim($_POST['site_name']);
$siteadmin = trim($_POST['site_admin']);
$sitedesc = trim($_POST['site_desc']);
$siteurl = trim($_POST['site_url']);
$madeyby= trim($_POST['made_by']);
if(!empty($sitename) && !empty($siteadmin) && !empty($sitedesc) && !empty($siteurl) && !empty($madeyby)){
$insert = $db->prepare("INSERT INTO list_of_sites (site_name, site_admin, site_desc, site_url, made_by, date_created) VALUES (?, ?, ?, ?, ?, NOW())");
$insert->bind_param('sssss',$sitename,$siteadmin,$sitedesc,$siteurl,$madeby);
if($insert->execute()){
header('Location: index.php');
die();
}
}
}
}
if($results = $db->query("SELECT * FROM list_of_sites")){
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Daygotar Application</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
/**********************************/
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}
#searchForm{
float:right;
top:0;
margin-bottom:20px;
}
</style>
</head>
<body>
<form id="searchForm">
<input type="text" name="search" placeholder="Search..">
</form>
<h3 style="font-size:30px;"><u>List Of Sites</u></h3>
<?php
if(!count($records)){
echo 'No records';
}else{
}
?>
<table>
<tr>
<th>Site Name</th>
<th>Site Admin</th>
<th>Site Description</th>
<th>Site URL</th>
<th>Made By</th>
<th>Date Created</th>
</tr>
<?php
foreach($records as $r){
?>
<tr>
<td><?php echo escape($r->site_name); ?></td>
<td><?php echo escape($r->site_admin); ?></td>
<td><?php echo escape($r->site_desc); ?></td>
<td><a title="goto this site" href="<?php echo escape($r->site_url); ?>" target="_blank"><?php echo escape($r->site_url); ?></a></td>
<td><?php echo escape($r->made_by); ?></td>
<td><?php echo escape($r->date_created); ?></td>
</tr>
<?php
}
?>
</table>
</hr style="margin-top:20px;">
<form action="" method="POST">
<div class="field">
<label for="site_name"><h4>Site name</h4></label>
<input type="text" name="site_name" id="site_name" autocomplete="off"></input>
</div>
<div class="field">
<label for="site_admin"><h4>Site admin</h4></label>
<input type="text" name="site_admin" id="site_name" autocomplete="off"></input>
</div>
<div class="field">
<label for="site_description"><h4>Site description</h4></label>
<input type="text" name="site_desc" id="site_desc" autocomplete="off"></input>
</div>
<div class="field">
<label for="site_url"><h4>Site url</h4></label>
<input type="text" name="site_url" id="site_name" autocomplete="off"></input>
</div>
<div class="field">
<label for="made_by"><h4>Made by</h4></label>
<input type="text" name="made_by" id="site_name" autocomplete="off"></input>
</div></br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
如果您知道如何解决这个问题,请告诉我......谢谢。
答案 0 :(得分:1)
所以我稍微调整了一下,只是为了稍微改进它,你可以稍微整理一下并为每个if!empty语句添加条件。
在插入查询中,您需要添加值,我在每个值下都使用'$ variable'。
<?php
error_reporting(E_ALL);
require 'connect.php';
require 'security.php';
$records = array();
if(isset($_POST['send'])) {
$sitename = trim($_POST['site_name']);
$siteadmin = trim($_POST['site_admin']);
$sitedesc = trim($_POST['site_desc']);
$siteurl = trim($_POST['site_url']);
$madeyby = trim($_POST['made_by']);
if (!empty($sitename)) {
if (!empty($siteadmin)) {
if (!empty($sitedesc)) {
if (!empty($siteurl)) {
if (!empty($madeyby)) {
$insert = $db->prepare("INSERT INTO list_of_sites (site_name, site_admin, site_desc, site_url, made_by, date_created) VALUES ('$sitename','$siteadmin','$sitedesc','$siteurl','$madeby', NOW())");
$insert->execute();
} else {
echo "Please fill in the Made by field.";
}
} else {
echo "Please add a site url.";
}
} else {
echo "Please add a site description.";
}
} else {
echo "Please add a site admin name.";
}
} else {
echo "Please add a site name.";
}
}
然后在HTML部分中将name =“”更改为
<input type="submit" name="send" value="Submit">