将选中的项插入另一个sql表? (PHP和SQL)

时间:2015-03-07 09:11:06

标签: php sql

大家好,我是新来的!我想知道如何在勾选特定复选框时插入打印行的值。

这是我的代码:

<?php
$query = "SELECT * FROM phpmyreservation_forvalidation";
$result = mysql_query($query);
?>

<form action='insertrecords.php' method='post'>
<?php
echo "<table  cellpadding='0' cellspacing='0' border='1'>";
echo "<tr> <th>reservation_id</th> <th>reservation_user_name</th> <th>reservation_day</th> <th>reservation_week</th> 
    <th> # </th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>"; 
echo $row['reservation_id'];   
echo "</td><td>"; 

echo $row['reservation_user_name'];
echo "</td><td>"; 

echo $row['reservation_day'];
echo "</td><td>"; 

echo $row['reservation_week'];
echo "</td><td align='center'>"; 

echo "</td>";  
?>

<td><input name="checkbox[]" type="checkbox" id="checkbox[]"  value="<?= $row['id'] ?>" ></td>

<?php
} 
echo "</table>";
?>


<input type='submit' value='Submit' />
</form> 

我不确定我所做的是否正确,我也没有插入records.php,我试图制作一个,但它没有用,现在我真的很困惑。有谁知道我怎么做到这一点?谢谢!

附加说明:我正在做的是验证系统,尚未验证的计划在一个单独的表上,当管理员验证它们时,它会被插入到phpmyreservation_reservations表中。

谢谢!

3 个答案:

答案 0 :(得分:0)

获取&#39; phpmyreservation_forvalidation&#39;将结果导入变量并将其插入到“phpmyreservation_reservations”中。表格提交时的表格。

答案 1 :(得分:0)

在records.php中:

$row_id=$_POST['checkbox'];
foreach ($row_id as $key=>$val)
{
    //do insert here with $val
}

答案 2 :(得分:0)

如果我是你,我会使用AJAX调用来获取数据库所需的所有数据,并将数据插入到新表中。在下面的例子中,我使用jQuery来执行ajax调用。

我的示例网页代码:

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Title of the page</title>
    <script src="jquery-1.10.2.min.js"></script>

    <script>
    // Function beeing called when a checkbox is changed
    function MyCheckboxChanged(sender)
    {

      // Send the data to be processed to the server if the checkbox is checked
      if (sender.checked)
      {
        var MyObject            = {};
        MyObject.ValueToInsert  = sender.value;

        DoAJAXData("insert", MyObject);
      }
    }

    // Function responsible for AJAX calls - requires jQuery
    function DoAJAXData(command, MyObject)
    {
      $.post("phpReceiver.php",
      {
        command:command,
        sendObject:JSON.stringify(MyObject)
      })
      .success(function(data)
      {
        // Process the data echoed from php
        // In my case, should  alert: "successfully inserted data: " + your selected checkbox value
        alert(data);

      })
      .fail(function(error){
        //alert("Unable to retrieve data from the server");
      });
    }

    </script>

</head>
<body>
<?php

for ($i=0; $i < 5; $i++) 
{ 
  $var = <<<CHB
  <input type="checkbox" onchange="MyCheckboxChanged(this);" name="$i" value="Bike number $i">I have a bike number $i</br>
CHB;
  echo $var;
}
?>

</body> 
</html>

和phpReceiver.php代码:

<?php

$command    = $_POST['command'];
$object     = json_decode($_POST['sendObject'], true);

if ($command == "insert") 
{
    $valueToInsert = $object['ValueToInsert'];

    // Do you insertion to the database here

    // Echo any results if wanted
    echo "successfully inserted data: $valueToInsert";

}
?>