[英]How Can I Query for Unique Value and Update another Field in Same Table If return True mysql php
I want to update mysql table pin if Pin field value is the same as the user input pin.In this case, I want to update appid field in the pin table once the the select query returns true.如果 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");
}
}
?>
I think you should add one more field
like: number_update
into your pin
table first.我认为您应该先添加一个
field
例如: number_update
到您的pin
表中。 Default value is 0
First time update, it will have value is 1
and you could check that value, if it is 1
, will alert ID Already in Use, Pls login
.默认值为
0
第一次更新,它的值为1
,您可以检查该值,如果是1
,将提醒ID Already in Use, Pls login
。 If it is 0
, allow to update如果为
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");
}
}
?>
Add one more field "number_update" in your database table as suggested by Mr. Neo and replace your code with the above code.按照 Neo 先生的建议,在您的数据库表中再添加一个字段“number_update”,并将您的代码替换为上述代码。
There should be a log table that record each updation of tables.应该有一个日志表来记录表的每次更新。 If the record found then system should prevent further updation for that particular record.
如果找到记录,则系统应阻止对该特定记录的进一步更新。
<?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.