使用 PHP 根据传递给 URL 的值更新 MySQL 行
update MySQL row based on values passed on URL using PHP
我正在尝试根据传递给页面 url 的值更新 Mysql 行。
但是当我以 html 形式提交按钮时出现错误 Notice: Undefined index: id_store in C:\xampp\htdocs\store\php\update.php on line 29
。
这是我的代码:
<?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>
我在第 $id=$_GET['id_store'];
行遇到错误
我想当我提交然后按钮时,表单被定向到 update.php 而没有 id_store
因为 SQL 查询得到空值。有什么我需要改变的吗?
注:
- 确保您的 URL 中有一个
id_store
值。
- 您的第一个查询是错误的。您正在像
WHERE
. 一样使用 ORDER BY
- 您尝试更新并提交页面,但没有通过
id_store
。您的表单将转到 update.php。删除 ACTION
. 中的 URL 属性
修改select查询代码:
$sql ="SELECT store_name,heading FROM store WHERE id_store='$id_store' ORDER BY id_store";
您的表格应该是:
<form method="post" action="">
这样当您从 localhost/store/php/update.php?id_store=36
按下提交按钮时,它仍会转到 localhost/store/php/update.php?id_store=36
,而不仅仅是 localhost/store/php/update.php
。
提交后,未定义错误将消失,因为您在URL中保留了id_store
。
在你的 isset()
里面,这样用户就无法刷新页面并重新提交表单,只需将其放在右括号之前:
header("LOCATION:update.php?id_store=".$_GET["id_store"]);
请检查这些错误:-
您所有的编码内容和表单代码都在一个文件中,那么您为什么要采取行动。从您的表单中删除 action
属性。
表单方法是 POST
但你正在使用 $_GET
将它更改为 $_POST
每个你使用它的地方。
也像这样更改查询:-
$sql ="SELECT store_name,heading FROM store WHERE id_store='$id_store' ORDER BY id_store";
注意:- 检查并执行所有这些操作并告知发生了什么
我正在尝试根据传递给页面 url 的值更新 Mysql 行。
但是当我以 html 形式提交按钮时出现错误 Notice: Undefined index: id_store in C:\xampp\htdocs\store\php\update.php on line 29
。
这是我的代码:
<?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>
我在第 $id=$_GET['id_store'];
我想当我提交然后按钮时,表单被定向到 update.php 而没有 id_store
因为 SQL 查询得到空值。有什么我需要改变的吗?
注:
- 确保您的 URL 中有一个
id_store
值。 - 您的第一个查询是错误的。您正在像
WHERE
. 一样使用 - 您尝试更新并提交页面,但没有通过
id_store
。您的表单将转到 update.php。删除ACTION
. 中的 URL 属性
ORDER BY
修改select查询代码:
$sql ="SELECT store_name,heading FROM store WHERE id_store='$id_store' ORDER BY id_store";
您的表格应该是:
<form method="post" action="">
这样当您从 localhost/store/php/update.php?id_store=36
按下提交按钮时,它仍会转到 localhost/store/php/update.php?id_store=36
,而不仅仅是 localhost/store/php/update.php
。
提交后,未定义错误将消失,因为您在URL中保留了id_store
。
在你的 isset()
里面,这样用户就无法刷新页面并重新提交表单,只需将其放在右括号之前:
header("LOCATION:update.php?id_store=".$_GET["id_store"]);
请检查这些错误:-
您所有的编码内容和表单代码都在一个文件中,那么您为什么要采取行动。从您的表单中删除
action
属性。表单方法是
POST
但你正在使用$_GET
将它更改为$_POST
每个你使用它的地方。也像这样更改查询:-
$sql ="SELECT store_name,heading FROM store WHERE id_store='$id_store' ORDER BY id_store";
注意:- 检查并执行所有这些操作并告知发生了什么