2014-09-19 94 views
1

詢問之前,這裏是我的格式的圖片: enter image description herePHP不插入/後空值,但仍插入/後填充值

正如你所看到的,我有1個表格2個表,每個表,有六個輸入。

在「表阿拉特」和「表Bahan」的輸入代碼是這樣的:

<form method='post' action='p_input.php'> 
    <table> 
    <tr> 
     <td>1.</td> 
     <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'> 
     <input type='hidden' name='jenis[]' value='alat' autocomplete='off'></td> 
    </tr> 
    </table> 

    <table> 
    <tr> 
     <td>1.</td> 
     <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'> 
     <input type='hidden' name='jenis[]' value='bahan' autocomplete='off'></td> 
    </tr> 
    <table width='100%' align='center' style='margin-top:0;'> 
    <tr> 
    <td><input name='reset' type='reset' id='bersihkan' value='Reset'> 
    <input name='submit' type='submit' id='ajukan' value='Send'></td> 
    </tr> 
    </table> 
    </table> 
    </form> 

在這裏所不同的是<input type='hidden' name='jenis[]' value='alat' autocomplete='off'>,對於「表阿拉特」的值是「ALAT」的「表Bahan 「價值是'巴漢'(我在這裏沒有麻煩,只是告訴你)。

這是我p_input.php腳本:

<?php 
     error_reporting(0); 
     session_start(); 

     if(isset($_REQUEST['submit'])) { 
     include "../conf/koneksi.php"; 
     $count = count($_POST['nama']); 
     $jurusan = $_POST['jurusan']; 
     $lab = $_POST['lab']; 
     $materi = $_POST['materi']; 
     $mahasiswa= $_POST['mahasiswa']; 
     for($i = 0; $i < $count; $i++){ 
      $nama = mysql_real_escape_string($_POST['nama'][$i]); 
      $merk = mysql_real_escape_string($_POST['merk'][$i]); 
      $kemasan = mysql_real_escape_string($_POST['kemasan'][$i]); 
      $mhs = mysql_real_escape_string($_POST['mhs'][$i]); 
      $jml = mysql_real_escape_string($_POST['jml'][$i]); 
      $hps = mysql_real_escape_string($_POST['hps'][$i]); 
      $jenis = mysql_real_escape_string($_POST['jenis'][$i]); 
      $sql2= "ALTER TABLE tb_usulan AUTO_INCREMENT = 1"; 
      mysql_query($sql2); 
      $sql=mysql_query("INSERT INTO tb_usulan (nama,merk,kemasan,kebutuhan,jml_kebutuhan,hps,jenis,materi_praktikum) 
      VALUES 
      ('$nama', '$merk', '$kemasan', '$mhs', '$jml', '$hps','$jenis','$materi')") or die(mysql_error()); 

     } 
     } 
     ?> 

我的問題是:如果我只在「TABLE阿拉特」填寫輸入和保持輸入「TABLE BAHAN」空,然後當我點擊提交,如果我只填寫「TABLE BAHAN」中的輸入並將「TABLE ALAT」中的輸入保留爲空,那麼只有在「TABLE BAHAN」中輸入時纔會插入/發佈,我只想在表alat中插入/發佈到數據庫,反之亦然到數據庫。

更新問題:這只是一個表單和一個提交按鈕,並且同時插入到同一個表中。

+0

一個很好的方法來解決這個問題,就是要利用AJAX,因爲使用php/html你只能提交每頁一個表單,用ajax你可以保存到數據庫而不需要刷新頁面。 – 2014-09-19 04:05:25

+0

ahh對不起,請參閱編輯。 我只爲這個頁面使用一種形式。 :) – Shitprogrammers 2014-09-19 04:08:30

+0

您可以通過更改表單中的表名來實現此目的。 – 2014-09-19 04:11:04

回答

1

記住我添加了輸入字段。因此在應用解決方案之前將其刪除。因爲您的原始表單中已經有了輸入字段。

<table> 
    <tr> 
     <td>1.</td> 
     <td><input type='text' name='nama1' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='merk1' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='kemasan1' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='mhs1' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='jml1' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='hps1' style='width:100%;' autocomplete='off'> 
     <input type='hidden' name='jenis1' value='alat' autocomplete='off'></td> 
    </tr> 
    </table> 

    <table> 
    <tr> 
     <td>1.</td> 
     <td><input type='text' name='nama2' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='merk2' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='kemasan2' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='mhs2' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='jml2' style='width:100%;' autocomplete='off'></td> 
     <td><input type='text' name='hps2' style='width:100%;' autocomplete='off'> 
     <input type='hidden' name='jenis2' value='bahan' autocomplete='off'></td> 
    </tr> 
    </table> 


    <input type='submit' name="submit"> 
</form> 
</body> 

比你在哪裏運行查詢,只改變的那部分。

