我有这段代码,无法让它工作:
$character_set_array = array();
$character_set_array[] = array('count' => 8, 'characters' => '0123456789');
$temp_array = array();
foreach ($character_set_array as $character_set) {
for ($i = 0; $i < $character_set['count']; $i++) {
$temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
}
}
shuffle($temp_array);
$pinstart = 'AA';
$pinend = implode('', $temp_array);
$newpin = $pinstart.$pinend;
function regenerate_pin($pin)
{
if ($PIN == 'PIN') { return ''; } else {
$pin = mysql_real_escape_string($pin); // SECURITY!
$result = mysql_query("SELECT pin FROM pins WHERE pin='$pin' LIMIT 1");
if(mysql_num_rows($result) == 0) {
return 'This pin has already been used';
} else {
$sql = mysql_query("UPDATE pins SET pins='$newpin'");
return "The pin has been regened, the new pin is '.$newpin.'";
} }
}
基本上我想让它做的是这样的: - 从HTML输入框中获取引脚, - 检查数据库中是否存在引脚(有效) - 如果存在,用$ newpin替换$ pin并打印$ newpin,这就是我的表格的样子
感谢您的阅读,希望我找到一个快速的解决方案。
答案 0 :(得分:1)
您的regenerate_pin()
函数没有看到$newpin
变量,而您的UPDATE SQL可能出错:
将"UPDATE pins SET pins='$newpin'"
更改为"UPDATE pins SET pin='$newpin'"
对于该功能,您有两个选择:
添加全球$ newpin;函数regenerate_pin($ pin)-code
中的行function regenerate_pin($pin)
{
global $newpin;
...
}
或将$ newpin变量作为一个arbument发送到regenerate_pin-function。'
function regenerate_pin($pin, $newpin)
{
...
}
你必须像这样调用你的regenerate_pin函数
regenerate_pin('oldpin', 'newpin');
答案 1 :(得分:0)
似乎我误解了 但最好改变:
$result = mysql_query("SELECT pin FROM pins WHERE pin='$pin' LIMIT 1");
if(mysql_num_rows($result) == 0) {
在
$result = mysql_fetch_row(mysql_query("SELECT COUNT(pin) FROM pins WHERE pin='$pin' LIMIT 1"));
if($result[0] == 0) {