I am trying to update a Mysql row based on value passed to url of a page.
But i am getting an error Notice: Undefined index: id_store in C:\xampp\htdocs\store\php\update.php on line 29 when i submit the button in html form.
Here is my code:
<?php
require 'db.php';
if(isset($_GET['id_store'])){
$id_store=$_GET['id_store'];
$sql ="SELECT store_name,heading FROM store ORDER BY id_store='$id_store'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$store_name = $row['store_name'];
$heading = $row['heading'];
}
if(isset($_POST['btn-update']))
{
// variables for input data
$store_name_ = $_POST['store_name'];
$heading_ = $_POST['heading'];
// variables for input data
$id=$_GET['id_store'];
// sql query for update data into database
$sql_query = "UPDATE store SET store_name='$store_name_',heading='$heading_' WHERE id_store='$id'";
$conn->query($sql_query);
// sql query for update data into database
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
</head>
<body>
<center>
<div >
<form method="post" action="update.php">
<table align="center">
<tr>
<td><input type="text" name="store_name" placeholder="Store Name" value="<?php echo $store_name; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="heading" placeholder="Store Heading" value="<?php echo $heading; ?>" required /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
</td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
I am getting an error at line $id=$_GET['id_store'];
I think when I submit then button the form is directed to update.php without id_store due to which SQL query gets null value. Is there any thing that i need to change?
$conn->query()sanitize for you? Your using an untrusted $_GET variable in your SQL query, which could easily lead to an SQL injection..