简体   繁体   English

如何通过 PHP 更新 MySQL 表上的数据

[英]How can I update data on MySQL table via PHP

I can't seem to trigger if(isset($_POST['schedule'])) condition via form input button click.我似乎无法通过单击表单输入按钮触发if(isset($_POST['schedule']))条件。 How can I update the $schedule_one variable using the following code?如何使用以下代码更新$schedule_one变量?

    if(isset($_POST['login'])){
    $password=$_POST['password'];
    $email=$_POST['email'];
    $ret= mysqli_query($con,"SELECT * FROM maid_marketplace_users WHERE email='$email' and password='$password'");
    $num=mysqli_fetch_array($ret);
    $nomee = $num['full_name'];
    $schedOne = $num['schedule_one'];
    echo "<p align=center>You are entitled to <b>%10 off</b> for the next <b>12 months.</b></p> ";
?>       
     <div class="uk-child-width-expand@s uk-text-center" uk-grid>
           <center> <div class="uk-card uk-card-default uk-card-body">Welcome <b>
            <?php
            echo "<br>";
            if (is_null($schedule_one)){
                echo "You don't have any maid services scheduled";
                if(isset($_POST['schedule']))
                {
                    $id = $_SESSION['id'];
                    $schedule_one = $_POST['schedule_one'];
                    $sql=mysqli_query($con,"UPDATE maid_marketplace_users Set schedule_one='$schedule_one' WHERE id='$id'");
                    echo "<script>alert('$id Succes! your next schedule is on $schedule_one');</script>";

                    
                }
            }
            else
            {
                $schedule_one =  $_POST['schedule_one'];
                echo "Your next schedule is at $schedule_one";
            }
            ?>
            <!--Click here to <a href="logout.php" tite="Logout">Logout. -->
                <form name="schedule" action="" method="post">
                    <div class="uk-width-1-1@s">
                        <div class="uk-inline">
                            <input class="uk-input" name="schedule_one" type="datetime-local">
                        </div>
                    </div>
                    <div class="uk-width-1-1@s">
                        <input type="submit" name="schedule" value="Schedule" >
                    </div>
                </form>                
            </div></center>
     </div>
    <?php
        die();
        }

NOTE: I know you aren't supposed to add password directly into Mysql database, I will encrypt it once it's done.注意:我知道您不应该将密码直接添加到 Mysql 数据库中,一旦完成,我将对其进行加密。

You can't have the if(isset($_POST['schedule'])) inside of another if(isset($_POST['login'])) conditional statement.您不能将if(isset($_POST['schedule']))放在另一个if(isset($_POST['login']))条件语句中。 You have to move it out of the first condition like this;您必须像这样将其从第一个条件中移出;

if(isset($_POST['login']))
{
 ....
}

and you add the other conditional if statement然后添加另一个条件 if 语句

if(isset($_POST['schedule']))

{
 ....
}

One has to finish before you start the other if statement.一个必须在开始另一个 if 语句之前完成。

It's good that you know you're creating security vulnerabilities.很高兴您知道自己正在制造安全漏洞。

One thing you would need to do is to not make your SQL update logically unreachable.您需要做的一件事是不要让您的 SQL 更新在逻辑上无法访问。

In this:在这:

                echo "You don't have any maid services scheduled";
                die();
                if(isset($_POST['schedule']))
                {
                    $id = $_SESSION['id'];
                    $schedule_one = $_POST['schedule_one'];
                    $sql=mysqli_query($con,"UPDATE maid_marketplace_users Set schedule_one='$schedule_one' WHERE id='$id'");
                    echo "<script>alert('$id Succes! your next schedule is on $schedule_one');</script>";

                    
                }

the die() will always interrupt your code before it reaches the SQL update. die()将始终在您的代码到达 SQL 更新之前中断您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM