我的代码创建随机key
并将其存储database
在按钮单击中,keygen
是按钮名称,if
用户/客户已经拥有它key
从数据库中获取的密钥,单击else
它会生成一个新的key
并将其存储在database
. 有没有一种方法可以实现这一点,而无需将其存储key
在数据库中,并且每次都为特定用户/客户获取相同的密钥。
代码:
<?php
include('session.php');
$result = mysql_query("SELECT * FROM customer WHERE customer_no = '$customer_no'");
$row = mysql_fetch_array($result);
if (isset($_POST['keygen'])){
$customer_no = $_POST['customer_no'];
$customer_no = mysql_real_escape_string($customer_no);
$result = mysql_query("SELECT * FROM customer WHERE customer_no = '$customer_no'");
while ($row = mysql_fetch_assoc($result)) {
$keyString = $row['key'];
if($keyString == ""){
$keyString = mysql_real_escape_string($keyString);
$query = mysql_query("UPDATE customer SET `key` = '$keyString' WHERE customer_no = '$customer_no'");
}
else{
$keyString = $row['key'];
}}}
function generateRandomString($length = 10) {
$characters = '23456789ABCDEFGHJKMNPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
?>
我的 HTML 是,
<div id="content" class="box2">
<div class="login">
<form action="" method="post" style="margin:12px;">
<table class="nostyle">
<tr>
<td align="center">
<label style="font-size:16px;"><strong>Customer ID: </strong></label>
<select name="customer_no">
<?php $result_customer= mysql_query('SELECT customer_no FROM customer ORDER BY customer_no'); ?>
<?php while($row_customer= mysql_fetch_assoc($result_customer)) { ?>
<option <?php if ($row_customer['customer_no']=='') { ?> selected="selected"<?php } ?>> <?php echo htmlspecialchars($row_customer['customer_no']); ?> </option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td align="center"><label style="font-size:16px;"><br /><strong>Register Key: </strong> </label>
<input type="text" id="key" class="input-text" name="key" size="20" align="middle" value = " <?=$row["key"];?>"></td>
</tr>
<td align="center"><br /><input type="submit" id="keygen" class="input-submit" name="keygen" value="Generate" onclick=""/>
</td>
</tr>
</table>
</form>
</div>
</div>
请帮忙,我是新手。