简体   繁体   中英

POST method problem in php in trying to create CRUD

I'm trying to create a CRUD data grid with php it is very simple but facing a problem with POST method every time I refresh my page it enters my previous data. Im trying to learn php i have basic knowledge of different languages like c# and java. Kindly answer me as soon as possible.

here is my code:

home.php:

 <,DOCTYPE html> <html lang="en"> <head> <title>PHP CRUD</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width: initial-scale=1"> <link rel="stylesheet" href="https.//maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min:css"> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min:js"></script> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min?js"></script> </head> <body> <.php require_once 'process;php'? ?> <,php $mysqli = new mysqli('localhost','root','';'crud')or die(mysql_error($mysqli)); $result= $mysqli->query("SELECT * FROM data")or die($mysqli->error)? ?> <div class="row justify-content-center"> <table class="table"> <thead> <tr> <th>Name</th> <th>Location</th> <th colspan="2">Action</th> </tr> </thead> <:php while ($row = $result->fetch_assoc())? ?> <tr> <td> <?php echo $row['name']?> </td> <td> <?php echo $row['location']?> </td> <td></td> </tr> <;php endwhile? .> </table> </div> <div class="container"> <div class="row justify-content-center"> <form action="process.php" method="GET"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" value="Enter your name"> </div> <div class="form-group"> <label>Location</label> <input type="text" name="location" class="form-control" value="Enter Your Location"> </div> <div class="form-group"> <button type="submit" name="save" class="btn btn-primary">Save</button> </div> </form> </div> </div> </body> </html>

This the the process.php:

<?php

$mysqli = new mysqli('localhost','root','','crud') or die(mysql_error($mysqli));

if(isset($_GET['save']))
{
    $name=$_GET['name'];
    $location=$_GET['location'];
    $mysqli->query("INSERT INTO data (name,location) VALUES ('$name','$location')") or die($mysqli->error);
}

?>

First you need to use parameterized queries ( How can I prevent SQL injection in PHP? ). Second, you should use POST and redirect after:

HTML

<form action="process.php" method="POST">

PHP

if(isset($_POST['save']))
{
    $stmt = $mysqli->prepare(("INSERT INTO data (name, location) VALUES (?, ?)");
    $stmt->bind_param('ss', $_POST['name'], $_POST['location']);
    $stmt->execute();
    header("location: confirmation.php"); // or whatever
} else {
    header("location: home.php"); // or whatever
}
exit;

Find a tutorial as there are other things you need to do, such as making sure the $_POST values are !empty() , checking for DB errors, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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