我想创建一个系统,以便用户可以注册/登录,然后继续进行操作,将它们定向到诸如blahurl.com/nations/customid之类的自定义页面,注册时已经生成了每个ID(称为UserID),我希望能够访问blahurl.com/nations/92,然后该人将弹出并显示其SQL信息。
我想要这样,当他们注册时,他们已经可以访问这些页面,我不需要为每个注册的人创建一个页面。我将如何去做呢?然后通过他们的MySQL用户名搜索它们?
当人们注册时,它们会自动放入我的Submit-form.php中,它是:
<?php
$username=$_POST['UserName'];
$password=$_POST['Password'];
$leadername=$_POST['LeaderName'];
$nationname=$_POST['NationName'];
$email=$_POST['Email'];
//Database connection
require_once("config.php");
require_once("checkregister.php");
//mysql query to insert value to database
$query=mysql_query("INSERT INTO users (UserName, Password, LeaderName, NationName, Email)
VALUES ('$username', '$password', '$leadername', '$nationname', '$email')");
//if value inserted successyully disply success message
if($query)
{
echo '<div style="color:#008000; font-weight:bold;">Your Account has been Created! <del>Check your Email for Verification Code!</del> in the future you will need to Verify your Account, for right now just <a href="../Login.php">go back to the Website!</a></div>';
} else
{
//error message
echo '<div style="color:#c24f00; font-weight:bold;">Registration Failed: Could not INSERT to Query! (Could not Connect to Database)<br><a href="../home.php">Back to Website</a></div>';
}
?>
您已经问过如何链接到非花哨的URL,这很容易。您只需要获取主键,该主键将在您的自动增量列中创建:
提交form.php
// Create user
// @todo Fix injection vuln
$query=mysql_query( ... );
// If successful...
if($query)
{
// Get the inserted primary key
$userId = mysql_insert_id();
?>
<div style="color:#008000; font-weight:bold;">
Your account has been created,
<a
href="nations.php?userId=<?php echo $userId ?>"
>go back to the website!</a>
</div>
<?php
}
然后,您可以在另一页中阅读它:
nationals.php
<?php
// Read it only if it exists
$userId = isset($_GET['userId']) ? $_GET['userId'] : null;
// Escape any apostrophes in the ID, to avoid SQL injection
$cleanUserId = mysql_real_escape_string($userId);
// Always check if it exists, the user may have tampered with the URL
$sql = "SELECT * FROM users WHERE UserID = '$cleanUserId'";
$query = mysql_query($sql);
// Do something with the result...
以下是一些一般性说明。这些可能一口气无法吸收,但是如果您对它们进行一些阅读,它们将在长期内为您提供帮助:
mysql_
不再维护调用。他们将在PHP 5.5+中引发通知。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句