我是PHP和HTML编码的新手,所以如果这看起来非常明显,我很抱歉。
我的问题是:一旦用户登录并访问我网站的主页,在他们点击表单上的提交之前,$str_result
和$str_comments
就会显示。
这是我的代码:
<?php
//If connected to the database get services names from database and write out DropDownMenu
mysqli_select_db($db_server, $db_database);
$query = "SELECT ID, Name FROM categories ORDER BY Name";
$result = mysqli_query($db_server, $query);
if (!$result) die("Query failed: " . mysqli_error($db_server));
while($row = mysqli_fetch_array($result)){
$str_options .= "<option value= '" . $row[ 'ID'] . "'>";
$str_options .= $row['Name'];
$str_options .= "</option>";
}
mysqli_free_result($result);
// Your code here to handle a successful verification
$str_result = "<h2>Thanks for your search! Services avaliable are:" .
$category = clean_string($db_server, $_POST["categories"]) . "</h2>";
?>
<!--form-->
<form method="post" action="nihome.php"><p>I am searching for</p>
<select name="categories"><?php echo $str_options; ?></select>
<br />
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
<?php
//Capture form data, if anything was submitted
if (isset($_POST['categories']) and ($_POST['categories'] != '')){
$category = clean_string($db_server, $_POST['categories']);
// create the SQL query
$query = "SELECT salon.ID AS ID, categories.Name as Category, salon.salon_name AS Salon, services.name AS Service, servicesoffered.price AS price FROM services
JOIN categories ON services.cID = categories.ID
JOIN servicesoffered ON servicesoffered.serviceID = services.ID
JOIN salon ON servicesoffered.salonID = salon.ID WHERE categories.ID=$category";
// query the database
mysqli_select_db($db_server, $db_database);
$result = mysqli_query($db_server, $query);
if (!$result) die("Database access failed: " . mysqli_error($db_server));
// if there are any rows, print out the contents
while ($row = mysqli_fetch_array($result)) {
$str_result .= '<h3>' . $row['Salon'] . ',</h3><p>' .
$row['Service'] . ", £" .
$row['price'] .'</p>' .<a href="salonpage.php?salonid=' . $row['ID'] .'">Click here to view or add to salon reviews</a>';
}
if($str_result == "") $str_result = "<h2>No services found</h2>";
} else {
$str_result = '<h2>No service was requested</h2>';
}
mysqli_close($db_server);
echo $str_result;
echo $str_comments;
?>
答案 0 :(得分:0)
您实际上应该在if()
处写// Your code here to handle a successful verification
:
if (!empty($_POST) /* && all your other validation routines */)
{
$str_result = "<h2>Thanks for your search! Services avaliable are:" .
$category = clean_string($db_server, $_POST["categories"]) . "</h2>";
}
此外,您在'
<a href="salonpage.php?
答案 1 :(得分:0)
在脚本的开头,您设置了这个:
$str_result = "<h2>Thanks for your search! Services avaliable are:"
(此外,该行上有一个尾随点,可能应该是;
)
然后检查以下内容:
if($str_result == "") $str_result = "<h2>No services found</h2>";
当然不会发生这种情况,因为它永远不会是一个空字符串。
如果您执行此类操作,最后的if
语句应该正确触发,您应该得到预期的结果:
$str_result = "";
[ ... ]
if (isset($_POST['categories']) and ($_POST['categories'] != '')){
// Set it inside this if statement.
// So it will stay empty when nothing was submitted
$str_result = "<h2>Thanks for your search! Services avaliable are:"
答案 2 :(得分:0)
你应该试试这个
if($str_result == "")
{ $str_result = "<h2>No services found</h2>";
} else {
$str_result = '<h2>No service was requested</h2>';
}
mysqli_close($db_server);
echo $str_result;
echo $str_comments;
}
这里有两件事非常重要。
第一:
if($str_result == "")
{ $str_result = "<h2>No services found</h2>";
}
您忘了开始{
和第二:你应该保留
echo $str_result;
echo $str_comments;
在}
内,只有在点击按钮时才会执行。希望这可以帮助
和强>
$str_result = "<h2>Thanks for your search! Services avaliable are:" ;
以;
答案 3 :(得分:0)
首先纠正这一行
你应该在<a href=........>'
之前单引号
`$row['price'] .'</p>' .'<a href="salonpage.php?salonid=' . $row['ID'] .'">Click here to view or add to salon reviews</a>';
和
将$str_result
和$str_comments
封装在isset
中,然后它们只会在categories
变量存在时出现。
答案 4 :(得分:0)
你应该将代码包含在if条件中。
即
if (!empty($_POST){
if($str_result == "") $str_result = "<h2>No services found</h2>";
} else {
$str_result = '<h2>No service was requested</h2>';
}
}
另外,你在<a href="salonpage.php?