2014-03-28 60 views
1

大家好,我的標題告訴我正在做一個動態輸入,但我有錯誤,我要哭了哈哈。說真的,我很難做到。我想添加動態輸入並在我的數據庫中添加值。這裏是我的代碼:動態輸入並將其添加到數據庫

<?php 
// Connect to the DB 
$link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link)); 

// store in the DB 
if(!empty($_POST['ok'])) { 
    // first delete the records marked for deletion. Why? Because we don't want to process them in the code below 
    if(!empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) { 
     // you can optimize below into a single query, but let's keep it simple and clear for now: 
     foreach($_POST['delete_ids'] as $id) { 
      $sql = "DELETE FROM recherche WHERE id=$id"; 
      $link->query($sql); 
     } 
    } 



    // adding new recherche 
    if(!empty($_POST['name'])) { 
    // ($i = 0; $i < count($_POST['name']); $i++) 
     { 
      $sql = "INSERT INTO recherche (name) VALUES ".$_POST['name'][$i]; 
      $link->query($sql); 
     } 
    } 
} 

// select existing recherche here 
$sql="SELECT * FROM recherche ORDER BY id"; 
$result = $link->query($sql); 
?> 

<html> 
<head> 
    <title>Simple example of dynamically adding rows with jQuery</title> 
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script> 
</head> 

<body> 

<div style="width:90%;margin:auto;"> 
    <h1>Simple example of dynamically adding rows with jQuery</h1> 

    <form method="post"> 
    <div id="itemRows"> 

    Item name: <input type="text" name="add_name" /> <input onclick="addRow(this.form);" type="button" value="Add row" /> (This row will not be saved unless you click on "Add row" first) 

    <?php 
    // let's assume you have the product data from the DB in variable called $recherche 
    while($product = mysqli_fetch_array($result)): ?> 
     <p id="oldRow<?=$product['id']?>"> Item name: <input type="text" name="name<?=$product['id']?>" value="<?=$product['name']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$product['id']?>"> Mark to delete</p> 
    <?php endwhile;?> 

    </div> 

    <p><input type="submit" name="ok" value="Save Changes"></p> 
    </form> 
</div> 

<script type="text/javascript"> 
var rowNum = 0; 
function addRow(frm) { 
    rowNum ++; 
    var row = '<p id="rowNum'+rowNum+'">Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>'; 
    jQuery('#itemRows').append(row); 
    frm.add_qty.value = ''; 
    frm.add_name.value = ''; 
} 

function removeRow(rnum) { 
    jQuery('#rowNum'+rnum).remove(); 
} 
</script> 
</body> 
</html> 

我在環路已經的問題,我得到這個錯誤:

警告:mysqli_fetch_array()預計參數1被mysqli_result,在C中給出 布爾:\瓦帕\ WWW \測試\動態外形fields.html.php上 線51這是在前面的代碼行51

while($product = mysqli_fetch_array($result)): ?> 
     <p id="oldRow<?=$product['id']?>"> Item name: <input type="text" name="name<?=$product['id']?>" value="<?=$product['name']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$product['id']?>"> Mark to delete</p> 
    <?php endwhile;?> 

感謝SUP港口!

編輯

這裏你們幫助我的代碼的新的部分:但沒有當我向數據庫提交情況。我檢查了我的數據庫的phpmyadmin。

<?php 
// Connect to the DB 
$link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link)); 

// store in the DB 
if(!empty($_POST['ok'])) { 
    // first delete the records marked for deletion. Why? Because we don't want to process them in the code below 
    if(!empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) { 

     foreach($_POST['delete_ids'] as $id) { 
      $sql = "DELETE FROM recherche WHERE id=$id"; 
      $link->query($sql); 
     } 
    } 

    // adding new recherche 
    if(!empty($_POST['name'])) { 
    foreach($_POST['name'] as $name) 
    { 
     //escape special characters from inputed "name" to prevent SQL injection. 
     $sql = "INSERT INTO recherche (name) VALUES ".mysqli_real_escape_string($link,$name); 
     $link->query($sql); 
    } 
} 
} 

// select existing recherche here 
$sql="SELECT * FROM recherche ORDER BY id"; 
$result = $link->query($sql); 
?> 

<html> 
<head> 
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script> 
</head> 

<body> 

<div style="width:90%;margin:auto;"> 


    <form method="post"> 
    <div id="itemRows"> 

    Item name: <input type="text" name="add_name" /> <input onclick="addRow(this.form);" type="button" value="Add row" /> (This row will not be saved unless you click on "Add row" first) 
    <?php 
