dongpo5264 2014-05-30 09:53
浏览 20
已采纳

如何从Java插入$ _POST

I am trying to insert a $_POST array into a mysql database. The foreach works but not all values are inserted on the same row as you can see on the image below.

Img1

This is the foreach that i use to insert the array into the database:

if($_POST)
{

    // con->insert is my mysql function to insert the values
    foreach ($_POST as $key => $value) {
        if($con->insert('test',$key, $value)==true)
        {
            echo 'Het werkt!!!';
        }
    }

}

EDIT This is the con function

public function insert($table,$row,$value) {
        $query = mysqli_query($this->connect, "INSERT INTO `$table` (`$row`) VALUES ('$value')");

        if ($query == true)
        {
            return true;
        }
        else{
            return false;
        }
    }

I want to collect the variables from the post and then i want to put them in the database on the same row of the table.

How can i get the values on the same row? Thank in advance

  • 写回答

3条回答 默认 最新

  • ds11111111111111111 2014-05-30 10:03
    关注

    The values get into different rows because you are doing one insert per variable.

    You probably want to collect all variables from the $_POST and add them to the database in a single insert.

    Firstly, change your insert clause like this:

    public function insert_multi($table, $items) {
        $query = mysqli_query($this->connect,
            "INSERT INTO `$table` (`firstKeyName`, `secondKeyName`) ".
            "VALUES ('".$items['firstKeyName']."', '".$items['secondKeyName']."')");
    
            if ($query == true)
            {
                return true;
            }
            else{
                return false;
            }
        }
    

    Then call it only once with all params:

    $con->insert_multi('test', $_POST);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看