是的,这应该是非常基本的PHP,但是我不能为我的生活弄清楚为什么这不起作用。
我有一个表单,在提交时应该抓取字段然后将数据插入数据库。我在我的index.php上有一个部分实际上从数据库中提取信息并且工作正常,所以我不能看到这是一个连接问题。
db_connection.php 我已将此处的信息删除为我的数据库,但一切正确
<?php
define("DB_SERVER","**********");
define("DB_USER","*************");
define("DB_PASS","*********");
define("DB_NAME","reviews");
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
if(mysqli_connect_errno()){
die("database connection failed");
}
?>
的functions.php
<?php
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
function confirm_query($result){
if(!$result){
die("database query failed");
}
}
function find_reviews($connection){
$query = "SELECT * ";
$query .= "FROM reviews";
$result = mysqli_query($connection,$query);
confirm_query($result);
while($reviews = mysqli_fetch_assoc($result)){
$output = "<li class=\"first\">";
$output .= $reviews["name"];
$output .= "</li>";
$output .= "<li class=\"second\">";
$output .= $reviews["company"];
$output .= "</li>";
$output .= "<li class=\"third\">";
$output .= $reviews["comment"];
$output .= "</li>";
$output .= "<li class=\"line\"></li>";
echo $output;
}
mysqli_free_result($result);
return $reviews;
}
?>
的index.php 要注意这一点,函数find_reviews()可以工作并从数据库中获取信息。以上&#34; db_connection.php&#34;和&#34; functions.php&#34;在index.php
中是必需的<?php
require("includes/header.php");
$page_title = "All County Road Markings";
$description = "Specialising in Road Marking & Car Park Lining. We are a professional established road marking service with over 20 years experience";?>
<?php require("includes/db_connection.php"); ?>
<?php require("includes/functions.php"); ?>
<?php if(isset($_POST["submit"])){
$name = $_POST["name"];
$company = $_POST["company"];
$comment = $_POST["comment"];
$query = "INSERT INTO `reviews` (name,company,comment) VALUES ('$name','$company','$comment')";
$result=mysqli_query($connection,$query);
confirm_query($result);
redirect_to("index.php");
}
?>
<div id="banner"></div>
<div id="paragraph">
<h2>All County Road Markings are a professional established road marking<br>service with over 20 years experience within the industry</h2>
<hr style="width: 1050px;">
</div>
<div id="content">
<div id="left">
<div class="slot">
<div class="top carpark"> </div>
<div class="linkbar">
<h1>Car Parks</h1>
</div><div class="linktext">
<ul class="comments display">
<li>- Car Parking Bays</li>
<li>- Disabled Parking Bays</li>
<li>- Parent and Child Bays</li>
<li>- Lettering</li>
<li>- Hatchings</li>
<li>- Arrows</li>
<li>- Customised Lettering</li>
</ul>
</div>
</div>
<div class="slot">
<div class="top roadmark"> </div>
<div class="linkbar">
<h1>Road Markings</h1>
</div>
<div class="linktext">
<ul class="comments display">
<li>- Hatchings</li>
<li>- Centre Lines</li>
<li>- Double Yellow lines</li>
<li>- Give way junctions</li>
<li>- Reinstatement of existing markings</li>
</ul>
</div>
</div>
<div class="slot">
<div class="top sportcourt">
</div>
<div class="linkbar">
<h1>Sports/Playground Court</h1>
</div>
<div class="linktext">
<ul class="comments display">
<li>- Tennis Court</li>
<li>- Basketball Court</li>
<li>- 5 A side Court</li>
<li>- Netball Court </li>
<li>- Reinstatement of existing markings</li>
<li>- Custom designs available</li>
</ul>
</div>
</div>
</div>
<div id="right">
<div id="rightbar">
<h2 style="color: white;">Testimonials</h2>
</div>
<div id="comment">
<ul class="comments">
<?php echo find_reviews($connection); ?>
</ul>
</div>
<div id="write">
<p style="margin: 0px; padding-top: 5px;color: grey; font-size: 1.25em;">Click to write a review...<p>
</div>
</div>
<a href="contact.php"><div id="quote"><div class="link-text">contact us</div></div></a>
</div>
<div id="add">
<div id="close"></div>
<form action="index.php" method="POST">
<div id="name"> Name:<br/><span>Please Enter Full Name</span>
<input type="text" name="name" id="textbox">
</div>
<div id="company"> Company<br/><span>Please Enter Company Name</span>
<input type="text" name="company" id="textbox1">
</div>
<div id="review"> Review<br/><span>Please Enter Review</span>
<textarea name="comment" id="reviewComment"></textarea>
</div>
<div id="save">
<input type="submit" name="submit">
</div>
</form>
</div>
</body>
</html>
好的,所以任何人都可以看到我做错了吗?就像提交表单时一样,它只是不插入数据库。
修改::
当我尝试echo mysqli-&gt;错误;我得到以下错误:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /hermes/bosoraweb140/b484/ipg.allcountyroadmarking/index.php on line 17
所以我把它改成了
if(!results){
echo "hello";
}
我没有看到&#34;你好&#34;所以它表明我甚至没有进入它处理帖子字段的部分
自行决定:::::
我修好了,我改变了行动=&#34; index.php&#34;行动=&#34;&#34;
我不明白,它应该有效,但由于某种原因,行动空白有效。两者都应该可以接受!
答案 0 :(得分:0)
$ _POST ['submint']的if语句在每次提交时都可能为false。也许试试!空($ _ POST)
答案 1 :(得分:0)
设置为
的表单操作 <form action="index.php" method="POST">
不起作用,但我不明白为什么不能引用当前页面。
然而,将其更改为可以解决问题。
<form action="" method="POST">
答案 2 :(得分:0)
嗯,你在相同的文件中得到了php,这也许就是为什么action =“”如果它默认为同一页面就行了.action =“index.php”应该在前面加上当前的网址。也许尝试将完整的网址放在action =“index.php的url”中,看看是否有效。如果是,则action =“index.php”不得转到正确的文件。