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

是否将选中项插入另一个sql表?(PHP和SQL),php,sql,Php,Sql,大家好,我是新来的!我想知道当选中特定复选框时,如何插入打印行的值 这是我的密码: <?php $query = "SELECT * FROM phpmyreservation_forvalidation"; $result = mysql_query($query); ?> <form action='insertrecords.php' method='post'> <?php echo "<table cellpadding='0' cellspac

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

这是我的密码:

<?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> 


将“phpmyreservation\u for Validation”结果提取到变量中,并在提交表单时将其插入到“phpmyreservation\u Reservation”表中。

在records.php中:

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

如果我是您,我将使用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>

书名
//更改复选框时调用函数
函数MyCheckboxChanged(发送方)
{
//如果选中该复选框,则将要处理的数据发送到服务器
如果(发件人已选中)
{
var MyObject={};
MyObject.ValueToInsert=sender.value;
数据(“插入”,MyObject);
}
}
//负责AJAX调用的函数-需要jQuery
函数DoAJAXData(命令,MyObject)
{
$.post(“phpreciver.php”,
{
命令:命令,,
sendObject:JSON.stringify(MyObject)
})
.成功(功能(数据)
{
//处理从php回显的数据
//在我的情况下,应该提醒:“成功插入数据:”+您选择的复选框值
警报(数据);
})
.失败(功能(错误){
//警报(“无法从服务器检索数据”);
});
}
以及phpreciver.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";

}
?>


谢谢!插入值是这样的吗?mysql_查询(“插入到“'phpmyreservation_reservations.”(预订时间、预订年份、预订周、预订日、预订时间、预订价格、预订用户id、预订用户电子邮件、预订用户名称)值(“$val”)”;请详细说明或解释您的问题。事实上,我知道了,我需要做的是使每一行都有一个隐藏的输入类型,并将其值放入变量中,以将其插入数据库:DThanks!下次我做类似的操作时,我肯定会这样做