使用php通过单个提交按钮插入多行数据

时间:2015-06-03 18:08:51

标签: php mysql

如何从SQL表中检索数据,修改数据并将其存储在具有多行&的另一个数据库表中。列和单个提交按钮我想一次插入每一行我不知道如何获取该隐藏值并正确使用

<?php
 include"connect_database.php";
 if(isset($_POST['submit'])) {
         $amt = $_POST['total'];
         if($amt > 0) {
                $qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
                for($i=1; $i<=$amt; $i++) {
                        $qry .= "('".$_POST["rollno$i"]."', '".$_POST["name$i"]."', '".$_POST["year$i"]."', '".$_POST["attendance$i"]."', '".$_POST["reason$i"]."' ),"; // loop the mysql_query values to avoid more server loding time
                }
                $qry    = substr($qry, 0, strlen($qry)-2);
                $insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
         }
 // Redirect for each cases
         if($insert) {
                $msg = '<script type="text/javascript">alert("added");</script>';
         }
         else {
                $msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
         }

};

if (isset($_POST['select']))
{
  $sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);


  echo "<table border=1>
  <tr>
  <th>Rollno</th>
  <th>Name</th>
  <th>Year</th>
  <th>Attendance</th>
  <th>reason</th>
  </tr>";

 for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
  echo "<tr>";
  echo "<td>" . "<input type=text name=rollno$i value=" . $record['rollno'] . " </td>";
  echo "<td>" . "<input type=text name=name$i value=" . $record['name'] . " </td>";
  echo "<td>" . "<input type=text name=year$i  value=" . $record['year']  . " </td>";
 echo  "<td> "."<select name=attendance$i >
      <option value=Present >present</option>
      <option value=Absent >Absent</option>
  </select>"."</td>";
  echo "<td>". "<textarea cols=15 rows=2 name=reason$i  placeholder=Enter reason ...></textarea>" . "</td>" ;
  echo "<td>" . "<input type=hidden name=total value=" . $i-1 . "</td>";
  echo "</tr>";


}

echo"</table>";

echo "<input type=submit name=submit value=save class=Button3>";

echo "</form>";




 };


mysqli_close($dbcon);


?>

2 个答案:

答案 0 :(得分:0)

    <?php
     include"connect_database.php";
     if(isset($_POST['submit'])) {
             $amt = $_POST['total'];
     $rollnos= $_POST['rollno'];
             if($amt > 0) {
                    $qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
    $i=0;
                    foreach($rollnos as $rollno) {
                            $qry .= "('".$rollno."', '".$_POST["name"][$i]."', '".$_POST["year"][$i]."', '".$_POST["attendance"][$i]."', '".$_POST["reason"][$i]."' ),"; // loop the mysql_query values to avoid more server loding time
$i=$i+1;
                    }
                    $qry    = substr($qry, 0, strlen($qry)-2);
                    $insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
             }
     // Redirect for each cases
             if($insert) {
                    $msg = '<script type="text/javascript">alert("added");</script>';
             }
             else {
                    $msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
             }

    };

    if (isset($_POST['select']))
    {
      $sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
    $myData = mysqli_query($dbcon, $sql);
    $num = mysqli_num_rows($myData);


      echo "<table border=1>
      <tr>
      <th>Rollno</th>
      <th>Name</th>
      <th>Year</th>
      <th>Attendance</th>
      <th>reason</th>
      </tr>";

     for ($i=0; $i <$num; $i++)
    {
    $record = mysqli_fetch_array($myData);
    echo "<form action=smanage.php method=post>";
      echo "<tr>";
      echo "<td>" . "<input type='text' name='rollno[]' value='" . $record['rollno'] . "'> </td>";
      echo "<td>" . "<input type='text' name='name[]' value='" . $record['name'] . "'> </td>";
      echo "<td>" . "<input type='text' name='year[]'  value='" . $record['year']  . "'> </td>";
     echo  "<td> "."<select name='attendance[]' >
          <option value='Present' >present</option>
          <option value='Absent' >Absent</option>
      </select>"."</td>";
      echo "<td>". "<textarea cols='15' rows='2' name='reason[]'  placeholder='Enter reason ...'></textarea>" . "</td>" ;
      echo "<td></td>";
      echo "</tr>";


    }
    echo   "<input type='hidden' name='total' value='" . $i-1 . "'>";
    echo"</table>";

    echo "<input type='submit' name='submit' value='save' class='Button3'>";

    echo "</form>";




     };


    mysqli_close($dbcon);


    ?>

答案 1 :(得分:0)

您要为表格中的每一行打开多个表单。 这会导致您的html无效,只需在显示表格之前启动表单。

你可以使用这个html

<table>
<?php
for ($i = 0; $i < $num; $i++) {
    $record = mysqli_fetch_array($myData);
?>
    <tr>
        <td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
        <td><input type="text" name="name[<?= $record['rollno'] ?>]" value="<?= $record['name']?>" </td>
        <td><input type="text" name="year[<?= $record['rollno'] ?>]"  value="<?= $record['year'] ?>" </td>
        <td><select name="attendance[<?= $record['rollno'] ?>]" >
                <option value="Present" >present</option>
                <option value="Absent" >Absent</option>
            </select></td>
        <td><textarea cols="15" rows="2" name="reason[<?= $record['rollno'] ?>]"  placeholder="Enter reason ..."></textarea></td>
    </tr>
<?php
}
?>
</table>

使用此值,您的每一行都将放入$ _POST-Array,您可以通过索引访问值(我猜测rollno表示数据集的ID)。 当您真的只想将所有值插入表中时,可以将索引保留为out。意思是你可以写

<td><input type="text" name="rollno[]" value="<?= $record['rollno'] ?>" </td>

而不是

<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>

您不需要隐藏字段,您只需计算数组中的项目。

$total = count($_POST['rollno']);
相关问题
最新问题