简体   繁体   English

提交按钮在 PHP 中不起作用

[英]Submit button won't work in PHP

PHP Code

<?php
if (!isset($_SESSION)) { session_start(); }




include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
    header('location: no_acces.php');
    exit();
} else {
    $id_user = $_SESSION['userid'];
    $q_user = mysqli_query($conn, "SELECT * FROM users WHERE id = $id_user");
    if (mysqli_num_rows($q_user) === 1) {
        $r_user = mysqli_fetch_assoc($q_user);
    } else {
        unset($_SESSION['login']);
        unset($_SESSION['userid']);
        header('location: no_acces.php');
        exit();
    }
}

$error = "";

$userQuery = mysqli_query($conn, "SELECT username FROM users");

$user = mysqli_fetch_assoc($userQuery);
$id = $_GET['id'];
if (isset($_POST['edit_contact'])) {
    $roepnaam = $_POST['roepnaam'];
    $naam = $_POST['naam'];
    $land = $_POST['land'];
    $bedrijf = $_POST['bedrijf'];
    $adres1 = $_POST['adres1'];
    $adres2 = $_POST['adres2'];
    $stad = $_POST['stad'];
    $postcode = $_POST['postcode'];
    $provincie = $_POST['provincie'];
    $telefoon = $_POST['telefoon'];
    $email = $_POST['email'];
        $captcha=   $_POST['g-recaptcha-response'];

    if(!$captcha){
            $error = "Er is een fout opgetreden";

          }


          if ($error == "") {
                $insertUser = ("UPDATE address SET
                roepnaam = '$roepnaam', naam = '$naam', bedrijf = '$bedrijf', telefoon = '$telefoon', email = '$email', adres1 = '$adres1', adres2 = '$adres2', stad = '$stad', postcode = '$postcode', provincie = '$provincie', land = '$land' WHERE id = $id");


                if (mysqli_query($conn, $insertUser)) {
                    $_SESSION['edit_contact'] = true;
                    header('location: address_book.php');
                } else {
                    $error = "Er is een fout opgetreden";
                }
            }
          }

 ?>

HTML Code

 <!DOCTYPE html>
 <html lang="en">

   <body>

       <form action="" method="post">
        <?php if ($error !== "") { ?>
        <div class="row">
           <div class="col-md-12 error">
             <?php echo $error; ?>
           </div>
         </div>
        <?php } ?>
         <label for="firstName" class="control-label">Naam:</label>
         <div class="row ">
           <div class="col-md-6">
           <input type="text" class="form-control" id="firstName" placeholder="Roepnaam" name="roepnaam" value="<?php if (isset($_POST['roepnaam'])) { echo $_POST['roepnaam']; } ?>" required/>
         </div>
         <div class="col-md-6">
         <input type="text" class="form-control" id="lastName" placeholder="Naam" name="naam" value="<?php if (isset($_POST['naam'])) { echo $_POST['naam']; } ?>" required/>
       </div>
         </div>
 <label for="username" class="control-label">Bedrijf:</label>
         <div class="row ">
           <div class="col-md-12">
             <input type="text" class="form-control" id="username" placeholder="Bedrijf" name="bedrijf" value="<?php if (isset($_POST['bedrijf'])) { echo $_POST['bedrijf']; } ?>" required/>
           </div>
         </div>
         <label for="password" class="control-label">Telefoonnummer:</label>
                 <div class="row ">
                   <div class="col-md-12">
                     <input type="text" class="form-control" id="password" placeholder="Telefoonnummer" name="telefoon" value="<?php if (isset($_POST['telefoon'])) { echo $_POST['telefoon']; } ?>" required/>
                   </div>

                 </div>
                 <label for="email" class="control-label">Email:</label>
                         <div class="row ">
                           <div class="col-md-12">
                             <input type="text" class="form-control" id="email" placeholder="E-mailadres" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" required/>
                           </div>

                         </div>
 <label for="adres1" class="control-label">Adres:</label>
         <div class="row">
           <div class="col-md-12">
           <input type="text" class="form-control" id="adres1" placeholder="Adres 1" name="adres1" value="<?php if (isset($_POST['adres1'])) { echo $_POST['adres1']; } ?>" required/>
         </div>
         </div>
         <div class="row padding-top-10">
           <div class="col-md-12">
           <input type="text" class="form-control" id="adres2" placeholder="Adres 2" name="adres2" value="<?php if (isset($_POST['adres2'])) { echo $_POST['adres2']; } ?>"/>
         </div>
         </div>
         <div class="row">
           <div class="col-md-3">
             <label for="postcode" class="control-label">Postcode:</label>
           </div>
           <div class="col-md-5">
             <label for="city" class="control-label">Stad:</label>
           </div>
           <div class="col-md-4">
             <label for="regio" class="control-label">Regio:</label>
           </div>
         </div>
         <div class="row ">
           <div class="col-md-3">
             <input type="text" class="form-control" id="postcode" placeholder="Postcode" name="postcode" value="<?php if (isset($_POST['postcode'])) { echo $_POST['postcode']; } ?>" required/>
           </div>
           <div class="col-md-5">
             <input type="text" class="form-control" id="city" placeholder="Stad" name="stad" value="<?php if (isset($_POST['stad'])) { echo $_POST['stad']; } ?>" required/>
           </div>
           <div class="col-md-4">
             <input type="text" class="form-control" id="regio" placeholder="Provincie" name="provincie" value="<?php if (isset($_POST['provincie'])) { echo $_POST['provincie']; } ?>" required/>
           </div>
         </div>
         <label for="land" class="control-label">Land:</label>
                 <div class="row ">
                   <div class="col-md-12">
                     <input type="text" class="form-control" id="password" placeholder="Land" name="land" value="<?php if (isset($_POST['land'])) { echo $_POST['land']; } ?>" required/>
                   </div>

                 </div>

                                <div class="row">
                                    <div class="col-md-8 padding-top-10 ">
                                        <div class="g-recaptcha " data-sitekey="6LcCsBoTAAAAAK72uzyJSrgWwD8xuF6jFIfgFaHX"></div>
                                    </div>
                                </div>
         <div class="row">
           <div class="col-md-2 padding-top-10">
             <input type="submit" name="edit_contact" class="btn btn-succes" value="Wijzigen">
           </div>
                     <div class="col-md-2 padding-top-10">
                         <input type="text" name="delete_contact" action="delete_contact.php" class="btn btn-succes" value="Contact verwijderen">
                     </div>
         </div>


       </form>
   </body>
   </html>

PHP Code

<?php
if (!isset($_SESSION)) { session_start(); }


include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true || !isset($_SESSION['userid']) || $_SESSION['userid'] == "") {
    header('location: login.php');
    exit();

} else {
    session_regenerate_id();
}
$id = $_GET['id'];

