我一直在忙着学习php / mysql 为了确保我理解我正在做什么和做什么,我已经开始了一个项目来构建我自己的php游戏。
我到了创建注册表的重点 表单按照编程方式工作。
在表单中有5个不同类的选择,其思路是当选择某个类时,该类的起始统计数据将被放入数据库。
这是类选择器,是的我知道它有die()
,但是它显示了它"工作"。
我的问题是:
如何添加额外的"隐藏"输入? (如hp,mana,def,>>>>)
我尝试将die()
替换为:
$query = " INSERT INTO user (hp,deff) VALUES (3,2)";
//Class selector
if($_POST['class'] == 'ssd')
{
die("Selected ssd as Class.");
}
elseif($_POST['class'] == 'holo')
{
die("Selected holo as Class.");
}
elseif($_POST['class'] == 'cleric')
{
die("Selected clerric as Class.");
}
elseif($_POST['class'] == 'wizzard')
{
die("Selected wizz as Class.");
}
elseif($_POST['class'] == 'floppy')
{
die("Selected pitcher as Class.");
}
elseif(empty($_POST['class']))
{
die("Select a Class.");
}
完整代码:
<?php
require("common.php");
if(!empty($_POST))
{
// Ensure that the user has entered a non-empty username
if(empty($_POST['username']))
{
die("Please enter a username.");
}
// Ensure that the user has entered a non-empty password
if(empty($_POST['password']))
{
die("Please enter a password.");
}
//Class selector
if($_POST['class'] == 'ssd')
{
die("Selected ssd as Class.");
}
elseif($_POST['class'] == 'holo')
{
die("Selected holo as Class.");
}
elseif($_POST['class'] == 'cleric')
{
die("Selected clerric as Class.");
}
elseif($_POST['class'] == 'wizzard')
{
die("Selected wizz as Class.");
}
elseif($_POST['class'] == 'floppy')
{
die("Selected pitcher as Class.");
}
elseif(empty($_POST['class']))
{
die("Select a Class.");
}
//Character name
if(empty($_POST['charname']))
{
die("Please enter a Charactername.");
}
// Make sure the user entered a valid E-Mail address
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
die("Invalid E-Mail Address");
}
//SQL query to see whether the username entered by the user is already in use
$query = "
SELECT
1
FROM user
WHERE
username = :username
";
$query_params = array(
':username' => $_POST['username']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query:");
}
$row = $stmt->fetch();
if($row)
{
die("This username is already in use");
}
// email address unique check
$query = "
SELECT
1
FROM user
WHERE
email = :email
";
$query_params = array(
':email' => $_POST['email']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query");
}
$row = $stmt->fetch();
if($row)
{
die("This email address is already registered");
}
//charname unique check
$query = "
SELECT
1
FROM user
WHERE
charname = :charname
";
$query_params = array(
':charname' => $_POST['charname']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query");
}
$row = $stmt->fetch();
if($row)
{
die("This charactername is already taken");
}
$query = "
INSERT INTO user (
username,
charname,
password,
salt,
email,
class
) VALUES (
:username,
:charname,
:password,
:salt,
:email,
:class
)
";
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$password = hash('sha256', $_POST['password'] . $salt);
for($round = 0; $round < 65536; $round++)
{
$password = hash('sha256', $password . $salt);
}
$query_params = array(
':username' => $_POST['username'],
':password' => $password,
':salt' => $salt,
':email' => $_POST['email'],
':charname' => $_POST['charname'],
':class' => $_POST['class']
);
try
{
// Execute the query to create the user
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: ");
}
die("Redirecting to login.php");
}
?>
<h1>Register</h1>
<b>NOTE! A admin or moderator will NEVER ask you for a loginname or password. Only a Character is needed for help or problems.</b>
<form action="register.php" method="post">
<br />Loginname<b> keep this private!</b>:<br />
<input type="text" name="username" value="" /><br />
Charactername<b> Advice is to use different name then the loginname, this is how people will see you</b>: <br />
<input type="text" name="charname" value="" /><br />
E-Mail: <br />
<input type="text" name="email" value="" /><br />
Password: <br />
<input type="password" name="password" value="" /><br />
Class: <br />
<input type="radio" name="class" value="ssd"> Solid State Defender<br />
<input type="radio" name="class" value="holo"> Hologram<br />
<input type="radio" name="class" value="cleric"> Office Cleric<br />
<input type="radio" name="class" value="wizzard"> IT Wizzard<br />
<input type="radio" name="class" value="floppy"> Floppy Picher<br /><br />
<input type="submit" value="Register" />
</form>
答案 0 :(得分:0)
再创建两个表:
Classes
- &gt;此表应包含ClassId
,ClassName
和Starting Class Properties
(即:HP,Mana等)。
UserProperties
- &gt;此表应包含包含对每个用户的引用的列,PropertyName
和PropertyValue
创建新用户时,从Classes
表中加载选定的类,并使用所选类的初始属性填充UserProperties
表。
现在,您可以查询和更新特定用户的特定属性。
答案 1 :(得分:0)
您不需要任何隐藏的输入字段,因为这些字段可能是为每个类预定义的。这意味着您可以将它们存储在数据库中的单独表(例如Classes(classID, classLabel, HPInitial, ManaInitial))
)上。
然后在您的查询中,您只需添加:
$query = ' INSERT INTO user (...,mana, hp,...) VALUES
(...,
(SELECT ManaInitial FROM Classes WHERE classLabel = :class),
(SELECT HPInitial FROM Classes WHERE classLabel = :class),
...);
如果您向类添加更多属性,这种方法会导致很多问题,因为您将拥有大量子查询。更实际的方法是预先选择属性并在执行之前将它们添加到插入查询中。
答案 2 :(得分:0)
非常有趣的解决方案,我喜欢你的axlj
我实际上一直在搞乱,并找到了解决方案,告诉我你对它的看法。 仍在考虑将基本统计数据放在表格中,但我很高兴这实际上有效。
//Class selector and full userinfo prepare
if($_POST['class'] == 'ssd')
{
$query = "INSERT INTO user (username,charname,password,salt,regkey,email,class,hp,mana,atkpwr,defpwr,speed,crit,luck) VALUES (:username,:charname,:password,:salt,:regkey,:email,:class,'4','0','3','3','1','1','5')";
}
elseif($_POST['class'] == 'holo')
{
$query = "INSERT INTO user (username,charname,password,salt,regkey,email,class,hp,mana,atkpwr,defpwr,speed,crit,luck) VALUES (:username,:charname,:password,:salt,:regkey,:email,:class,'4','2','5','0','1','1','5')";
}
elseif($_POST['class'] == 'cleric')
{
$query = "INSERT INTO user (username,charname,password,salt,regkey,email,class,hp,mana,atkpwr,defpwr,speed,crit,luck) VALUES (:username,:charname,:password,:salt,:regkey,:email,:class,'4','3','2','1','1','1','5')";
}
elseif($_POST['class'] == 'wizzard')
{
$query = "INSERT INTO user (username,charname,password,salt,regkey,email,class,hp,mana,atkpwr,defpwr,speed,crit,luck) VALUES (:username,:charname,:password,:salt,:regkey,:email,:class,'3','3','5','1','1','1','5')";
}
elseif($_POST['class'] == 'floppy')
{
$query = "INSERT INTO user (username,charname,password,salt,regkey,email,class,hp,mana,atkpwr,defpwr,speed,crit,luck) VALUES (:username,:charname,:password,:salt,:regkey,:email,:class,'2','1','3','2','1','1','10')";
}
elseif(empty($_POST['class']))
{
die("Chose a class plix.");
}