简体   繁体   English

如果返回 True mysql php,如何查询唯一值并更新同一表中的另一个字段

[英]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.

相关问题 MySQL 更新列并从同一表中的另一列返回值 - MySQL update column and return value from another column in the same table 如果结果返回true,我如何验证mysql中的存储代码并更新表 - How Can I Verify store code in mysql and update the table if result return true 如何通过换行获取字段中的值并更新到php mysql中具有相同ID的表中的列? - How to get the value in field by new line and update into the column in table with same id in php mysql? 如何在列中拆分或获取值并在同一表的另一列中更新? PHP的MySQL - How to split or get the value in column and update in another column in same table? php mysql php mysql用同一字段更新多个表? - php mysql update multiple table with same field? PHP mysql_query更新始终返回true - PHP mysql_query update always return true PHP变量值不更新MySQL表字段? - PHP variable value to NOT update MySQL table field? MySQL的选择查询结果为空,所以我怎么能在PHP中返回默认值 - mysql select query result is null so how can i return default value in php MySQL-如何用一个查询更新两个表,其中表2的值为true? - MySQL - How to update two tables with one query where table 2 value is true? 如何在php中检查mysql表的布尔字段并将0或1值转换为true / false - How to check boolean field of mysql table and convertion of 0 or 1 value to true/false in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM