I am trying to create a program that creates random registration keys and stores it in the database.If the user/customer already has a registration code it displays that key stored in the database in a text box on click 'else' it generates a new key and stores it in the database.The problem is i am not being able to store the key in the database.My code is:
<?php
if (isset($_POST['keygen'])){
$customer_no = $_POST['customer_no'];
$result = mysql_query("SELECT * FROM customer WHERE customer_no = '$customer_no'");
$row = mysql_fetch_array($result);
$keyString = $row['key'];
if($keyString == ""){
$keyString = generateRandomString();
$query = "UPDATE 'customer' SET key ='$keyString' WHERE customer_no = '$customer_no'";
mysql_query($query);
echo $keyString;
}
else{
echo $keyString;
}
}
function generateRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
?>
this is my 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>
I am a newbie, and am not that sure about the code.Please help!