如何使用带有PHP的html复选框插入INTO

时间:2022-11-29 20:56:05

What I need

I have the results of an Oracle query being displayed on a webpage in an html table. I have also added another column for checkboxes in each row. My code for this is below.

我需要的是我有一个Oracle查询的结果显示在html表的网页上。我还为每行中的复选框添加了另一列。我的代码如下。

<?php

(...some code with Oracle Query...)

echo "<form method='post'>";
echo "<table>";

while($row = oci_fetch_array($sql)) {
    echo "<tr>";
    echo "<td>" . $row['SKU'] . "</td>";
    echo "<td>" . $row['DESCRIPTION'] . "</td>";
    echo "<td>" . $row['LOCATION_MAX'] . "</td>";
    echo "<td>" . $row['QUANTITY'] . "</td>";
    echo "<td><input type='checkbox'></td>";
    echo "</tr>";
}

echo "</table>";
echo "</form>";

(...some more code...)

?>


When the checkbox for each row is clicked, I need to use INSERT INTO for a MySQL table that I have in another database.
I am having trouble wrapping my brain around how I am supposed to grab all of the information for each row and pass it to another script so I can insert the data into the MySQL table.

What I have tried

I figured that I would store each value of the row in an array, and then put that into the checkboxes value attribute and reference it with the name attribute, like so:

当单击每行的复选框时,我需要使用INSERT INTO作为我在另一个数据库中的MySQL表。我无法绕过我应该抓住每一行的所有信息并将其传递给另一个脚本,因此我可以将数据插入MySQL表中。我试过的,我想我会将行的每个值存储在一个数组中,然后将其放入checkboxes值属性并使用name属性引用它,如下所示:

...
echo "<input type='submit' name='submit' value='submit'>";
while($row = oci_fetch_array($sql)) {

    $rowVal = array(
        "SKU" => $row['SKU'],
        "DESCRIPTION" => $row['DESCRIPTION'],
        "LOCATION_MAX" => $row['LOCATION_MAX'],
        "QUANTITY" => $row['QUANTITY']
    );

    echo "<tr>";
    echo "<td>" . $row['SKU'] . "</td>";
    echo "<td>" . $row['DESCRIPTION'] . "</td>";
    echo "<td>" . $row['LOCATION_MAX'] . "</td>";
    echo "<td>" . $row['QUANTITY'] . "</td>";
    echo "<td><input type='checkbox' name='checkbox' value='" . $rowVal . "'></td>";
    echo "</tr>";
}


So, I am trying to reference this in the $_POST['name'] attribute in the bottom of the script (or in another script). See below.

所以,我试图在脚本底部(或另一个脚本)的$ _POST ['name']属性中引用它。见下文。

if (isset($_POST['submit'])) {
    foreach($_POST['checkbox'] as $checkVal) {
        /*(do stuff here, like print out all check rows first
        so I can understand what is going on, then eventually
        write the MySQL INSERT INTO query.)*/
}



What My Problems Are

1) Quite honestly, I am having trouble wrapping my brain around this in the first place. I really thought that storing all of the $row information in an array and assigning that array variable to the value attribute would work no problem. However, I can't figure out how to call all checked boxes once the submit button is pressed.

The thing is, when I put the array in the value attribute, I get an error/notice that reads Notice: Array to string conversion in [/script location.php] on line [where the checkbox code is].

我的问题是什么1)老实说,我首先在整理这个问题时遇到了麻烦。我真的以为将所有$ row信息存储在一个数组中并将该数组变量赋值给value属性都没有问题。但是,一旦按下提交按钮,我无法弄清楚如何调用所有复选框。问题是,当我把数组放在value属性中时,我得到一个错误/通知,它在[行/复选框代码为]的[/ script location.php]中读取注意:数组转换为字符串。

2) Let's say I didn't care about the notice above. I still need to reference the data that is checked so I can insert it into the MySQL database I have. I don't even see this as being difficult. Once I reference all of the checked data, I can cycle through that array with a foreach and execute an insert query for each row in the query.

2)假设我不关心上面的通知。我仍然需要引用已检查的数据,以便将其插入到我拥有的MySQL数据库中。我甚至认为这很困难。一旦我引用了所有已检查的数据,我就可以使用foreach遍历该数组,并对查询中的每一行执行插入查询。

How do I make this happen? Thanks in advance! If I left out any vital information that any helper needs, please let me know and I can update this question.

我该如何做到这一点?提前致谢!如果我遗漏了任何帮助者需要的重要信息,请告诉我,我可以更新这个问题。

