【问题标题】:Add multiple combined PHP form data from multiple foreign MSSQL tables to one primary table将多个外部 MSSQL 表中的多个组合 PHP 表单数据添加到一个主表
【发布时间】:2019-02-02 19:47:20
【问题描述】:

我在这个例子中得到了 7 个复选框:

    <table style="border-collapse: collapse; width: 100%;" border="1">
    <tbody>


    <tr style="height: 21px;">
    <td style="width: 25%; height: 21px;"><strong>Technologie</strong></td>
    <td style="width: 25%; height: 21px;"></td>
    <td style="width: 25%; height: 21px;"></td>
    <td style="width: 25%; height: 21px;"></td>
    </tr>
    <tr style="height: 21px;">
    <td style="width: 25%; height: 21px;">Tec1</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="1" /> </td>
    <td style="width: 25%; height: 21px;">Tec2</td>
    <td style="width: 25%; height: 21px;"><input name="Technoloie[]" type="checkbox" value="1" /></td>
    </tr>
    <tr style="height: 21px;">
    <td style="width: 25%; height: 21px;">Tec3</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="1" /> </td>
    <td style="width: 25%; height: 21px;"Tec4</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="1" /> </td>
    </tr>
    <tr style="height: 21px;">
    <td style="width: 25%; height: 21px;">Tec5</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="1" /> </td>
    <td style="width: 25%; height: 21px;">Tec6</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="1" /> </td>
    </tr>
    <tr style="height: 21px;">
    <td style="width: 25%; height: 21px;"></td>
    <td style="width: 25%; height: 21px;">Tec7</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="1" /> </td>
   <td style="width: 25%; height: 21px;">Tec8</td>
    <td style="width: 25%; height: 21px;">  <input name="Technoloie[]" type="checkbox" value="2" /> </td>

    </tr>
    </tbody>
    </table>

这是此复选框的 SQL 表:

+--------+------+------+------+------+------+------+------+------+------+
| Tec_ID | Tec1 | Tec2 | Tec3 | Tec4 | Tec5 | Tec6 | Tec7 | Tec8 |RanNr |
+--------+------+------+------+------+------+------+------+------+------+
|      1 |    1 |    0 |    0 |    0 |    1 |    0 |    0 |    0 | 1353 |
|      2 |    1 |    0 |    0 |    0 |    0 |    1 |    0 |    0 | 0000 |
|      3 |    1 |    0 |    0 |    0 |    0 |    0 |    1 |    1 | 1353 |
|      4 |    1 |    1 |    1 |    0 |    1 |    0 |    0 |    0 | 1123 |
|      5 |    1 |    1 |    1 |    0 |    0 |    1 |    0 |    2 | 1353 |
|      6 |    1 |    1 |    1 |    0 |    0 |    0 |    1 |    2 | 1353 |
|      7 |    0 |    0 |    0 |    1 |    0 |    0 |    0 |    1 | 1993 |
|      8 |    0 |    1 |    1 |    0 |    1 |    0 |    0 |    0 | 1123 |
|      9 |    0 |    1 |    1 |    0 |    0 |    1 |    0 |    0 | 1353 |
|     10 |    0 |    0 |    0 |    0 |    0 |    0 |    0 |    2 | 1366 |
+--------+------+------+------+------+------+------+------+------+------+

如果我检查Tec1Tec5,您已经看到了,我想获得Tec_ID 1,所以我需要一个组合复选框选择以获得正确的ID,并且我想将此主键作为外来插入键入另一个表以在其他功能中使用id 进行处理。

但是 atm 我不知道如何在 MSSQL 和 PHP 代码中处理这个问题?有人可以帮忙吗?

原表

FlashID SAP COB SMT BGA TSOP    LGA
1   102292  0   1   0   2   0
3   102293  0   1   0   2   0
4   102294  0   1   0   2   0
5   102296  0   1   0   0   1
6   102412  0   1   0   1   0
7   102413  0   1   0   1   0
8   102414  0   1   0   1   0
9   102651  0   1   0   2   0
10  102652  0   1   0   2   0
11  102664  0   1   0   2   0

