2016-01-24 80 views
2

我有一個表格,從我的數據庫中的表中獲取數據的HTML網頁。每行都有一個按鈕,當點擊被動態地使用javascript添加到網頁上的另一個表中時。動態表有一個提交按鈕,點擊時,表中的數據應該添加到我的數據庫的表中。我的問題是表中的內容沒有添加到數據庫中,但每次按下提交按鈕時,都會添加一個具有唯一標識的新行。還有一個輸入文本框也被正確添加到我的數據庫中。這使我相信我的數據庫連接正在工作,但由於某種原因,表中的數據不會被添加。我怎樣才能讓動態表中的數據進入數據庫?這是我的代碼。代碼已經更新了建議。提交數據從動態表到數據庫 - PHP

<?php 
     $sql = "SELECT * 
       FROM inventoryCars"; 
     $result = mysqli_query($connection, $sql); 
?>        
<div class="inventory"> 
    <div class ="cars"> 
    <table> 
     <?php   
      if (mysqli_num_rows($result) > 0) 
      { 
      while($row = mysqli_fetch_assoc($result)) 
      { 
       $RegNo = $row["regNo"]; 
       $CarMake = $row["carMake"]; 
       $CarModel = $row["carModel"]; 
     ?> 
     <tr> 
      <input type="hidden" name="regNo" value="<?php echo $row['regNo'] ?>"> 
      <input type="hidden" name="carMake" value="<?php echo $row['carMake'] ?>"> 
      <input type="hidden" name="carModel" value="<?php echo $row['carModel'] ?>"> 
      <td><?php echo $row["regNo"] ?></td> 
      <td><?php echo $row["carMake"] ?></td> 
      <td><?php echo $row["carModel"] ?></td> 
      <td><button onclick="addRowToTable('<?php echo $RegNo ?>','<?php echo $CarMake ?>','<?php echo $CarModel ?>')">+</button></td> 
     </tr> 
     <script type ="text/javascript"> 
      function addRowToTable(regNo, carMake, carModel) { 
       var table = document.getElementById("newhireTBL"); 
       var row = table.insertRow(0); 
       var cell1 = row.insertCell(0); 
       var cell2 = row.insertCell(1); 
       var cell3 = row.insertCell(2); 
       cell1.innerHTML = regNo; 
       cell2.innerHTML = carMake; 
       cell3.innterHTML = carModel; 
       } 
     </script> 
     <?php 
      } 
      } 
     ?> 
    </table> 
    </div> 
</div>    

    <div class="order"> 
    <div class ="newhire"> 
    <table id="newhireTBL"> 
    </table> 

    <form action = "addNewHire.php" method = "post"> 
      <input type="hidden" name="regNo" value="<?php echo $row['regNo'] ?>"> 
      <input type="hidden" name="carMake" value="<?php echo $row['carMake'] ?>"> 
      <input type="hidden" name="carModel" value="<?php echo $row['carModel'] ?>"> 

      <input id="submit" type="submit" name="submit" value="Submit"> 
    </form> 
    </div> 
</div> 

addNewHire.php

<?php session_start(); ?> 
<?php require_once("connectToSQL.php"); ?> 
<?php 
echo "<pre>";var_dump($_POST);echo "</pre>";die; 
if (isset($_POST['submit'])){ 
    $RegNo = $_POST['regNo']; 
    $CarMake = $_POST['carMake']; 
    $CarModel = $_POST['carModel']; 

    $_SESSION["regNo"] = $RegNo; 
    $_SESSION["carMake"] = $CarMake; 
    $_SESSION["carModel"] = $CarModel; 

$sql = "INSERT INTO newHire(regNo, carMake, carModel) 
     VALUES('$RegNo', '$CarMake', '$CarModel')"; 

if (mysqli_query($connection, $sql)) { 
header("location:order_success.php"); 
echo "Order has been made"; 
} else { 
header("location:order_fail"); 
echo "Order fail"; 

} 
} 
?> 

} 
?> 

測試表明這種

array(4) { 
["regNo"]=> 
string(0) "" 
["carMake"]=> 
string(0) "" 
["carModel"]=> 
string(0) "" 
["submit"]=> 
string(6) "Submit" 
} 
+0

你'如果-condition'之前,文件'addNewHire.php'中,加上'回聲 「

";var_dump($_POST);echo "
」;模具;'並檢查輸出。使用var_dump可以顯示內容,使用'pre'-tag可以更可讀地格式化輸出。用'死'你只需停止程序。 – AMartinNo1

+0

嗨@ AMartinNo1這是輸出,我是否認爲沒有什麼被添加到變量? '陣列(4){ [ 「REGNO」] => 串(0) 「」 [ 「carMake」] => 串(0) 「」 [ 「carModel」] => 串(0)「 「 [」submit「] => string(6)」Submit「 }' – SnowmanPusha

+0

@ShowmanPusha好吧,數組鍵存在。至於該內容應該已經被傳送。試試馬克建議的。 – AMartinNo1

回答

0

的形式和表標籤具有無效的嵌套,這樣可以防止被提交的表單數據。嘗試將「newhireTBL」表的結束標記移動到表的開始標記下方(表單外部),以便表和表單標記不重疊。

如:

<table...> 
</table> 

<form...> 
</form> 

編輯:這裏是一些僞數據,可以幫助您解決問題一個基本的例子。祝你好運!

<?php 
    $rows = array(
     array(
      "regNo" => "abc123", 
      "carMake" => "toyota", 
      "carModel" => "camry", 
     ), 
     array(
      "regNo" => "def456", 
      "carMake" => "ford", 
      "carModel" => "laser", 
     ),   
    ); 
?>  

<?php foreach ($rows as $row): ?> 
    <?php echo $row["regNo"] ?> 
    <?php echo $row["carMake"] ?> 
    <?php echo $row["carModel"] ?> 
    <button onclick="addRow('<?php echo $row["regNo"] ?>','<?php echo $row["carMake"] ?>','<?php echo $row["carModel"] ?>')">+</button> 
    <br/> 
<?php endforeach ?> 

<form action = "addNewHire.php" method = "post"> 
    <input id=regNo type="text" name="regNo" value=""> 
    <input id=carMake type="text" name="carMake" value=""> 
    <input id=carModel type="text" name="carModel" value=""> 
    <input id="submit" type="submit" name="submit" value="Submit"> 
</form> 

<script type ="text/javascript"> 
    function addRow(regNo, carMake, carModel) { 
     document.getElementById('regNo').value = regNo; 
     document.getElementById('carMake').value = carMake; 
     document.getElementById('carModel').value = carModel; 
    } 
</script> 
+0

我猜你的意思是'

'和'
',這阻止了動態表的工作,當我點擊提交時,我得到了相同的輸出如上。 – SnowmanPusha

+0

對不起,我錯過了腳本中表格的引用。回答編輯建議修改表格和表格嵌套,而不是刪除表格。 – Mark

+0

不幸的是,似乎沒有任何東西進入數組。 – SnowmanPusha