doujing5435 2019-07-08 05:34
浏览 49
已采纳

如何使用php区分相同html表单中的不同输入

I'm building a chatbot in php I want the functionality of that chatbot to be that it is used for resetting the password, so it asks for employee's PIS_CODE which it uses as a primary key to change the password in the password column, you can see my database table.

enter image description here

see it has columns PIS_CODE and password, so I ask the user for PIS_CODE and then it asks the user for the new password and then it changes the password in the corresponding column

so I've been able to take the PIS_CODE and use it as a primary key to reset the password but the password which is reset is the PIS_CODE itself

enter image description here

see here I wanted to reset the password for 41000000 PIS_CODE but it reset the password to 41000000 itself. So it seems like my chatbot assumes the input value to be the pis code and it updates the password column with that value only, so my chatbot is not able to differentiate between different inputs. Plus I want to use only a single form and a single input field. you can see my chatbot here.

enter image description here

HTML Code :

<div class="form-group">
  <form action="process.php" id="form" name="f2" method="POST" >
    <input type="textarea" id="tt" name="input" placeholder="Type Your Message" style="position:absolute; bottom:0; height:30px; width:100%; height:50px;" required />

</div>

// this is the code which takes the input for the PIS_CODE
$msgg=$_POST['input'];

// this is the code which takes the input for the password
$pass=$_POST['input'];

// the problem is it saves the same input(that is PIS_CODE) in both the variables($msgg and $pass)

FULL Code:

<?php
require_once("config.php");
$msgg=$_POST['input'];
$msg=strtolower($msgg);
$ID = $msg;
$length=strlen($msg);
$flag=0;

$pass=$_POST['input'];
$update = "UPDATE lala SET password='$pass' WHERE PIS_CODE=".$msg;
$res_4=mysqli_query($con,$update);

$sql1 = "SELECT * FROM lala WHERE PIS_CODE='$msg'";
$res_u = mysqli_query($con, $sql1);

?>
<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<style>
.in
{
   background-color:rgb(64,128,255);
   color:white;
   padding:10px; 
   right:0;
   width:130px;
   text-align: center;
   height:auto;
   border-radius: 5px;
   margin-left: 120px;
   margin-bottom: 5px; 
}
.out
{
    background-color:rgb(241,240,240);
    color:black;
    padding:10px; 
    left:5; 
    width:130px;
    text-align: center;
    height:auto;
    border-radius: 15px;
}
</style>
<body>
<div class="in">
<?php echo "$msgg"; ?>
</div><br>
<div class="out">
<?php

if (($_POST['input']) =='password reset') 
{
echo "Do you want to reset your password? "; 
}
else if (($_POST['input']) =='yes') 
{
echo "Sure, Please provide PIS code ";   
}
if (mysqli_num_rows($res_u) == 1) 
{
echo 'Pis verified';
echo "Enter new password";
}

if($update){//if the update worked

      echo "Update successful!";



    } 
 ?>
</div><br>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duanba7653 2019-07-08 06:21
    关注

    You could use PHP session. This would do what you want:

    require_once("config.php");
    $msgg=$_POST['input'];
    $msg=strtolower($msgg);
    $ID = $msg;
    $length=strlen($msg);
    $flag=0;
    
    session_start(); 
    
    if(isset($_SESSION['PIS_CODE'])){
        $pass=$_POST['input'];
        $update = "UPDATE lala SET password='$pass' WHERE PIS_CODE=".$_SESSION['PIS_CODE'];
        $res_4=mysqli_query($con,$update);
        unset($_SESSION['PIS_CODE']);
    }
    else{
        $sql1 = "SELECT * FROM lala WHERE PIS_CODE='$msg'";
        $res_u = mysqli_query($con, $sql1);
        if (mysqli_num_rows($res_u) == 1) {
            $_SESSION['PIS_CODE'] = $msg;
        }
    }
    

    If i were you (i.e. working on a chatbot), i'd use the $_SESSION object for more than that. You could keep your simple 1 input form, and use PHP session to keep information about the next expected answer, or for a serie of questions like in your example, to know what is the current question, an many other possible usage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类