My only request is that any solutions offered be in the scope of PHP, HTML, and CSS/Oracle/MySQL if needed. I am not too familiar with Javascript or any of it's libraries and because this is somewhat time-sensitive, I don't have the time to learn all that might go into it. So unless there is a javascript answer that is super straight forward, it will do more harm than good in my case.

我唯一的要求是,如果需要,提供的任何解决方案都在PHP,HTML和CSS / Oracle / MySQL的范围内。我不太熟悉Javascript或它的任何库,因为这有点时间敏感,我没有时间学习所有可能进入它的内容。因此,除非有一个超级直接的javascript答案,否则在我的情况下弊大于利。

Thanks!

谢谢!

-Anthony

- 安东尼

1 个解决方案

#1


1  

Quick and dirty and without testing but...

快速,肮脏,没有测试,但......

$rowNum = 0;
while($row = oci_fetch_array($sql)) {

    $rowVal = array(
       "SKU" => $row['SKU'],
       "DESCRIPTION" => $row['DESCRIPTION'],
       "LOCATION_MAX" => $row['LOCATION_MAX'],
        "QUANTITY" => $row['QUANTITY']
    )

    echo "<tr>";
    echo "<td>" . $row['SKU'] . "</td>";
    echo "<td>" . $row['DESCRIPTION'] . "</td>";
    echo "<td>" . $row['LOCATION_MAX'] . "</td>";
    echo "<td>" . $row['QUANTITY'] . "</td>";
    echo "<td>" .
           "<input type='hidden' name='opt[$rowNum]' value='". implode("::", $rowVal) ."'>".
           "<input type='checkbox' name='checkbox[$rowNum]' value='$rowNum'>".
         "</td>";
    echo "</tr>";

    $rowNum++;
}

You just put the array value in a hidden input and then on your post handling code:

您只需将数组值放在隐藏的输入中,然后放在后处理代码上:

// Note that you need to check if all values are $_POSTEd, 
// this is a quick and dirty solution!
if (isset($_POST['submit'])) {
  $data = [];
  foreach($_POST['checkbox'] as $rowNum) {
    $data[] = explode("::", $_POST['opt'][$rowNum]);
  }
  var_dump($data);
}

// Output sample (selected row 2 and 4):
array (size=2)
  0 => 
    array (size=4)
      0 => string 'SKU2' (length=4)
      1 => string 'DESC2' (length=5)
      2 => string 'LOC2' (length=4)
      3 => string 'QUAN2' (length=5)
  1 => 
    array (size=4)
      0 => string 'SKU4' (length=4)
      1 => string 'DESC4' (length=5)
      2 => string 'LOC4' (length=4)
      3 => string 'QUAN4' (length=5)

#1


1  

Quick and dirty and without testing but...

快速,肮脏,没有测试,但......

$rowNum = 0;
while($row = oci_fetch_array($sql)) {

    $rowVal = array(
       "SKU" => $row['SKU'],
       "DESCRIPTION" => $row['DESCRIPTION'],
       "LOCATION_MAX" => $row['LOCATION_MAX'],
        "QUANTITY" => $row['QUANTITY']
    )

    echo "<tr>";
    echo "<td>" . $row['SKU'] . "</td>";
    echo "<td>" . $row['DESCRIPTION'] . "</td>";
    echo "<td>" . $row['LOCATION_MAX'] . "</td>";
    echo "<td>" . $row['QUANTITY'] . "</td>";
    echo "<td>" .
           "<input type='hidden' name='opt[$rowNum]' value='". implode("::", $rowVal) ."'>".
           "<input type='checkbox' name='checkbox[$rowNum]' value='$rowNum'>".
         "</td>";
    echo "</tr>";

    $rowNum++;
}

You just put the array value in a hidden input and then on your post handling code:

您只需将数组值放在隐藏的输入中,然后放在后处理代码上:

// Note that you need to check if all values are $_POSTEd, 
// this is a quick and dirty solution!
if (isset($_POST['submit'])) {
  $data = [];
  foreach($_POST['checkbox'] as $rowNum) {
    $data[] = explode("::", $_POST['opt'][$rowNum]);
  }
  var_dump($data);
}

// Output sample (selected row 2 and 4):
array (size=2)
  0 => 
    array (size=4)
      0 => string 'SKU2' (length=4)
      1 => string 'DESC2' (length=5)
      2 => string 'LOC2' (length=4)
      3 => string 'QUAN2' (length=5)
  1 => 
    array (size=4)
      0 => string 'SKU4' (length=4)
      1 => string 'DESC4' (length=5)
      2 => string 'LOC4' (length=4)
      3 => string 'QUAN4' (length=5)