简体   繁体   中英

php mysql insert data with multiple rows in one page and one query

This is MySql query:

if ($_POST["ok"] == "OK") {
  $updateSQL = sprintf("UPDATE attend 
  SET 
  at_status=%s,
  at_remarks=%s WHERE at_tt_idx=%s",

                       GetSQLValueString($_POST['attend'], "text"),
                         GetSQLValueString($_POST['remarks'], "text"),
                         GetSQLValueString($_POST['tt_idx'], "int"));

  mysql_select_db($database_conn, $conn);
  $Result1 = mysql_query($updateSQL, $conn) or die(mysql_error());


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form3") && $_POST["ok"] != "OK") {
  $insertSQL = sprintf("INSERT INTO attend (at_matrix_no, at_status, at_remarks, at_staff_idx, at_sb_name , at_class, at_sb_code, at_tt_idx) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s)",

                      GetSQLValueString($_POST['st_matrix_no'], "text"),
                      GetSQLValueString($_POST['attend'], "text"),
                       GetSQLValueString($_POST['remarks'], "text"),
                       GetSQLValueString($_POST['tt_staff_idx'], "int"),
                         GetSQLValueString($_POST['sb_name'], "text"),
                       GetSQLValueString($_POST['tt_class'], "text"),
                       GetSQLValueString($_POST['sb_code'], "text"),
                       GetSQLValueString($_GET['tt_idx'], "int"));

  mysql_select_db($database_conn, $conn);
  $Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());

and this is my form

<?php $i=0; do { $i++;?>
    <td align="center" bgcolor="#CCCCCC"><?php echo $i;?></td>
        <input type="hidden" name="st_matrix_no[]" value="<?php echo $row_scs['st_matrix_no']; ?>"/></td>
         <td align="center" bgcolor="#CCCCCC"><label>

      <select name="attend[]" id="attend">
                <option value="/" <?php if (!(strcmp("Present", $row_scs['at_status']))) {echo "selected=\"selected\"";} ?>>Present</option>
                <option value="O" <?php if (!(strcmp("Absent", $row_scs['at_status']))) {echo "selected=\"selected\"";} ?>>Absent</option>
              </select>
      </label></td>

      <input type="text" name="remarks[]" value="<?php echo $row_scs['at_remarks']; ?>" size="15" maxlength="20"/>  
      <input type="hidden" name="tt_staff_idx[]" value="<?php echo $row_scs['tt_staff_idx']; ?>"/>
      <input type="hidden" name="tt_idx[]" value="<?php echo $row_scs['tt_idx']; ?>"/>
      <input type="hidden" name="sb_name[]" value="<?php echo $row_scs['sb_name']; ?>"/>
      <input type="hidden" name="tt_class[]" value="<?php echo $row_scs['tt_class']; ?>"/>
      <input type="hidden" name="sb_code[]" value="<?php echo $row_scs['sb_code']; ?>"/>
      <input type="hidden" name="start" value="<?php echo $row_scs['tt_datetime_start'];?>" />
      <input type="hidden" name="end" value="<?php echo $row_scs['tt_datetime_end'];?>" />
      </td>

im so sorry! this is my second post. i didnt notice it! sorry peeps. this is my code. i want to store multiple row data in my database. but it cant. it just store one row only data. :((. i got so many student to mark attendance. please help? sorry for my bad english

Your SQL certainly only inserts one row of data

INSERT INTO table (col1, col2, ...) VALUES ( %s, %s, ...)

You're going to need to loop through your form field arrays ( foreach , for , while , something iterative) to end up with SQL that looks more like this if you want to insert them all at once

INSERT INTO table (col1, col2, ...) VALUES ( %s, %s, ...), ( %s, %s, ...), ...

Ok the main problem I see is the way you are creating your HTML table. For the name attribute you are forcing an array, which is good but the way you have it:

<input type="hidden" name="tt_staff_idx[]" />
<input type="hidden" name="tt_idx[]"/>
<input type="hidden" name="sb_name[]" />

Will lead to this result:

tt_staff_idx = array("value1", "value2", "value3", etc...)
tt_idx= array("value1", "value2", "value3", etc...)
sb_name= array("value1", "value2", "value3", etc...)

Therefore, when you POST this information, you are probably only storing the very last value in the array.

What you need to do is setup your HTML inputs in a way that it builds a multi-dimensional array so you can iterate and store in your database. I would try something like this:

<input type="hidden" name="data[$i][tt_staff_idx]" />
<input type="hidden" name="data[$i][tt_idx]"/>
<input type="hidden" name="data[$i][sb_name]" />

so that you end up with this:

data= array(
   0 => array(
          tt_staff_idx => "";
          tt_idx=> "";
          sb_name=> "";
       )
   1 => array(
          tt_staff_idx => "";
          tt_idx=> "";
          sb_name=> "";
       )
)

Once you have this you can iterate in your PHP like this:

foreach($_POST['data'] as $data){
   ... your INSERT to DB code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM