首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL Execute语句

SQL Execute语句
EN

Stack Overflow用户
提问于 2016-11-16 00:18:05
回答 1查看 105关注 0票数 0

我正尝试在动态HTML表中插入一行,然后在数据库中更新它。我在一个单独的文件中的php标记内有一条SQL准备和执行语句。有没有一种方法可以将变量放在execute语句中,该语句将读取和存储插入到主页对话框中的信息,这样它就可以插入任何输入到文本字段中的内容来添加一行??到目前为止,我在网上能找到的唯一一件事就是将特定信息硬编码到execute语句中,这是我不想要的。

表格按钮:

代码语言:javascript
复制
<form> 
    Table Name: <input type="text" value="Stage_Rebate_Master" id="tableNameInput">
    <button class="create-user" id="insertButton">Insert Test Object</button>
    <button id="updateButton">Update Test Object</button>
    <button id="deleteButton">Delete Test Object</button>
    </form>

通过DB循环以导入信息行的HTML表:

代码语言:javascript
复制
<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 loop that brings in information to populate table */
    foreach ($dbh->query($sql) as $rows){
    ?>
    <tr>
        <td class="mr_id" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
        <td class="mr_name" name="field" 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
  }
 ?>

DB连接和execute语句:

代码语言:javascript
复制
<?php

  $tableName = $_POST['tableName'];

  $host="xxxx"; 
  $dbName="xxxxxx"; 
  $dbUser="xxxxxxxxxxx"; 
  $dbPass="xxxxxx";

  $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);

  $sql = "INSERT INTO ".$tableName." (MR_ID, MR_Name, Buyer_ID, MR_POC_N, MR_POC_E, MR_POC_P) VALUES (?, ?, ?, ?, ?, ?)";
  $stmt = $pdo->prepare($sql);
  $result = $stmt->execute(array(0,'Test Object', '1234', 'John','john@example.com','555-555-5555'));  
  echo json_encode($result);

?>

对话框,我希望从其中提取信息并将其输入到我的execute语句中:

代码语言:javascript
复制
  <p>All form fields are required.</p>

  <form>
    <fieldset>
      <label for="mr_name">Vendor</label>
      <input type="text" id="mr_name">
      <label for="buyer_id">Buyer ID</label>
      <input type="text" id="buyer_id">
      <label for="poc_n">POC Name</label>
      <input type="text" id="poc_n">
      <label for="poc_p">POC Email</label>
      <input type="text" id="poc_e">
      <label for="poc_p">POC Phone</label>
      <input type="text" id="poc_p">

      <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
EN

回答 1

Stack Overflow用户

发布于 2016-11-16 18:59:21

一种选择是使用AJAX在单独的PHP文件中调用代码,并使用AJAX将行添加到表中,或者在完成SQL查询后让它重新加载页面。

你可以通过ajax的“data:”部分将变量传递给被调用的PHP页面;

代码语言:javascript
复制
<script type="text/javascript">
function addRow() {
ajaxWrite = $.ajax({
    url: './requesters/addRow.php',
    type: 'GET',
    dataType: 'text',
    data: { fileName: "." + propFolder + currentUser + userTimings,
            fileContents: JSON.stringify( slotTimings )
    },
    beforeSend: function( xhr, settings ) {
        $( "#fileLoadIndicator" ).fadeIn( 0, "linear" );
        document.getElementById( "fileLoadIndicator" ).innerHTML = "Creating custom user file for: " + currentUser + "<br>Now uploading file to server.";
    },
    success: function( data, textStatus, xhr ) {
        document.getElementById( "fileLoadIndicator" ).innerHTML = xhr.responseText;
        $( "#fileLoadIndicator" ).delay( 5000 ).fadeOut( "slow", "linear" );
    },
    error: function( xhr, textStatus, errorThrown ) {
        document.getElementById( "fileLoadIndicator" ).innerHTML = "No custom user file was able to be written for: " + currentUser + "<br>Error: " + errorThrown + ".";
        $( "#fileLoadIndicator" ).delay( 5000 ).fadeOut( "slow", "linear" );
    }
});

}

在HTML中;

代码语言:javascript
复制
<button id="addRow" onclick="addRow()">Add a Row</button>

要将它们提取出来,这是一个简单的PHP变量赋值;

代码语言:javascript
复制
<?php
$fileName=$_GET[ 'fileName' ];
$fileContents=$_GET[ 'fileContents' ];
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40614747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档