[英]MySQL update column and return value from another column in the same table
[英]How Can I Query for Unique Value and Update another Field in Same Table If return True mysql php
如果 Pin 字段值與用戶輸入 pin 相同,我想更新 mysql 表 pin。在這種情況下,我想在選擇查詢返回 true 后更新 pin 表中的 appid 字段。
<?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");
}
}
?>
我認為您應該先添加一個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());
}
<?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”,並將您的代碼替換為上述代碼。
應該有一個日志表來記錄表的每次更新。 如果找到記錄,則系統應阻止對該特定記錄的進一步更新。
<?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
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.