[英]Create a dropdown list from one table and entering selected value into another table (same MySQL DB)
[英]How to insert selected value from dropdown list into another MySQL table
我正在嘗試制作一個下拉列表,允許用戶 select 一個他們需要的部件,所以在選擇了他們需要的所有部件並將其提交到 go 到 MySQL 數據庫之后。 但是在選擇提交后沒有任何東西插入到我的數據庫中。
我的代碼和連接:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "userregistration";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo 'good connection';
?>
<form action="trying.php" method=" POST">
<table border=" 1">
<thead>
<tr>
<th>Component</th>
<th>Item Name</th>
<th>Price </th>
</tr>
</thead>
<tbody>
<tr>
<td>CPU</td>
<td>
<?php
//Retrieving CPU table
$query = $conn->query("SELECT * FROM cpu");
echo '<select name="cpu" class="cpu" onChange = $("#cpuprice").val($(this).find("option:selected").attr("cpuprice"))>';
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option cpuprice = ' . $obj['price'] . ' cpuname=' . $obj['cpuname'] . ' >' . $obj['cpuname'] . '</option> /n';
}
echo '</select>';
?>
</td>
<td>
<output id="cpuprice" disabled value="">
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>GPU</td>
<td>
<?php
//Retrieving GPU table
$query = $conn->query("SELECT * FROM gpu");
echo '<select name="gpu" class ="gpu" onChange = $("#gpuprice").val($(this).find("option:selected").attr("gpuprice"))>';
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option gpuprice = "' . $obj['price'] . '" gpuname = "' . $obj['gpuname'] . '">' . $obj['gpuname'] . '</option>';
}
echo '</select>';
?>
</td>
<td>
<output class="form-control prc" id="gpuprice" disabled value="">
</td>
</tr>
</tbody>
</table>
<input class="submit" type="submit" />
</form>
我試過了,但它不起作用,單擊提交后回顯添加錯誤並且沒有任何內容插入我的數據庫
<?php
if (!empty($_POST["cpu"]) && !empty($_POST["gpu"])) {
$cpu = isset($_POST["cpu"]);
$gpu = isset($_POST["gpu"]);
$qstr = "INSERT INTO trycombuild(cpuname, gpuname) VALUES ('$cpu' , '$gpu')";
$query = mysqli_query($conn, $qstr);
} else
echo 'adding error';
?>
我試圖回顯$cpu
和$gpu
,它說未定義的變量
我也試過這個:
if (!empty($_POST['cpu']) && !empty($_POST['gpu'])) {
$cpu = $_POST['cpu'];
$gpu = $_POST['gpu'];
$qstr = $conn->prepare("INSERT INTO trycombuild (cpuname, gpuname) VALUES (?, ?)");
$qstr->bind_param("ss", $cpu, $gpu);
$qstr->execute();
$sqtr->close();
}
您的 html 不正確。 刪除不必要的空間並嘗試
<tbody>
<tr>
<td>CPU</td>
<td>
<?php
//Retrieving CPU table
$query = $conn->query("SELECT * FROM cpu");
echo '<select name="cpu" class="cpu" onChange = $("#cpuprice").val($(this).find("option:selected").attr("cpuprice"))>';
echo "<option>---select your CPU---</option>/n";
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option cpuprice = ' . $obj['price'] . ' cpuname=' . $obj['cpuname'] . ' >' . $obj['cpuname'] . '</option> /n';
}
echo '</select>';
?>
</td>
<td>
<output id="cpuprice" disabled value="">
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>GPU</td>
<td>
<?php
//Retrieving GPU table
$query = $conn->query("SELECT * FROM gpu");
echo '<select name="gpu" class ="gpu" onChange = $("#tpuprice").val($(this).find("option:selected").attr("gpuprice"))>';
echo "<option>---select your GPU---</option>";
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option gpuprice = "' . $obj['price'] . '" gpuname = "' . $obj['gpuname'] . '">' . $obj['gpuname'] . '</option>';
}
echo '</select>';
?>
</td>
<td>
<output class="form-control prc" id="tpuprice" disabled value="">
</td>
</tr>
</tbody>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.