Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将多个数据添加到表中的多个字段中_Php_Mysql - Fatal编程技术网

Php 如何将多个数据添加到表中的多个字段中

Php 如何将多个数据添加到表中的多个字段中,php,mysql,Php,Mysql,我的表格字段如下所示: name | email | status | img_1 | img_2 | img_3 | img_4 | img_5 | img_6 | avail 所以我想使用php通过一个循环添加6个图像。但是我无法编写查询来添加非静态的图像,我的意思是说,如果有人只添加2个图像,或者在条目处可能是1个图像。请帮帮我。这是我的基本问题,我认为这是可行的 $sql = "update product set img_".$dbfield." ='".$filename1."'

我的表格字段如下所示:

name | email | status | img_1 | img_2 | img_3 | img_4 | img_5 | img_6 | avail
所以我想使用php通过一个循环添加6个图像。但是我无法编写查询来添加非静态的图像,我的意思是说,如果有人只添加2个图像,或者在条目处可能是1个图像。请帮帮我。这是我的基本问题,我认为这是可行的

$sql = "update product set img_".$dbfield." ='".$filename1."' where 
         id='".$iiid."'";
where $dbfield is the increment one by one to match the field name in img_1 img_2.

先谢谢你

我不确定我们取得了什么成就,但我想你需要一些人来思考一下

$query = "UPDATE product SET";
$comma = " ";
$whitelist = array(
    'img_1',
    'img_2',
    'img_3',
    'img_4',
    'img_5',
     'img_6',
    // ...etc
);
foreach($_POST as $key => $val) {
    if( ! empty($val) && in_array($key, $whitelist)) {
        $query .= $comma . $key . " = '" . mysql_real_escape_string(trim($val)) . "'";
        $comma = ", ";
    }
}
$sql = mysql_query($query); 

thanx它真的很有用。@DipayanDas如果你发现这个ans真的很有用。别忘了投票或接受ans,它会帮助别人。对不起,我放弃了。