if(!empty($_POST['name1'])) 
    { 
     //Run First query 
    } 
    if(!empty($_POST['name2'])) 
    { 
     //run your second query 
    } 

該代碼將執行的操作將檢查該值。如果值不爲空,比它會執行其他查詢否則無法執行查詢

和第二是當您使用的代碼,你不會有任何陣列,所以你需要刪除$ count變量。和你的foreach代碼循環。

第三件事。如果(isset($ _ REQUEST ['submit'])){ include「../conf/koneksi.php」;你需要設置

之後的值。

對於每個費爾德像下面爲我設置幾個

$名1 = $ _ POST [ '名稱1']; $ merk1 = $ _ post ['merk1'];

對兩個表的每個值都做這件事。

以及東西您需要更改查詢中的值部分,例如您的值將變爲$ name1,以此類推對第一個查詢和第二個查詢$ name2等等。

希望我解釋一下這一步。並在執行步驟之前。如果你不明白,在你開始使用它之前問我。

謝謝

+0

我使用$ count和for循環,因爲我的表格行是動態的,所以如果我點擊「+」(就像您在圖片中看到的那樣),它會添加一個新行。 – Shitprogrammers 2014-09-19 07:34:52

+1

@MuhammadFahmy如果你使用count,因爲這個沒關係。解決方案將保持不變。只是一些步驟將改變。你需要使用計數每個加號讓$ count1和$ count2。和耶而不是刪除你原來的代碼中使用的for循環。因爲你需要計數循環的意思,但是包括在你的兩個循環中如果staement。如果你不知道我在說什麼。讓我知道我會更新我的答案。謝謝 – 2014-09-19 07:50:52

+0

是的,我想我明白你的意思,所以我必須讓每個查詢循環,對不對? – Shitprogrammers 2014-09-19 07:53:45

2

只是做你的for循環內檢查。

for($i = 0; $i < $count; $i++) 
{ 
    //skip if nama is empty 
    if($_POST["nama"][$i] == ""){continue;} 

    $nama = mysql_real_escape_string($_POST['nama'][$i]); 
    $merk = mysql_real_escape_string($_POST['merk'][$i]); 
    $kemasan = mysql_real_escape_string($_POST['kemasan'][$i]); 
    $mhs = mysql_real_escape_string($_POST['mhs'][$i]); 
    $jml = mysql_real_escape_string($_POST['jml'][$i]); 
    $hps = mysql_real_escape_string($_POST['hps'][$i]); 
    $jenis = mysql_real_escape_string($_POST['jenis'][$i]); 
    $sql2= "ALTER TABLE tb_usulan AUTO_INCREMENT = 1"; 
    mysql_query($sql2); 
    $sql=mysql_query("INSERT INTO tb_usulan (nama,merk,kemasan,kebutuhan,jml_kebutuhan,hps,jenis,materi_praktikum) 
    VALUES ('$nama', '$merk', '$kemasan', '$mhs', '$jml', '$hps','$jenis','$materi')") or die(mysql_error()); 
} 
+0

再次閱讀該問題。 – 2014-09-19 06:07:18

+0

這個問題很好。如果這個答案有什麼問題,那麼這已經是一個設計問題。如果OP只想POST 1行,他不應該共享表格,除非他在玩AJAX。 – 2014-09-19 06:15:22

+0

感謝您幫助我的朋友,但我的問題已經解決了,謝謝您花時間幫助我。 – Shitprogrammers 2014-09-19 09:17:28

1

一個簡單的解決辦法是將表分離的形式分離是這樣的:

<form name=formOne> 
    <table><!-- Alat etc. --> 
    </table> 
    <input type=submit> 
</form> 

<form name=formTwo> 
    <table><!-- Behan etc. --> 
    </table> 
    <input type=submit> 
</form> 

則僅用於至極提交被推表將得到submited!

編輯 使它只與一個提交按鈕(客戶端)一起工作。 添加下面的jQuery:

var lastForm; 
$("form").click(function(){ 
    lastForm = this; 
}); 

$("input[type=submit]").click(function(){ 
    $(lastForm).submit(); 
}); 

然後添加在表單元素外的提交按鈕。 但最好的解決方案是檢查字段serverside。 (無論如何都應該這樣做,因爲你不能真正相信最終用戶)。

但這已經在其他答案中提出!

+0

提交按鈕只有一個,所以如果當我點擊提交按鈕時,表格bahan被填充並且表格alat爲空,它將自動僅提交表格bahan並忽略表格alat。 – Shitprogrammers 2014-09-19 07:56:12

+0

您需要添加一個提交按鈕,如上例所示! – 2014-09-19 08:02:25

+0

但是,它會降低效率,因爲我的工作場所只需要一個sumbit按鈕,無論如何它都插入到同一張表中。 – Shitprogrammers 2014-09-19 08:25:10