$query = "DELETE FROM address WHERE id= $id";

mysqli_query ($query);

if (mysql_affected_rows() == 1) {
header('location: addressbook.php');

} else {
echo "Verwijderen mislukt"; 

}


?>

I'm trying to make a delete button for my contacts within the addressbook.我正在尝试为地址簿中的联系人制作一个删除按钮。 but everytime I click "Contact verwijderen" the webpage resets it self and the contact won't be deleted.但是每次我点击“Contact verwijderen”时,网页都会自行重置,并且不会删除联系人。 Could anyone help me to fix this?谁能帮我解决这个问题?

You input is a text input and you don't have a form asociated with it,create one and change the type of submit to submit您输入的是文本输入,并且您没有与之关联的表单,请创建一个并更改提交类型以submit

<form action="delete_contact.php" method="post">
//other inputs 
<input type="submit" name="delete_contact" class="btn btn-succes" value="Contact verwijderen">
</form>

You are mixing MySQL and MySQLi functions:您正在混合MySQLMySQLi函数:

mysqli_query ($query);

if (mysql_affected_rows() == 1) 

You cannot mix MySQL with MySQLi , your code should be:您不能将MySQLMySQLi混合使用,您的代码应该是:

mysqli_query ($query);

if (mysqli_affected_rows($conn) == 1) 

Add a normal link to delete the contact, you don't need a form.添加普通链接删除联系人,不需要表单。

<a href="delete_contact.php?id=<?php echo $id ?>">
      Contact verwijderen
</a>

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

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