如何根据下拉选择将 table 表单提交到数据库中选定的 table
How to submit table form to a selected table in database based on drop down selection
我可以知道是否可以根据下拉列表 selection 将 table 表单提交到 mysqli 数据库中的 selected table?
例如,用户可以使用下拉菜单 selection 在 mysqli 数据库中 select table A 或 B 并单击发送。
下面的代码只是一个例子(但我的确切代码)
请求解决。
谢谢。
<form method="post" action="">
<table name="userform" >
<tr><th>Full Name</th>
<th> Week </th></tr>
<tr><td><input name="name" type="text" id="name"></td>
<td><input name="week" type="number" id="week"></td></tr>
</table>
<select name="sendToWho" >
<option value="tableA" >table A</option>
<option value="tableB" >table B</option>
<option value="tableC">table C</option>
<input type="submit" name="save" id="save" value="Save Data">
</form>
我用了 '".$name[$tableName]."' 因为它是在发送到 mysqli 数据库之前添加,删除,editable table,
<?php
$conn = mysqli_connect("localhost","root","","mySystem");
$sendTO = [
"tableA" => "optionA",
"tableB" => "optionB",
"tableC" => "optionC",
];
foreach ($sendTO as $tableName => $optionName)
{
$table = isset($_POST["userform"], $sendTO[$_POST["name"]])
? $sendTO[$_POST["week"]]
: $sendTO[0];
$sql = "INSERT INTO `$table`(Name, Week) VALUES ('".$name[$tableName]."','".$week[$tableName]."')";
$query = mysqli_query($conn,$sql);
}
?>
不完全确定您要实现的目标,但这样的事情可能会奏效:
$conn = mysqli_connect("localhost","root","","mySystem");
if (isset ($_POST['save'])){
$name = $_POST['name'];
$week = $_POST['week'];
$sendTo = $_POST['sendToWho'];
//check which option was selected and delegate the correct table accordingly
switch ($sendTo){
case 'tableA':
$stmt = $conn->prepare('INSERT INTO optionA (Name, Week) VALUES (?,?)');
break;
case 'tableB':
$stmt = $conn->prepare('INSERT INTO optionB (Name, Week) VALUES (?,?)');
break;
case 'tableC':
$stmt = $conn->prepare('INSERT INTO optionC (Name, Week) VALUES (?,?)');
break;
default:
echo "error";
break;
}
$stmt->bind_param('ss', $name, $week); // see www.php.net/manual/en/mysqli-stmt.bind-param.php
$stmt->execute();
}
我可以知道是否可以根据下拉列表 selection 将 table 表单提交到 mysqli 数据库中的 selected table?
例如,用户可以使用下拉菜单 selection 在 mysqli 数据库中 select table A 或 B 并单击发送。 下面的代码只是一个例子(但我的确切代码)
请求解决。 谢谢。
<form method="post" action="">
<table name="userform" >
<tr><th>Full Name</th>
<th> Week </th></tr>
<tr><td><input name="name" type="text" id="name"></td>
<td><input name="week" type="number" id="week"></td></tr>
</table>
<select name="sendToWho" >
<option value="tableA" >table A</option>
<option value="tableB" >table B</option>
<option value="tableC">table C</option>
<input type="submit" name="save" id="save" value="Save Data">
</form>
我用了 '".$name[$tableName]."' 因为它是在发送到 mysqli 数据库之前添加,删除,editable table,
<?php
$conn = mysqli_connect("localhost","root","","mySystem");
$sendTO = [
"tableA" => "optionA",
"tableB" => "optionB",
"tableC" => "optionC",
];
foreach ($sendTO as $tableName => $optionName)
{
$table = isset($_POST["userform"], $sendTO[$_POST["name"]])
? $sendTO[$_POST["week"]]
: $sendTO[0];
$sql = "INSERT INTO `$table`(Name, Week) VALUES ('".$name[$tableName]."','".$week[$tableName]."')";
$query = mysqli_query($conn,$sql);
}
?>
不完全确定您要实现的目标,但这样的事情可能会奏效:
$conn = mysqli_connect("localhost","root","","mySystem");
if (isset ($_POST['save'])){
$name = $_POST['name'];
$week = $_POST['week'];
$sendTo = $_POST['sendToWho'];
//check which option was selected and delegate the correct table accordingly
switch ($sendTo){
case 'tableA':
$stmt = $conn->prepare('INSERT INTO optionA (Name, Week) VALUES (?,?)');
break;
case 'tableB':
$stmt = $conn->prepare('INSERT INTO optionB (Name, Week) VALUES (?,?)');
break;
case 'tableC':
$stmt = $conn->prepare('INSERT INTO optionC (Name, Week) VALUES (?,?)');
break;
default:
echo "error";
break;
}
$stmt->bind_param('ss', $name, $week); // see www.php.net/manual/en/mysqli-stmt.bind-param.php
$stmt->execute();
}