【问题标题】:PHP form adds previous value in DB every time when I refresh page每次刷新页面时,PHP表单都会在数据库中添加先前的值
【发布时间】:2021-10-04 20:39:00
【问题描述】:

我创建了一个表单,它有两个输入字段,一个用于输入,第二个用于提交虽然在我刷新页面之前一切正常,假设我输入 John 作为输入并单击提交,它将添加到数据库中,但是当我刷新时页面它将再次在数据库中添加 john,如果我刷新页面 100 次,这将在一个循环中发生,它将在 DB 中添加 john 100 次我不知道为什么我的 if 条件不起作用if(isset($_post['save']),我的代码的行为就像那里根本没有如果条件它正在执行而不检查条件请提前帮助我谢谢。这是我的代码:

`<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Document</title>
  <link rel="stylesheet" type="text/css" href="style/stylesheet.css">
  <script src="jquery.js"></script>
</head>
<body>
  <h1>Secret message game</h1>
  <form id ="form-data" action="" method="post">
      <input type="text" name="user" id="user" autofocus placeholder="Enter your name" pattern="[a-z A-Z]{3,10}" title="a-z A-Z" required>
      <input type="submit" id="save" name="save">
  </form>
</body>
</html>

<?php
  if(isset($_POST['save'])){
    $value = $_POST['user'];

    $con = mysqli_connect('localhost','root','','website') or die("Connection failed");
    $query = "INSERT into user(name)
              values('{$value}')" or die(mysqli_error($con));

    $r = mysqli_query($con,$query) or die('Query failed!');

  }
?>
`

【问题讨论】:

  • 您应该将其发布到您首先处理数据库的第二个页面,然后将页面重定向到您喜欢的页面。还记得关闭 mysql 连接以避免数据库服务器上的错误。你选对了 mysqli 女巫的使用速度比 PDO 快!坚持做测试,你自己就得到了正确的答案
  • 我做到了,但问题仍然存在
  • 你能分享你编辑的代码吗? @Constantin 建议的解决方案应该可以正常工作。
  • 你的意思是我必须从这个文件中剪切 PHP 代码并将其添加到新文件中,如果你是认真的,我已经这样做了,然后我可以与你分享
  • 这实际上是按预期工作的。您将浏览器重定向到同一页面,但使用post 请求而不是通常的get。之后刷新或更新页面时,浏览器会再次运行之前的请求,恰好是post

标签: php sql database forms


【解决方案1】:

这只是演示目的:不是完成项目的东西:您还应该清理所有帖子以避免来自松散者 = 黑客的 sql 注入 .. 也适用于您可以使用的唯一用户

$query = "INSERT INTO `users` (`user`, `pass`, `name`, `lastname`)
VALUES ('".strtolower($_POST['user'])."', '".$_POST['pass']."', '".$_POST['name']."', '".$_POST['lastname']."');";







<?php
/*
important php stuff here(headers,cookies)
without spaces and make sure your
editor save it in utf8 format!!!






this script work with table

CREATE TABLE `users` (
  `id` int(15) unsigned zerofill NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `user` varchar(256) NOT NULL,
  `pass` varchar(256) NOT NULL,
  `name` varchar(256) NOT NULL,
  `lastname` varchar(256) NOT NULL
) ENGINE='InnoDB';



### !!! first create a database in phpmyadmin or adminer or custom script


you should use phpmyadmin or adminer as assistent if you are beginner(whatever who read this)
if you want a quick start .. use youtube search to understand how to use them
then copy/paste and modify queries like your needs

*/

function error($no){
    header('Location: index.php?error='.$no); exit();
}



  $con = mysqli_connect('localhost','root','THEPASSWORD',"kindofdemo") or die("Connection failed");
  


if(isset($_POST['save'])){
    $value = $_POST['user'];

   


    (isset($_POST['pass'])and isset($_POST['pass2'])and($_POST['pass']===$_POST['pass2']))OR error(2);
    $howmanyusers=mysqli_query($con,"SELECT count(*) FROM `users` WHERE `user` LIKE '%".$_POST['user']."%'");
    $howmanyusers=$howmanyusers->fetch_array(MYSQLI_NUM);
    //print_r($howmanyusers[0]);
    if($howmanyusers[0]>0){ error(1); }



    
    /*
        INSERT INTO `users` (`user`, `pass`, `name`, `lastname`) VALUES ('user', 'pass', 'name', 'lastname');
    */
    $query = "INSERT INTO `users` (`user`, `pass`, `name`, `lastname`)
VALUES ('".$_POST['user']."', '".$_POST['pass']."', '".$_POST['name']."', '".$_POST['lastname']."');";
    echo('<b>executed:'.$query.'</b>');
    $r = mysqli_query($con,$query) or die('Query failed!');
    
  }

//phpinfo();     /*use this to see whatever you post , or many others usefull vars*/







if(isset($_REQUEST['error']))
    switch($_REQUEST['error']){

    case 1: echo '<b>The username already exist !</b>';
        break;
    case 2: echo '<b>The password confirmation is not equal with the choosen password</b>';
        break;
}





/*
    Let see what is in database
*/


    if($actualusers=mysqli_query($con,"SELECT * FROM `users` WHERE 1;") ){

        while($actualusers  and($row = $actualusers->fetch_assoc()) ){ 
        echo '<pre>';
        print_r($row);
        echo '</pre><hr>';
        }
    }








/*!!!!!!!!!!!!!!!!!!!*/
mysqli_close($con);



?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="Custa">
<!-- <meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content=""> -->
<title>Document</title>

<style>
    
    b{
        color:red; 
    }
</style>

</head>
<body>
<h1>Secret message game</h1>
  <form id ="form-data" action="index.php" method="post"> <!-- index.php without ? query will reset the custom error system :switch-->
      <input type="text" name="user" id="user" autofocus placeholder="Enter your user name"><!-- this should not be constrain to that filter -->
      <input type="text" name="name" placeholder="Enter your name" pattern="[a-z A-Z]{3,10}" title="a-z A-Z" required>
      <input type="text" name="lastname" placeholder="Enter your lastname"  pattern="[a-z A-Z]{3,10}" title="a-z A-Z" required>
      <input type="password" name="pass" placeholder="Choose a password">
      <input type="password" name="pass2" placeholder="Confirm the password to ensure it's the correct one">  
      <input type="submit" id="save" name="save">
  </form>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    相关资源
    最近更新 更多