试图在点击按钮时将数据插入到另一个表格中去

问题描述 投票:0回答:2

我有一个表单,它从db中获取数据,并使用foreach循环将其显示在一个表中。在查看数据后,我想通过点击按钮将这些数据插入到另一个已经在DB中的表中。我已经尝试了多种方法,但似乎不能让它工作。任何帮助将是巨大的。谢谢!我的index.php页面

我的index.php页面

<div class='container-fluid'>
  <div class='row'>
    <div class='col-12'>
      <div class='table table-responsive'>
        <table class='table table-striped table-bordered datatable active' id='grantTable'>
          <thead>
              <tr>
                <th>FP ID</th>
                <th>Short Title</th>
                <th>PI</th>
                <th>Department</th>
                <th>Division</th>
                <th>Sponsor Name</th>
                <th>Project Start</th>
                <th>Project End</th>
                <th>Funding Type</th>
                <th>Yes/No</th>
                <th>Proper Type If No</th>
                <th>Comment</th>
              </tr>
          </thead>
          <tbody>
            <?php
            //Foreach loop iterates through each column of $getallrows function
            foreach($allRows as $rowID => $rowInfo){ ?>
              <tr>
                <td><?php echo $rowInfo['fpID'];?></td>
                <td><?php echo $rowInfo['shortTitle'];?></td>
                <td><?php echo $rowInfo['PI'];?></td>
                <td><?php echo $rowInfo['Department'];?></td>
                <td><?php echo $rowInfo['Division'];?></td>
                <td><?php echo $rowInfo['sponsorName'];?></td>
                <td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
                <td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
                <td><?php echo $rowInfo['fundingType'];?></td>
                <!-- //Create dynamic id -->
                <?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
                <form action="process.php" method="POST">
                <td>Yes<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="Yes"  id="rdYes"             onclick="disable('<?php echo $rdDrop;?>')" checked/><br />
                    No<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="No" id="rdNo" onclick="enable('<?php echo $rdDrop;?>')"/></td>
                  <input type="hidden" name="id" value="<?php echo $fpID; ?>"/>
                  <td>
                    <div class="dropdown">
                      <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="<?php echo $rdDrop;?>" data-toggle="dropdown" disabled>Select Proper Funding Type
                        <span class="caret"></span>
                      </button>
                      <ul class="dropdown-menu" aria-labelledby="ddgrantType">
                        <li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
                        <li><a data-value="Federal">Federal</a></li>
                        <li><a data-value="Foundation Selected">Foundation Selected</a></li>
                        <li><a data-value="Internally Funded">Internally Funded</a></li>
                        <li><a data-value="State/Local">State/Local</a></li>
                      </ul>
                    </div>
                  </td>
                  <td>
                    <div class="comment">
                      <textarea class="form-control" aria-label="With textarea" id="grantComment" placeholder="Comments"></textarea>
                    </div>
                  </td>
              </tr>
            <?php } ?>
          </tbody>
        </table>
          <div class="flex-container" id="saveBtn">
            <div class="dSave"><button type="submit" class="btn btn-success" name="submit" id="Save">Save</button></div>
            <div><button type="submit" class="btn btn-success" name="Complete" id="Complete">Complete and Save</button></div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

我的process.php页面

if(isset($_POST['submit'])) {
  $count = count($_POST['fpID']);

  for($i=0; $i < count; $i++) {
    $mssql = "INSERT into some_table (FP_id, Short_Title, PI, Department, Division, Sponsor, Date_Project_Start,     Date_Project_End, Funding_Type, Yes_No, Proper_Type_If_No, Comment, Complete_Flag)
              VALUES ('"
                $mssql .= $_POST['Funding Proposal ID'][$i] . "','";
                $mssql .= $_POST['Short Title'][$i] . "','";
                $mssql .= $_POST['PI'][$i] . "','";
                $mssql .= $_POST['Department'][$i] . "','";
                $mssql .= $_POST['Division'][$i] . "','";
                $mssql .= $_POST['Sponsor Name'][$i] . "','";
                $mssql .= $_POST['Date_Project_Start'][$i] . "','";
                $mssql .= $_POST['Date_Project_End'][$i] . "','";
                $mssql .= $_POST['Funding Type'][$i] . "','";
                $mssql .= $_POST['Yes_No'][$i] . "','";
                $mssql .= $_POST['Proper_Type_If_No'][$i] . "','";
                $mssql .= $_POST['Comment][$i] . "','";
                $mssql .= 'No' "')";
                }

            header("Location: index.php");

      }

?>

php mysql database if-statement button
2个回答
1
投票

这个。

$mssql .= $_POST['Funding Proposal ID'][$i] . "','"; 

应该是

$mssql .= $_POST[$i]['Funding Proposal ID'] . "','";

希望能帮到你


0
投票

看来你只把SQL查询建立在了 $mssql 变量,但你不对它做任何事情。你应该把变量的内容发送到数据库对象中,比如说

$result = db::$conn->query($mssql);

这样数据库就应该得到填充。把头语句放在for循环之外(在脚本的底部)。

© www.soinside.com 2019 - 2024. All rights reserved.