我想创建页面,如果有人已经输入他们的数据,它会重定向到一个页面,它说'抱歉,您已经做出选择'。最简单的字段是参加1,其中值是'是'或'否',因此如果它是这些值中的任何一个,它将重定向到我想要的页面。代码为我的网页是在这里:http://pastebin.com/1D6PrmBvPHP代码重定向,如果mysql字段是完整的
回答
include('connect.inc');
if ($rows['food'] == "yes" OR $rows['food'] == "no") {
header('location: mypagelocation.html');
}
确保没有任何输出之前。
我总是很小心推荐'或',因为很多人似乎认为它是'||'的同义词。显然,你的例子工作正常。 – cmbuckley 2012-02-07 01:13:07
是的,当没有赋值时,这会为初学者提供更易读的代码。 – Shomz 2012-02-07 01:24:31
@cbuckley它取决于'用户'连接的是什么。我有一个'食品'字段的用户名和密码。我在哪里添加: mysql_query(“SELECT * FROM users WHERE(username ='”。mysql_real_escape_string($ _ POST ['username'])。“')and(password ='”。mysql_real_escape_string(md5($ _ POST [''密码']))。“')”); – Jacob 2012-02-07 01:41:43
试试这个。
include('connect.inc');
if (($rows['food'] == "yes") || ($rows['food'] == "no")) {
header('location: mypagelocation.html');
exit;
}
或者你也可以这样写。
include('connect.inc');
if (in_array($rows['food'], array("yes","no"))) {
header('location: mypagelocation.html');
exit;
}
不知道downvote;这似乎是最好的答案(除了原始问题中存在的条件语法错误;固定)。 – cmbuckley 2012-02-07 01:14:56
@cbuckley:没关系。这不仅是有条件的问题。行标题(位置:mypagelocation.html');也不正确。出口;声明应该在if条件范围内。 – 2012-02-07 01:39:47
@ Prasad Rajapaksha它取决于用户连接的是什么。我有一个'食品'字段的用户名和密码。所以我只想检查某个特定用户在'食品领域'中是否有'是'/'否'。这是正确的代码吗?如果是这样,我插入它:mysql_query(“SELECT * FROM users WHERE(username ='”。mysql_real_escape_string($ _ POST ['username'])。“')and(password ='”。mysql_real_escape_string(md5($ _ POST ['password']))。“')”); – Jacob 2012-02-07 01:53:08
include('connect.inc');
if (isset($rows['food'])) {
header(location: mypagelocation.html');
exit;
}
else{
// $rows['food'] is not set
}
全码:如果该值是YES和NO
<?php
session_start();
require_once("connect.inc");
if(!isset($_SESSION['username'])){
header('Location: connect.php');
exit;
}else{
$sql = "SELECT attendance1 FROM user WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if(($row[0] == "Yes") || ($row[0] == "No")){
header("Location: redirect_page.html");
exit;
}
}
if(isset($_POST['submit'])){
$sql = "UPDATE user SET attendance1= '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET gender= '" . mysql_real_escape_string($_POST['gender']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
header("Location: index.html", true, 303); // Look up "303: See Other"
exit;
}
?>
- 1. PHP/MySQL的重定向页面,如果表中的值是0
- 2. 如果字段来自mysql,则重定向
- 3. 重定向如果PHP代码没有找到视频
- 4. 我如何更改Server.Transfer的代码PHP重定向代码member_id
- 5. 根据mysql字段值重定向
- 6. 这段代码是否安全 - PHP && MySQL
- 7. 执行代码后PHP重定向
- 8. php脚本重定向代码问题
- 9. 301重定向完整的网址
- 10. htaccess重定向不完整的任务
- 11. PHP MYSQL更新/重定向
- 12. PHP MySQL如何组织这段代码?
- 13. 获取PHP中的完整URL以重定向到SSL
- 14. PHP - 完成流程后重定向PHP
- 15. htaccess重定向到完整路径url
- 16. 这是ServiceKnownTypeAttribute的完整源代码吗?
- 17. 如果不是重定向重定向到原来的页面
- 18. bash将输出重定向到文件,但结果不完整
- 19. JavaScript对象返回完整的代码而不是结果
- 20. 如果URL是正确的,点击这个按钮,这段代码是否会将我重定向到register.php?
- 21. 查看完整重定向路径和HTTP状态代码(在Chrome中)
- 22. 优化重定向代码
- 23. HTTP重定向是否需要完整的URL?
- 24. 重定向时的代理重写位置标题字段
- 25. 如何从JavaScript代码片段重定向到html页面?
- 26. 确定字符串的完整性 - PHP
- 27. Dreamweaver中的PHP MySQL的URL重定向
- 28. 如何使用js代码重定向
- 29. PHP代码在wordpress中使用自定义字段的PHP代码
- 30. MySQL SUM如果字段b =字段a
your're检查。它不能既是肯定也不是。更好的是。如果您想要仅在var为空时重定向,请使用if($ rows ['food']) – 2012-02-07 02:36:21
@Jacob不要忘记将所选答案标记为已接受。 – Grexis 2012-02-07 04:00:28