动态输入并将其添加到数据库中

时间:2014-03-28 17:23:14

标签: php jquery html sql database

大家好,喜欢我的头衔告诉我做出动态输入,但我有错误,我会哭泣哈哈。说真的,我很难做到。我想添加动态输入并在我的数据库中添加值。这是我的代码:

<?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:\ wamp \ www \ testing \ dynamic-form-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;?>

感谢您的支持!

修改

这里是你们帮助我的代码的新部分:但是当我提交到数据库时没有任何反应。我检查了我的数据库的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>

这里有很棒的人在这个论坛上的结果!

您可以使用此代码随意编辑或制作任何内容!

<?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 个答案:

答案 0 :(得分: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;

}
?>

编辑

在您的代码中修改此部分,插入多行表单提交

// 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);
    }
}