简体   繁体   English

我想将数据从一种表格发送到许多表中,但是我选择的表却没有其他

[英]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.

相关问题 我从php创建一个HTML表单,该表单可以选择表格中的项目-我想使用javascript发送带有一些信息的所选项目 - From php I create a HTML form that mult selects items in a table - I want to send the selected items with some bits of information using javascript 我想从一个表中选择名称并显示在我的页面中 - i want select name form one table and display in my page 我想选择一个表并使用我选择的表中的相同数据创建一个表 - I want to select a table and create a table with the same data from the table i selected 选择一个表的特定数据时,将数据从一个表添加到另一个表 - Add data from one table to another when specific data form one table is selected 我想以表格形式显示来自循环的数据 - i want to display data which is coming from loop in a table form MySQL 一对多,只从多表中提取选定的记录 - MySQL One to many, pulling only selected record from many table 我想将数据从一张表移动到另一张表 - I Want to move data from one table to another 从一个表中提取数据,修改数据并将其插入另外两个表中? - Pulling data from one table, modifying the data, and inserting it into two others? 我想连接数据并显示一个表中的所有记录和另一个表中的一个记录 - I want to join data and display all records from one table and just one record from another table 我想根据另一个表格中的内容从表格中返回数据? - I want to return data form a table depending on whats in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM