我想更新mysql表但只有一次。如果你想再次更新它,它应该给出消息" ID已经在使用中,请登录"
<?php
If(isset($_POST['login'])){
$Pin=$_GET['pin'];
$ID =$_POST['ID'];
if($Pin!=''){
$result = mysql_query("SELECT * FROM pin WHERE Pin = '$Pin'");
$test = mysql_fetch_array($result);
mysql_query("UPDATE pin SET appid ='$num' WHERE Pin= '$Pin'")
or die(mysql_error());
header("location:compet_applicant.php");
}
}
?>
答案 0 :(得分:1)
我认为您应该首先在field
表格中再添加一个number_update
,如:pin
。默认值为0
第一次更新时,它的值为1
,您可以检查该值,
如果是1
,则会提醒ID Already in Use, Pls login
。如果是0
,则允许更新
$result = mysql_query("SELECT * FROM pin WHERE Pin = '$Pin'");
$test = array();
while ($row = mysql_fetch_array($result)) {
$test[] = array_map('utf8_encode', $row);
}
if($test["number_update"] == 1) { //Checking already updated
//Notify user that they have already updated
} else {
mysql_query("UPDATE pin SET appid ='$num' WHERE Pin= '$Pin'")
or die(mysql_error());
}
答案 1 :(得分:0)
<?php
if (isset($_POST['login'])) {
$Pin = $_GET['pin'];
$ID = $_POST['ID'];
if ($Pin != '') {
$result = mysql_query("SELECT * FROM pin WHERE Pin = '$Pin'");
$test = array();
while ($row = mysql_fetch_array($result)) {
$test[] = array_map('utf8_encode', $row);
}
if ($test["number_update"] == 1) { //Checking already updated
//Notify user that they have already updated
} else {
mysql_query("UPDATE pin SET appid ='$num',number_update = 1 WHERE Pin= '$Pin'") or die(mysql_error());
}
header("location:compet_applicant.php");
}
}
?>
按照Neo先生的建议在数据库表中再添加一个字段“number_update”,并用上面的代码替换您的代码。
答案 2 :(得分:0)
应该有一个记录表的每个更新的日志表。如果找到记录,那么系统应阻止该特定记录的进一步更新。
<?php
If(isset($_POST['login'])){
$Pin=$_GET['pin'];
$ID =$_POST['ID'];
if($Pin!=''){
$update_count = mysql_query("SELECT * FROM Audit_Table_Update WHERE Field_name = 'Pin' and field_value='$pin');
if(!mysql_num_rows($update_count)){
$result = mysql_query("SELECT * FROM pin WHERE Pin = '$Pin'");
$test = mysql_fetch_array($result);
mysql_query("UPDATE pin SET appid ='$num' WHERE Pin= '$Pin'")
or die(mysql_error());
mysql_query("INSERT INTO AUDIT_TABLE_UPDATES (TABLE_NAME, FIELD_NAME, FIELD_VALUE,TIME_UPDATED) VALUES('Pin','appid',$num,systemtime)")
or die(mysql_error());
header("location:compet_applicant.php");
} // end if update count
}
}
?>