从多个表中将值添加到选择表中

时间:2017-05-14 03:16:20

标签: php mysql

我到处搜寻,仍然无法找到答案。有人可以帮忙吗?

我有一个PHP表单,其中包含一个文本框(包含将进入列name_id的数据)和一个复选框,可以选择转到5个不同的MySQL表:

'table A'
'table B'
'table C'
'table D'
'table E'

人们可以选择他们想要的表格,name_id将转到所选的表格。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

我的答案与@ RandD-SexyBoy-不同。我的sql查询与@ RandD-SexyBoy不同 - 他使用$sql = "INSERT INTO $tables[$i](nameID_column);而不是VALUES (:nameID_column) 是用过$sql->prepare("INSERT INTO ".$v." (name_id) VALUES (?)"

以下是用户回复的3个案例

案例优先:用户可以选择单个表

案例第二:用户可以选择多个表格。这里我们必须使用foreach()循环和动态sql查询。

案例第三:在这种情况下,用户可能无法选择任何表格我们必须向用户提供消息table not selected

html表格:

`

<html>
    <head>

    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="name_id" required>

            <p>select your table to add data</p>
            <input type="checkbox" name="tables[]" value="tblA">Table A<br>
            <input type="checkbox" name="tables[]" value="tblB">Table B<br>
            <input type="checkbox" name="tables[]" value="tblC">Table C<br>
            <input type="checkbox" name="tables[]" value="tblD">Table D<br>
            <input type="checkbox" name="tables[]" value="tblE">Table E<br>

            <input type="submit" name="submit">
        </form>
    </body>
</html>

`

php文件:

 <?php
    $con = new mysqli('localhost','root','admin','demo');

    if(!$con){
        die("Connection ".$con->connect_error);
    }

    if(isset($_POST['submit'])){
        $name_id = $_POST['name_id'];

        $tables = $_POST['tables']; 

        if(!empty($tables)){
            foreach($tables as $key=>$v){
                $sql = $con->stmt_init();
                if($sql->prepare("INSERT INTO ".$v." (name_id) VALUES (?)")){
                    $sql->bind_param($name_id);
                    $sql->execute();

                    echo "DATA INSERTED";
                }
                else
                {
                    echo "Error".$con->error;
                }
            }
        }
        else
        {
            echo "You have not selected tables";
        }
    }
?>

答案 1 :(得分:0)

所以一个天真的方法可能是,当用户提交表单时,在你的php端,你检查勾选了哪些复选框,你的情况可能是这样的

if(checkbox_for_table_A) {// insert query}

if(checkbox_for_table_B) {// insert query}

if(checkbox_for_table_C) {// insert query}

if(checkbox_for_table_D) {// insert query}

if(checkbox_for_table_E) {// insert query}

这样,如果选中一个复选框,它将保存到该表中,如果选择了多个表,它将进入该表。

希望这有帮助!如果您对评论部分有疑问,请随时提出。

答案 2 :(得分:0)

根据来源:post checkbox value和教程http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-pdo/进行调整,您可以使用以下方法:

HTML表单:

<form action="checkbox-form.php" method="post">

    <input type="text" name="name_id" /><br />

    Select your database: <br/>
    <input type="checkbox" name="formTables[]" value="A" />Table A<br />
    <input type="checkbox" name="formTables[]" value="B" />Table B<br />
    <input type="checkbox" name="formTables[]" value="C" />Table C<br />
    <input type="checkbox" name="formTables[]" value="D" />Table D<br />
    <input type="checkbox" name="formTables[]" value="E" />Table E<br />

    <input type="submit" name="formSubmit" value="Submit" />

</form>

现在是PHP脚本:

<?php
  $pdo_connection = new PDO('mysql:host=localhost;dbname=test', 'demouser', 'demopass');
  $tables = $_POST['formTables'];
  if(empty($tables)) 
  {
    echo("You didn't select any tables.");
  } 
  else
  {
    $N = count($tables);

    echo("You selected $N table(s): ");

    for($i=0; $i < $N; $i++)
    {
      $sql = "INSERT INTO $tables[$i](nameID_column);
      $stmt = $pdo_connection->prepare($sql);
      $stmt->bindParam(':nameID_column', $_POST['name_id'], PDO::PARAM_STR);
      $stmt->execute(); 
      echo("Your ID Name was inserted into table $tables[$i] ! <br /> ");
    }
  }
?>