if else语句重定向到另一个页面 [英] If else statement that redirects to another page

查看:504
本文介绍了if else语句重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP / SQL很糟糕,所以任何帮助都会受到赞赏。

I'm terrible with PHP/SQL, so any help would be appreciated.

基本上我有一个表格贴出'firstname'和& '姓'到另一页。我想要该页面做什么,检查用户的名字是否已经在'Members'表上。如果是,我希望它继续加载此页面,但如果它们不在数据库中,我希望将查看器重定向到现有的注册页面。

Basically I have a form that posts the values 'firstname' & 'surname' to another page. What I want that page to do, is check to see if the user's name is already on the table 'Members'. If it is I want it to continue loading this page, but if they aren't on the database, I want the viewer to be re-directed to an existing sign up page.

以下是我一直在处理的代码,我不确定我是否正朝着正确的方向前进。

Here is the code I've been working on, I'm not sure if I'm heading in the right direction or not.

<?php
$con = mysql_connect("localhost","site","xxxxxxxx");
if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }

mysql_select_db("site", $con);

$query = "SELECT * FROM Members WHERE firstname='$_POST[firstname]' and surname='$_POST[surname]'";  
$result = mysql_query($query);

if ($result==$something)
   {
   echo "great success"; //user continues loading page
   }
else
   {
   echo "fail"; //user is redirected to sign up page
   }
?>


推荐答案

这样做可以解决问题:

<?php

$con = mysql_connect( "localhost", "site", "xxxxxxxx" );
if ( !$con ) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("site", $con);

1,删除sql注入部分至少如下:

1st, remove the sql injection part at least like this:

$firstname = strip_tags( $_POST[ 'firstname' ] );
$surname = strip_tags( $_POST[ 'surname' ] );

第二,我没有更改它,但你需要删除 * 并仅输入您要加载的特定值。即使这些都是值,仍然手动编写。

2nd, I didn't change it, but you need to remove the * and enter only specific values you want to load. Even if those are all the values, still write them manually.

$query = "SELECT * FROM Members WHERE firstname='" . $firstname . "' and surname=' " . $surname. "'";  
$result = mysql_query( $query );

第3,你可以检查行数,如果你有一些价值,有一个条目与那些变量

3rd, you can check for row count, if you got some value, there is an entry with those variables

if ( mysql_num_rows($result) >= 1 ) {
    // the page you want
} else {
    // redirect user to another page
    header( "Location: signup.php" ); die;
}

?>

编辑:

考虑向查询添加一些唯一请求。如果两个用户具有相同的姓名和姓氏,或者一个新用户想要加入,但名称和姓氏已经在数据库中,将会发生什么......

Think adding some unique requests to your query. What will happen if two users have identical names and surnames or if a new one wants to join, but the name and lastname is already in the db ...

这篇关于if else语句重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