php - 在 myphp 中添加项目

标签 php jquery mysql

当我尝试在 myphp.ini 中添加多行时遇到问题。我有一个函数,当调用它时,它会向网站添加一个文本框行。问题是,当我尝试将行输入 myphp 时,它只显示一行,而不是添加的所有行。我正在使用表单发布到 myphp。我相信问题是文本框的名称,当添加行,名称没有改变,所以 myphp 只插入一行而不是多行。如果有人对解决此问题的方法有任何想法,我们将不胜感激。这是我的 jquery 代码和 html 代码。

function displayResult() {
    var table=document.getElementById("customers");
    var row=table.insertRow(-1);
    row.innerHTML= '<td><input type ="text" name= "item" placeholder= "Item Description" > </td>'+
    '<td><input type ="text" name= "link" placeholder= "www.yourstorehere.com" > </td>'+
    '<td><input type ="text" name= "price" placeholder= "50.00" > </td>'+
    '<td><input type ="text" name= "comment" placeholder= "I would like it in blue.." > </td>'
}

还有 php...

 // if form was submitted
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        // validate inputs
        if (empty($_POST["item"]))
        {
            apologize("You must provid an item.");
        }
        else if (empty($_POST["link"]))
        {
            apologize("You must provide a link or a place to purchase the item.");
        }
        else if (empty($_POST["price"]))
        {
            apologize("You must provide a price.");
        }

        // try to register user
        $results = query("INSERT INTO List (id,name,purpose,item,link,price,comment) VALUES (?,?,?,?,?,?,?)", $_SESSION["id"],$_POST["name"],$_POST["purpose"],$_POST["item"],$_POST["link"],$_POST["price"],$_POST["comment"]);

最佳答案

试试这个

你可以创建动态名称数组 页面发布时取回

保留一个每行递增的整数 认为 $index 最初 = 0 添加新行时增加它。 并在名称中使用这个 $index 例如,您可以给出类似的名称


    name='form[".$index."]["item"]'
    name='form[".$index."]["link"]'
    name='form[".$index."]["price"]'
    name='form[".$index."]["comment"]'

每行索引值增加 1

发布页面后,您将得到如下所示的数组


    Array(
    [0]=>Array
          (
             ['item']=>"some value"
             ['link']=>"some value"
             ['price']=>"some value"
             ['comment']=>"some value"
          )

    [1]=>Array
          (
             ['item']=>"some other value"
             ['link']=>"some other value"
             ['price']=>"some other value"
             ['comment']=>"some other value"
          )
    )

最后你只需要旋转循环即可获取所有 REQUEST 值

如果有什么地方说错了,请见谅

关于php - 在 myphp 中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417483/

相关文章:

php - php 会在每次请求后自动关闭 TCP 连接吗?

jQuery Deferred - 获取链接 ajax 调用的结果

mysql - 我如何优化这个联合查询

php - 如何在netbeans中生成.phar

php - 关闭 cakePHP 中的通知

php - 在 csv_from_result 方法中为我们将 mysql 时间戳转换为 codeigniter 查询对象中的日期

PHP:安装后找不到类 'MongoClient' (Linux)

javascript - jQuery 计时问题 (ajax)

javascript - 输入中的 Chrome 现金值(value)

MySQL:如果在其中一个子记录中找到每个搜索条件,则选择记录