【问题标题】:SQL Execute StatementSQL 执行语句
【发布时间】:2016-11-15 16:18:05
【问题描述】:

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

表格按钮:

<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 表:

<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
  }
 ?>

数据库连接和执行语句:

<?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);

?>

我希望从中提取信息并输入到执行语句中的对话框:

  <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>

【问题讨论】:

  • 你的问题不是很清楚。您能否edit 它并添加一个示例?
  • @Chris 我编辑了它并添加了一些代码使其更清晰
  • 现在可以取消了吗!?我更新了,现在更清楚了!
  • 我仍然不确定您在问什么,但也许您应该阅读有关 AJAX 的信息?
  • 我有多次...我唯一能找到的是将信息硬编码到我的执行语句中...我希望将主页对话框中输入的任何内容插入到我的执行语句中.

标签: php sql ajax execute


【解决方案1】:

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

您始终可以通过 ajax 的 'data:' 部分将变量传递给被调用的 PHP 页面;

<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 中;

<button id="addRow" onclick="addRow()">Add a Row</button>

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

<?php
$fileName=$_GET[ 'fileName' ];
$fileContents=$_GET[ 'fileContents' ];
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多