if($result!=false && mysqli_num_rows($result)>0){ 
    while($product = mysqli_fetch_array($result)): ?> 
     <p id="oldRow<?=$product['id']?>"> Item name: <input type="text" name="name<?=$product['id']?>" value="<?=$product['name']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$product['id']?>"> Mark to delete</p> 
    <?php endwhile; 

} 
?> 
    </div> 

    <p><input type="submit" name="ok" value="Save Changes"></p> 
    </form> 
</div> 

<script type="text/javascript"> 
var rowNum = 0; 
function addRow(frm) { 
    rowNum ++; 
    var row = '<p id="rowNum'+rowNum+'">Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>'; 
    jQuery('#itemRows').append(row); 
    frm.add_qty.value = ''; 
    frm.add_name.value = ''; 
} 

function removeRow(rnum) { 
    jQuery('#rowNum'+rnum).remove(); 
} 
</script> 
</body> 
</html> 

在這裏finfinaly是偉大的人在這個論壇上的結果!

隨意編輯或做任何你想要的代碼!

<?php 
// Connect to the DB 
$link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link)); 

// store in the DB 
if(!empty($_POST['ok'])) { 
    // first delete the records marked for deletion. Why? Because we don't want to process them in the code below 
    if(!empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) { 

     foreach($_POST['delete_ids'] as $id) { 
      $sql = "DELETE FROM recherche WHERE id=$id"; 
      $link->query($sql); 
     } 
    } 

    // adding new recherche 
    if(!empty($_POST['name'])) { 
    foreach($_POST['name'] as $name) 
    { 
     //escape special characters from inputed "name" to prevent SQL injection. 

     $sql = "INSERT INTO recherche (name) VALUES ('".mysqli_real_escape_string($link,$name)."')"; 
     $link->query($sql); 
    } 
} 
} 

// select existing recherche here 
$sql="SELECT * FROM recherche ORDER BY id"; 
$result = $link->query($sql); 
?> 

<html> 
<head> 
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script> 
</head> 

<body> 

<div style="width:90%;margin:auto;"> 


    <form method="post"> 
    <div id="itemRows"> 

    Item name: <input type="text" name="add_name" /> <input onclick="addRow(this.form);" type="button" value="Add row" /> (This row will not be saved unless you click on "Add row" first) 
    <?php 
if($result!=false && mysqli_num_rows($result)>0){ 
    while($product = mysqli_fetch_array($result)): ?> 
     <p id="oldRow<?=$product['id']?>"> Item name: <input type="text" name="name<?=$product['id']?>" value="<?=$product['name']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$product['id']?>"> Mark to delete</p> 
    <?php endwhile; 

} 
?> 
    </div> 

    <p><input type="submit" name="ok" value="Save Changes"></p> 
    </form> 
</div> 

<script type="text/javascript"> 
var rowNum = 0; 
function addRow(frm) { 
    rowNum ++; 
    var row = '<p id="rowNum'+rowNum+'">Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>'; 
    jQuery('#itemRows').append(row); 
    frm.add_qty.value = ''; 
    frm.add_name.value = ''; 
} 

function removeRow(rnum) { 
    jQuery('#rowNum'+rnum).remove(); 
} 
</script> 
</body> 
</html> 

回答

1

永遠不要假設你在$result數據。測試它處理它。

<?php 
if($result!=false && mysqli_num_rows($result)>0){ 
    while($product = mysqli_fetch_array($result)): ?> 
     <p id="oldRow<?=$product['id']?>"> Item name: <input type="text" name="name<?=$product['id']?>" value="<?=$product['name']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$product['id']?>"> Mark to delete</p> 
    <?php endwhile; 

} 
?> 

編輯

FIX這部分代碼,以插入形式的多行提交

// adding new recherche 
if(!empty($_POST['name'])) { 
    foreach($_POST['name'] as $name) 
    { 
     //escape special characters from inputed "name" to prevent SQL injection. 
     $sql = "INSERT INTO recherche (name) VALUES ('".mysqli_real_escape_string($link,$name)."')"; 
     $link->query($sql); 
    } 
} 
+0

感謝這個部分它的工作,但最後我真的不知道如何做到這一點,代碼\t //添加新的搜索記錄 \t if(!empty($ _ POST ['name'])){ \t // \t($ i = 0; $ i query($ sql); \t \t} \t} \t' – TheBaconManWithouBacon

+0

@TheBaconManWithouBacon,如果我沒有錯那麼,你想添加多行數據庫表單提交,對嗎? –

+0

是的當我提交他在輸入(動態)輸入的客戶端的所有數據將被添加到數據庫。例如,如果他想在添加5行時添加5個名稱,並且當他在數據庫中完成時,保存1-名 - 第二個名... – TheBaconManWithouBacon

相關問題