2016-11-15 315 views
0

我試圖插入一行到動態HTML表格,然後在數據庫中更新它。我在一個單獨的文件中的php標籤內有一個SQL準備和執行語句。有沒有一種方法可以將變量放入執行語句中,該語句將讀取並存儲主頁上對話框中插入內容的信息,以便將任何輸入的內容插入到文本字段中以添加行?我到目前爲止在網上找到的唯一東西是將特定信息硬編碼到執行語句中,這是我不想要的。SQL執行語句

按鈕表:

<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連接,並執行語句:通過DB循環導入信息的行

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

HTML表格

<?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','[email protected]','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> 
+0

你的問題不是很清楚。你可能會[編輯]它並添加一個例子? – Chris

+1

@Chris我編輯它,並添加了一些代碼,使其更清晰 – Rataiczak24

+0

現在可以取消它!!我更新了它,現在更清晰了! – Rataiczak24

回答

0

一個選項可以使用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' ]; 
?>