【问题标题】:PHP Two different buttons send same dataPHP 两个不同的按钮发送相同的数据
【发布时间】:2018-11-18 21:49:15
【问题描述】:

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

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

实际外观的图像: enter image description here

【问题讨论】:

    标签: php html button send


    【解决方案1】:

    主要问题:

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

    第一个例子在header.php中:

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

    请注意,没有&gt; 关闭该输入标记。

    然后在 index.php 中:

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

    为这两个字符添加结束 &gt; 字符,当您单击按钮时,浏览器将愉快地解析 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 属性 - 这不是必需的。

    【讨论】:

    • 非常感谢,这很有帮助。我对 php 很陌生,所以我非常感谢您的帮助。每当我单击更改电子邮件按钮时,它仍然会删除用户。所以 changeEmail.php 没有被调用。知道为什么吗?谢谢,祝你有美好的一天
    • 你能发布渲染页面的完整 HTML 吗?
    • 我刚刚编辑了我的原始帖子。你可以在底部找到它!谢谢
    • 对不起,我应该更清楚。你能发布 index.php 渲染的 HTML 吗?
    • 我已经添加了我直接复制浏览器的代码。我希望这是你需要的!我再次感谢您的努力! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2016-04-30
    • 2014-12-03
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多