按钮单击后更新数据库

时间:2016-10-24 19:01:53

标签: javascript php jquery database

我有一个动态表,可以内联编辑,也可以动态添加行。我希望能够点击运行UPDATE查询的保存按钮来更新数据库。但我无法弄清楚如何。我真的很坚持这一点,并希望得到任何帮助。

这是一个codepen:http://codepen.io/anon/pen/yawyQQ

您可以在codepen中找到大多数代码......我将提供一些HTML / PHP代码和Ajaxsubmit代码。

HTML / PHP代码:

<html>

    <head>
        <title>Stage Rebate Master HTML Table</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
         <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
         <link rel="stylesheet" type="text/css" href="html_master.css">
         <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <script type="text/javascript" src="html_master.js"></script>
    </head>

    <label id="table_name">Stage_Rebate_Master</label><br>

<body>    

<div id="dialog-form" title="Add Vendor">
  <p class="validateTips">All form fields are required.</p>

  <form>
    <fieldset>
      <label for="mr_name">Vendor</label>
      <input type="text" name="mr_name" id="mr_name" class="text ui-widget-content ui-corner-all">
      <label for="buyer_id">Buyer ID</label>
      <input type="text" name="buyer_id" id="buyer_id" class="text ui-widget-content ui-corner-all">
      <label for="poc_n">POC Name</label>
      <input type="text" name="poc_n" id="poc_n" class="text ui-widget-content ui-corner-all">
      <label for="poc_p">POC Email</label>
      <input type="text" name="poc_e" id="poc_e" class="text ui-widget-content ui-corner-all">
      <label for="poc_p">POC Phone</label>
      <input type="text" name="poc_p" id="poc_p" class="text ui-widget-content ui-corner-all">

      <!-- Allow form submission with keyboard without duplicating the dialog button -->
      <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>



<div id="users-contain" class="ui-widget">
<table id="html_master" class="ui-widget ui-widget-content">
<thead>
    <tr class="ui-widget-header">
    <td>ID</td>
    <td>Vendor</td>
    <td>Buyer ID</td>
    <td>POC Name</td>
    <td>POC Email</td>
    <td>POC Phone</td>
    <td>Edit/Delete</td>
    </tr>
</thead>
<tbody>

<?php
    foreach ($dbh->query($sql) as $rows){
    ?>
    <tr>
        <td class="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
        <td class="mr_name" contenteditable="false"><?php echo $rows['MR_Name']?></td>
        <td class="buyer_id" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
        <td class="poc_n" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>     
        <td class="poc_e" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
        <td class="poc_p" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
        <td><input type="button" class="edit" name="edit" value="Edit">
        <input type="button" class="deactivate" name="deactivate" value="Deactivate"></td>
    </tr>
 <?php
  }
 ?>
</tbody>

    <input type="button" class="create-user" value="Add Row">
    <input type="submit" value="Save Table" class="save">

</table>
</div>

    <input type="button" class="create-user" value="Add Row">
    <input type="submit" value="Save Table" class="save">

</body>


</html>

Ajaxsubmit代码:

<?php

$host="xxxx"; 
$dbName="xxxxxxx"; 
$dbUser="xxxxx"; 
$dbPass="xxxxxxxxxxxx";

$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


$data = $_POST['data'];
foreach($data as $row){
    $sql = "UPDATE Stage_Rebate_Master SET MR_Name='$row[mr_name]',
    Buyer_ID='$row[buyer_id]',
    MR_POC_N='$row[poc_n]',
    MR_POC_E='$row[poc_e]',
    MR_POC_N='$row[poc_p]'
    WHERE MR_ID='$row[mr_id]'";

    $dbh->query($sql);
}

?>

2 个答案:

答案 0 :(得分:0)

如果您只是想在点击“更新”时发生这种情况。按钮,您可以尝试使用

if(isset($_POST["Update"]))
{
 //PDO statement to update the database
}

只有名称为&#39;更新&#39;的按钮才会执行此操作。在表单中按下。

答案 1 :(得分:0)

您可以使用jquery或javascript发布和更新数据

jquery代码:

$.post("save2db.php",{
  id:"id",
  vendor:"vendor_name"
});

id:$ _POST [&#39; id&#39;]

供应商:$ _POST [&#39;供应商&#39;]

save2db.php将是:

    if(isset($_POST['vendor'])){
       $vendor=$_POST['vendor'];
       $id=$_POST['id'];
     //your update sql query
    }