[英]I want to send data into many Table from one FORM but my selected table not others
I am new to in programming. 我是编程新手。 I want to send data into multiple tables by using one FORM but not every table just my selected table will be updated with data.
我想使用一个FORM将数据发送到多个表中,但不是每个表都只是我选择的表会更新数据。 It's similar to have an option when I select an option it will send only this table not others, I don't know is it a right method or wrong please help me...
当我选择一个选项时,它类似于有一个选项,它将仅发送此表而不发送给其他人,我不知道这是正确的方法还是错误的请帮助我...
<div class="row">
<div class="data-store text-center">
<?php
add_part('admin-menu.php');
?>
<?php
if(!empty($_POST)){
$title=$_POST['title'];
$sel_data='sel_data';
if($sel_data == 1){
$insert="INSERT INTO `database`.`gfx` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if(mysqli_query($con,$insert)){
echo "Product has been uploaded";
}
}else{
echo "Failed to store data";
}
if($sel_data == 2){
$insert="INSERT INTO `database`.`gfx` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if(mysqli_query($con,$insert)){
echo "Product has been uploaded";
}
}else{
echo "Failed to store data";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div>
<label for="title"></label>
<input name="title" id="title" type="text" placeholder="post title"/>
</div>
<select name="sel_data" id="">
<option name="" >Select one</option>
<option name="table1" value="1">Option 1</option>
<option name="table2" value="2">Option 2</option>
<option name="table3" value="3">Option 3</option>
</select>
<input type="submit" value="submit" />
</form>
</div>
</div>
Select tables from your database using 使用以下方法从数据库中选择表
" SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_database_name' " “从information_schema.tables中选择table_name,table_schema ='您的数据库名称'”
use resulted array for generating options using foreach. 使用结果数组使用foreach生成选项。 then instead of giving integer value for options, you should give name of table as value.
然后应该给表名称作为值,而不是给选项提供整数值。 after than you can change code as
之后,您可以将代码更改为
<?php
if(!empty($_POST)){
$title = $_POST['title'];
$sel_data = $_POST['sel_data'];
$insert="INSERT INTO `database`.`.$sel_data.` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if(mysqli_query($con,$insert)){
echo "Product has been uploaded";
}else{
echo "Failed to store data";
}
}
?>
By using above code entered title will be inserted into selected table only. 通过使用以上代码,输入的标题将仅插入到选定的表中。
You can optimize your code like this if all the table having the same structure. 如果所有表都具有相同的结构,则可以像这样优化代码。
<?php
if (!empty($_POST)) {
$title = $_POST['title'];
$sel_data = $_POST['sel_data'];
if (!empty($sel_data)) {
$insert = "INSERT INTO $sel_data.`gfx` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if (mysqli_query($con, $insert)) {
echo "Product has been uploaded";
}
} else {
echo "Failed to store data";
}
}
?>
<div class="row">
<div class="data-store text-center">
<?php
add_part('admin-menu.php');
?>
<form action="" method="post" enctype="multipart/form-data">
<div>
<label for="title"></label>
<input name="title" id="title" type="text" placeholder="post title"/>
</div>
<select name="sel_data" id="">
<option value="" >Select one</option>
<option value="table1">Option 1</option>
<option value="table2">Option 2</option>
<option value="table3">Option 3</option>
</select>
<input type="submit" value="submit" />
</form>
</div>
</div>
使用$ sel_data = $ _ POST ['sel_data']代替$ sel_data ='sel_data';
EDITED 已编辑
hey have you tried elseif? 嘿,您是否尝试过elseif? and also i think the only wrong thing was your structure
而且我认为唯一错误的是您的结构
<?php
if(!empty($_POST)){
$title=$_POST['title'];
$sel_data=$_POST['sel_data']; // you need to call the value of the select
if($sel_data == 1){
$insert="INSERT INTO `database`.`gfx` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if(mysqli_query($con,$insert)){
echo "Product has been uploaded";
}
else{
echo "Failed to store data";
}
}
elseif($sel_data == 2){
$insert="INSERT INTO `database`.`gfx` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if(mysqli_query($con,$insert)){
echo "Product has been uploaded";
}
else{
echo "Failed to store data";
}
}
elseif($sel_data == 3){
$insert="INSERT INTO `database`.`gfx` (`gfx_id`, `gfx_title`) VALUES (NULL, '$title');";
if(mysqli_query($con,$insert)){
echo "Product has been uploaded";
}
else{
echo "Failed to store data";
}
}
else {
echo"failed";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.