繁体   English   中英

PHP两个不同的按钮发送相同的数据

[英]PHP Two different buttons send same data

我有两个不同的按钮。 一个用于删除用户,另一个用于更改电子邮件地址。 问题是单击更改电子邮件按钮实际上会将用户从数据库中删除。

header.php文件

<?php
session_start();


$cookie_name = "LoginSystem";
$cookie_value = "Valid";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>



<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">


  <meta charset="UTF-8">
  <meta name="description" content="Enrol site for activites">
  <meta name="keywords" content="enrol, activities, school, hobby, college, login, register">
  <meta name="author" content="Gyorgy Hadhazy">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">


</head>
<body>



<header> 
    <nav>
        <div class="main-wrapper">
            <ul>
                <li><a href="index.php">HOME</a></li>
                <li><a href="about.php">ABOUT</a></li>
                <li><a href="media.php">MEDIA</a></li>
                <li><a href="activities.php">ACTIVITIES</a></li>
                <li><a href="contact.php">CONTACT</a></li>
            </ul>
            <div class="nav-login">
                <?php 
                    if (isset($_SESSION['u_id'])) {
                        echo '
                        <form action="includes/logout.inc.php" method="POST">
                            <button type="submit" name="submit">Logout</button>
                        </form>
                        ';
                       echo '<form action="deleteusr.php" method="POST">
                            <button type="submit" name="delete">Delete User</button>
                            <input type="hidden" name="user_uid" value="'. $_SESSION['u_id'].'"
                            </form>';


                    } else{
                        echo '
                        <form action="includes/login.inc.php" method="POST">
                            <input type="text" name="uid" placeholder="StudentID/email">
                            <input type="password" name="pwd" placeholder="password">
                            <button type="submit" name="submit">LOGIN</button>
                        </form>
                        <a href="signup.php">SIGN UP</a>
                        ';
                    }



                ?>

                <button type="button" onclick="resizeText(1)" name="resizeplus" class="resize-plus">+ Text size</button>
                <button type="button" onclick="resizeText(-1)" name="resizenegative">- Text size</button>  


<script>

function resizeText(multiplier) {
  if (document.body.style.fontSize == "") {
    document.body.style.fontSize = "1.0em";
  }
document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
}   
</script>



            </div>
        </div>
    </nav>
</header>

的index.php

    <?php
        include 'header.php';
    ?>


    <style>
        header{
        text-align: center; 
        }
        body{
            text-align: center;
        }
    </style>

    <section class="main-container">
        <div class="main-wrapper">
            <h2>HOME</h2>
        <p>Please log in if extra features are not displayed</p>
            <?php


            if (isset($_SESSION['u_email'])) {

                            echo '<form action="changeEmail.php" method="POST">
                                <button type="submit" name="email">Change Email</button> 
                                <input type="text" name="email" value="'. $_SESSION['u_email'].'"
                                </form>'; }


            ?>  
        </div>
    </section>





    <?php
    include 'footer.php';
?>

最后是应该调用的php文件: changeEmail.php

<?php
    include 'header.php';
?>

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "loginsystem";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



$email = $_SESSION['u_ email'];

$sql = "UPDATE users SET user_email='$email'";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?> 

我认为问题出在header.php中,但我不确定。 如果有人可以指出这个问题,我将非常感激。

由index.php呈现的HTML代码

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css">


  <meta charset="UTF-8">
  <meta name="description" content="Enrol site for activites">
  <meta name="keywords" content="enrol, activities, school, hobby, college, login, register">
  <meta name="author" content="Gyorgy Hadhazy">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">


</head>
<body>



<header> 
    <nav>
        <div class="main-wrapper">
            <ul>
                <li><a href="index.php">HOME</a></li>
                <li><a href="about.php">ABOUT</a></li>
                <li><a href="media.php">MEDIA</a></li>
                <li><a href="activities.php">ACTIVITIES</a></li>
                <li><a href="contact.php">CONTACT</a></li>
            </ul>
            <div class="nav-login">

                        <form action="includes/logout.inc.php" method="POST">
                            <button type="submit" name="submit">Logout</button>
                        </form>
                        <form action="deleteusr.php" method="POST">
                            <button type="submit" name="delete">Delete User</button>
                            <input type="hidden" name="user_uid" value="6"
                            </form>                
                <button type="button" onclick="resizeText(1)" name="resizeplus" class="resize-plus">+ Text size</button>
                <button type="button" onclick="resizeText(-1)" name="resizenegative">- Text size</button>  


<script>

function resizeText(multiplier) {
  if (document.body.style.fontSize == "") {
    document.body.style.fontSize = "1.0em";
  }
document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
}   
</script>



            </div>
        </div>
    </nav>
</header>

<style>
    header{
    text-align: center; 
    }
    body{
        text-align: center;
    }
</style>

<section class="main-container">
    <div class="main-wrapper">
        <h2>HOME</h2>
    <p>Please log in if extra features are not displayed</p>
        <form action="changeEmail.php" method="POST">
                            <button type="submit" name="email">Change Email</button> 
                            <input type="text" name="email" value="test11@gmail.com"
                            </form>  
    </div>
</section>






Cookie 'LoginSystem' is set!<br>Value: Valid

实际外观图片: 在此处输入图片说明

主要问题:

有两个<input>标记缺少结尾>字符。 这意味着浏览器正在构建不正确的DOM树。 它正在尽最大努力确定您要提交的表单,但它选择了错误的表单(删除表单)。

第一个示例在header.php中:

<input type="hidden" name="user_uid" value="'. $_SESSION['u_id'].'"

请注意,没有>关闭该输入标签。

然后在index.php中:

<input type="text" name="email" value="'. $_SESSION['u_email'].'"

向这两个都添加结束>字符,当您单击按钮时,浏览器将很高兴地解析DOM并选择正确的表单进行提交。

其他事宜:

changeEmail.php中有几个问题:

$email = $_SESSION['u_ email'];

需要是

$email = $_SESSION['u_email'];

否则, $email将始终为空字符串(或您不希望使用的其他值-我不确定$_SESSIONS的行为),然后将所有电子邮件设置为空字符串。

第二个问题是您的SQL:

$sql = "UPDATE users SET user_email='$email'";

您需要使用where子句指定要设置的用户电子邮件。 否则,将每个电子邮件设置为$email的值。

在这种情况下,您需要从发布的表单数据中获取新的电子邮件地址。

$new_email = $_POST["email"];
$sql = "UPDATE users SET user_email='$new_email' WHERE user_email='$email'";

为了确保您将获得新的email表单数据,请从button元素中删除name属性-不必要。

暂无
暂无

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

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