【问题讨论】:

    标签: php html sql-server checkbox


    【解决方案1】:

    解决方案:

    带有一些注释的简单示例:

    表(T-SQL 脚本):

    CREATE TABLE [dbo].[TechnoloieTable] (
        Tec_ID int,
        Tec1 int,
        Tec2 int,
        Tec3 int,
        Tec4 int,
        Tec5 int,
        Tec6 int,
        Tec7 int,
        Tec8 int
    )
    
    INSERT INTO [dbo].[TechnoloieTable] 
        (Tec_ID, Tec1, Tec2, Tec3, Tec4, Tec5, Tec6, Tec7, Tec8)
    VALUES
        (1, 1, 0, 0, 0, 1, 0, 0, 0),
        (2, 1, 0, 0, 0, 0, 1, 0, 0),
        (3, 1, 0, 0, 0, 0, 0, 1, 1),
        (4, 1, 1, 1, 0, 1, 0, 0, 0),
        (5, 1, 1, 1, 0, 0, 1, 0, 2),
        (6, 1, 1, 1, 0, 0, 0, 1, 2),
        (7, 0, 0, 0, 1, 0, 0, 0, 1),
        (8, 0, 1, 1, 0, 1, 0, 0, 0),
        (9, 0, 1, 1, 0, 0, 1, 0, 0),
        (10, 0, 0, 0, 0, 0, 0, 0, 2)
    

    HTML (checkbox-table.php):

    <html>
    <body>
    
    <form action="checkbox-table-submit.php" method="post">
    
        <table style="border-collapse: collapse; width: 100%;" border="1">
        <tbody>
        <tr style="height: 21px;">
        <td style="width: 25%; height: 21px;"><strong>Technologie</strong></td>
        <td style="width: 25%; height: 21px;"></td>
        <td style="width: 25%; height: 21px;"></td>
        <td style="width: 25%; height: 21px;"></td>
        </tr>
        <tr style="height: 21px;">
        <td style="width: 25%; height: 21px;">Tec1</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec1]" type="checkbox" value="1"/></td>
        <td style="width: 25%; height: 21px;">Tec2</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec2]" type="checkbox" value="1"/></td>
        </tr>
        <tr style="height: 21px;">
        <td style="width: 25%; height: 21px;">Tec3</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec3]" type="checkbox" value="1"/></td>
        <td style="width: 25%; height: 21px;">Tec4</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec4]" type="checkbox" value="1"/></td>
        </tr>
        <tr style="height: 21px;">
        <td style="width: 25%; height: 21px;">Tec5</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec5]" type="checkbox" value="1"/></td>
        <td style="width: 25%; height: 21px;">Tec6</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec6]" type="checkbox" value="1"/></td>
        </tr>
        <tr style="height: 21px;">
        <td style="width: 25%; height: 21px;">Tec7</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec7]" type="checkbox" value="1"/></td>
        <td style="width: 25%; height: 21px;">Tec8</td>
        <td style="width: 25%; height: 21px;"><input name="Technoloie[Tec8]" type="checkbox" value="2"/></td>
        </tr>
        </tbody>
        </table>
    
        <input type="submit">
    </form>
    
    </body>
    </html>
    

    PHP (checkbox-table-submit.php):

    <?php
    // Selection
    $selection = array();
    
    // Form selection
    if (isset($_POST['Technoloie'])) {
        foreach($_POST['Technoloie'] as $key => $value) {
            $selection[$key] = $value;
        }   
    }   
    if (empty($selection)) {
        echo 'Make a selection.';
        exit;
    }   
    
    // Statement generation.
    $sql = "SELECT Tec_ID FROM [dbo].[TechnoloieTable] WHERE ";
    foreach ($selection as $field => $value) {
        $sql .= "(".$field."=".$value.")AND";
    }   
    $sql = substr($sql, 0, strlen($sql)-3);
    
    // Connection with SQLSRV
    $server   = 'server\instance,port';
    $database = 'database';
    $username = 'username';
    $password = 'password';
    $cinfo = array(
        "Database" => $database,
        "UID" => $username,
        "PWD" => $password
    );
    $conn = sqlsrv_connect($server, $cinfo);
    if ($conn === false) {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    $stmt = sqlsrv_query($conn, $sql);  
    if ($stmt === false) {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    $id = 0;
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        $id = $row['Tec_ID'];
    }
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    
    // Echo ID
    echo 'ID: '.$id;
    ?>
    

    更新:

    如果您使用 PDO,请尝试使用以下命令执行您的语句:

    // Connection with PDO_SQLSRV
    $server   = 'server\instance,port';
    $database = 'database';
    $username = 'username';
    $password = 'password';
    try {
        $conn = new PDO("sqlsrv:server=$server;Database=$database", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        die("Error connecting to SQL Server".$e->getMessage());
    }
    try {
        $stmt = $conn->query($sql);
        $id = 0;
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $id = $row['Tec_ID'];
        }   
    } catch(PDOException $e) {
        die("Error executing query".$e->getMessage());
    }
    $stmt = null;
    $conn = null;
    

    【讨论】:

    • 非常感谢您的努力。你的代码会很有趣
      &lt;input type="checkbox" name="tec[Tec1]" value="1" /&gt; &lt;input type="checkbox" name="tec[Tec2]" value="1" /&gt; &lt;input type="checkbox" name="tec[Tec3]" value="1" /&gt;
      这样来自乔纳森。因为数据刚刚被匿名化了。
    • @Daniel 你的意思 - 复选框的名称是关联数组 (name="tec[Tec1]")?
    • 是的 sql 列名 tec1 = "Flash",例如 tec2 列名 = "DRAM"
    • @Daniel 更新了答案。
    • echo $sql; exit; $stmt = sqlsrv_query($connection, $sql); //$stmt-&gt;execute(); if ($stmt === false){ echo "Error ($connection): ".print_r(sqlsrv_error(), true); exit; } $id = 0; var_dump($id); while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){ $id = $row['Tec_ID']; } var_dump($id); sqlsrv_free_stmt($stmt); echo $id; thx echo $sql 工作正常,但我没有得到 $id 的回显,可能是什么问题?
    【解决方案2】:

    首先,您的输入需要是关联数组,否则您将不知道哪个值“1”与哪个“Tec”相关。

    <input type="checkbox" name="tec[Tec1]" value="1" />
    <input type="checkbox" name="tec[Tec2]" value="1" />
    <input type="checkbox" name="tec[Tec3]" value="1" />
    ...
    

    复选框只有在被选中时才会被发布,所以在 PHP 中你可以:

    $sql = "SELECT * FROM tec WHERE 1=1 "; //WHERE 1=1 is a nice 'hack' so you can have *none* or many AND statements.
    foreach($_POST['tec'] as $key => $value) {
        $sql .= "AND $key > 0 "; //Make sure to sanitise your data!!!
        //We can do this because checkboxes are only posted if they're ticked
    }
    
    $result = mysqli_query($link, $sql);
    
    $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
    
    if(count($rows) == 1) {
        $my_found_tec_id = $rows[0]['Tec_ID'];
    }
    
    //if count is not equal to 1, then do something else... question just talked about 1 match.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 2014-06-09
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      相关资源
      最近更新 更多