【问题标题】:Dynamic input and add it into a database动态输入并将其添加到数据库中
【发布时间】:2014-03-28 17:23:13
【问题描述】:

大家好,我的标题表明我正在进行动态输入,但我有错误,我要哭了哈哈。说真的,我很难做到。我想在我的数据库中添加动态输入并增加价值。这是我的代码:

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

【问题讨论】:

    标签: php jquery html sql database


    【解决方案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);
        }
    } 
    

    【讨论】:

    • 感谢这部分它可以工作,但最后一部分我真的不知道该怎么做就是添加这个代码code // adding new recherche if(!empty($_POST['name'])) { // ( $i = 0; $i &lt; count($_POST['name']); $i++) { $sql = "INSERT INTO recherche (name) VALUES ".$_POST['name'][$i]; $link-&gt;query($sql); } }
    • @TheBaconManWithouBacon,如果我没记错的话,您想在表单提交时向数据库添加多行,对吧??
    • 是的,当我提交客户的所有数据时,他在输入中输入(动态)将被添加到数据库中。示例如果他想在添加 5 行中添加 5 个名称,并且当他在数据库中完成保存 1- 第一个名称 2- 第二个名称 ...
    • 我编辑了我的代码,正如你所看到的,我忘记了一些愚蠢的事情。
    • 几乎!我想知道为什么当我保存数据时什么都没有发生。我的数据库中没有添加任何内容。
    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2017-08-31
    • 2016-02-25
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多