使用 php 中的输入字段将 table 行数据提交给另一个 table
Submitting table row data to another table using an input field in php
如何使用 PHP 和 MYSQL 中的输入字段将 table 行数据提交给另一个 table?
HTML
<form method="post" action="post.php">
<input type="number" name="code" placeholder="Code..."/>
<input type="submit" name="submit" value="Submit"/>
</form>
Post.php
if (isset($_POST['submit'])) {
$code = $_POST['code'];
$getCode = "SELECT * FROM products WHERE code=$code";
mysqli_query($connection, $getCode);
if ($_POST['code'] == $code) {
$migrating = "INSERT INTO managment(price) VALUES ($price) SELECT
price FROM products";
mysqli_query($connection, $migrating);
header("location: index.php");
}
}
我的代码有什么问题?
syntax 是:
INSERT INTO managment(price) SELECT price FROM products
也许你想添加一个 WHERE
子句 :
INSERT INTO managment(price) SELECT price FROM products WHERE code=$code
注意:在您的代码中,$_POST['code'] == $code
没有意义,因为之前 $code = $_POST['code']
。
我还建议您查看 How can I prevent SQL injection in PHP? 以保护您的查询。
您的代码已更新(未经测试):
if (isset($_POST['submit'])) {
$code = $_POST['code'];
$getCode = "SELECT * FROM products WHERE code=$code";
$result = mysqli_query($connection, $getCode);
if (!$result) die("Error: " . mysqli_error($connection));
$row = mysqli_fetch_assoc($result);
if ($row['code'] == $code) {
$migrating = "INSERT INTO managment(price)
SELECT price FROM products WHERE code=$code";
$result2 = mysqli_query($connection, $migrating);
if (!$result2) die("Error: " . mysqli_error($connection));
header("Location: index.php");
exit;
}
}
如何使用 PHP 和 MYSQL 中的输入字段将 table 行数据提交给另一个 table?
HTML
<form method="post" action="post.php">
<input type="number" name="code" placeholder="Code..."/>
<input type="submit" name="submit" value="Submit"/>
</form>
Post.php
if (isset($_POST['submit'])) {
$code = $_POST['code'];
$getCode = "SELECT * FROM products WHERE code=$code";
mysqli_query($connection, $getCode);
if ($_POST['code'] == $code) {
$migrating = "INSERT INTO managment(price) VALUES ($price) SELECT
price FROM products";
mysqli_query($connection, $migrating);
header("location: index.php");
}
}
我的代码有什么问题?
syntax 是:
INSERT INTO managment(price) SELECT price FROM products
也许你想添加一个 WHERE
子句 :
INSERT INTO managment(price) SELECT price FROM products WHERE code=$code
注意:在您的代码中,$_POST['code'] == $code
没有意义,因为之前 $code = $_POST['code']
。
我还建议您查看 How can I prevent SQL injection in PHP? 以保护您的查询。
您的代码已更新(未经测试):
if (isset($_POST['submit'])) {
$code = $_POST['code'];
$getCode = "SELECT * FROM products WHERE code=$code";
$result = mysqli_query($connection, $getCode);
if (!$result) die("Error: " . mysqli_error($connection));
$row = mysqli_fetch_assoc($result);
if ($row['code'] == $code) {
$migrating = "INSERT INTO managment(price)
SELECT price FROM products WHERE code=$code";
$result2 = mysqli_query($connection, $migrating);
if (!$result2) die("Error: " . mysqli_error($connection));
header("Location: index.php");
exit;
}
}