gpt4 book ai didi

php - 在一张表中插入两个下拉列表值

转载 作者:行者123 更新时间:2023-11-29 19:00:52 25 4
gpt4 key购买 nike

我有两个下拉列表,其中的值是从两个不同的表中选择的。如何确保单击提交按钮时,下拉列表 orden 的 id 转到 norm

<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$normSql = 'SELECT * FROM norm WHERE orden_id IS NULL';
$normResult = $conn->query($normSql);

$ordenSql = 'SELECT * FROM orden';
$ordenResult = $conn->query($ordenSql);

function html($string) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}

?>
<!DOCTYPE html>
<html>
<body>
<form method="POST" action"">
<select name="normID">
<option selected disabled>Choose norm</option>
<?php while ($result = $normResult->fetch_assoc()): ?>
<option value="<?= html($result['id']); ?>"><?= html($result['norm_name']); ?></option>
<?php endwhile; ?>
</select>

<select name="ordenID">
<option selected disabled>Choose orden</option>
<?php while ($result = $ordenResult->fetch_assoc()): ?>
<option value="<?= html($result['id']); ?>"><?= html($result[ 'orden_name']); ?></option>
<?php endwhile; ?>
</select>

<input type="submit" value="Insert">
</form>
</body>
</html>

<?php

if(isset($_POST["submit"]))
{

$ordening=$_POST["ordenID"];

$query = mysqli_query("INSERT INTO norm (orden_id)VALUES ('$ordening')");
if($query)
{
echo "Thank You! you are now registered.";
}
}
?>

最佳答案

您的查询设置应如下所示,使用绑定(bind)参数以确保安全并执行查询:

仅选择ID,而不是所有内容,您不需要所有内容:

<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$normSql = 'SELECT id FROM norm WHERE orden_id IS NULL';
$normResult = $conn->query($normSql);

$ordenSql = 'SELECT id FROM orden';
$ordenResult = $conn->query($ordenSql);

function html($string) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}

?>
<!DOCTYPE html>
<html>
<body>
<form method="POST" action"">

/* Not entirely sure why you need two selects? There's no reason you're not even using the value from this select? */

/* <select name="normID">
<option selected disabled>Choose norm</option>
<?php while ($result = $normResult->fetch_assoc()): ?>
<option value="<?= html($result['id']); ?>"><?= html($result['norm_name']); ?></option>
<?php endwhile; ?>
</select>*/

<select name="ordenID">
<option selected disabled>Choose orden</option>
<?php while ($result = $ordenResult->fetch_assoc()): ?>
<option value="<?= html($result['id']); ?>"><?= html($result[ 'orden_name']); ?></option>
<?php endwhile; ?>
</select>

<input type="submit" value="Insert">
</form>
</body>
</html>




Then, change your insert to be more secure:



if(isset($_POST["submit"]))
{

$ordening= $_POST["ordenID"]; // get the order ID from your submitted from <select>

$query = mysqli_query("INSERT INTO norm (orden_id) VALUES (?)");
$stmt = $conn->prepare($query);
if($stmt){
$stmt->bind_param("s",$ordening"); // Bind the id
$stmt->execute(); // Execute query
$affected = $stmt->affected_rows; // Get the affected rows (inserted)
$stmt->close(); // Close the statement
}
if($affected > 0 {
echo "Successful Entry";
}

}

关于php - 在一张表中插入两个下拉列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43894653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com