How Can I Query for Unique Value and Update another Field in Same Table If return True mysql php

后端 未结 3 351
一个人的身影
一个人的身影 2021-01-29 10:46

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 retur

相关标签:
3条回答
  • 2021-01-29 10:58
    <?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.

    0 讨论(0)
  • 2021-01-29 11:10

    I think you should add one more field like: number_update into your pin table first. 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. If it is 0, allow to update

    $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());
    }
    
    0 讨论(0)
  • 2021-01-29 11:19

    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
        }
        }
        ?>  
    
    0 讨论(0)
提交回复
热议问题