用数组更新表列,PHP [英] updating table columns with arrays, php

查看:85
本文介绍了用数组更新表列,PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从输入字段中获取了三个数组(这些字段处于while循环中。)

i have three arrays fetched from input fields(the fields are in a while loop).

$act = $_POST["act"];
$apid = $_POST["relate"];
$reason = $_POST['reason'];

数组$ act,$ apid,$ reason包含要在每一列上更新的值。例如:

the arrays $act,$apid,$reason contain values to be updated on each column. for example:

$act=(1,2,3,4,5,6);
$apid=(303,304,305,306,307,308);
$reason=(delivery,contract,cargo,offloading,uploading,seal); 

在数据库端,我有表 ActivityProduct 具有3列,即; APID Actual 原因 APID 是表的主键。

on the database side, i have the table ActivityProduct with 3 columns, namely; APID,Actual,Reason. APID is the primary key of the table.

我想更新实际和原因,并使用数组中的值。这是我的代码(无效),用于更新:

I'd like to update the columns Actual and Reason with values from the arrays. Here's my code(that does not work), for the update:

$values=array('reas'=>$reason,'actu'=>$act,'ids'=>$apid);
foreach ($values as $key)
{ 
$update_row =mysql_query("UPDATE ActivityProduct SET Actual= '$key['actu']}',Reason='{$key['reas']}' WHERE APID='{$key['ids']}'  " )
}

这是我得到的错误:

Notice: Undefined index: actu in C:\xampp\htdocs\PD\validate\save-actual.php on line 65

Notice: Undefined index: reas in C:\xampp\htdocs\PD\validate\save-actual.php on line 65

Notice: Undefined index: ids in C:\xampp\htdocs\PD\validate\save-actual.php on line 65

任何人都知道我在做什么错吗?还是如何使其工作?.....我对数组并不熟悉。

Any idea what i'm doing wrong? or how to make it work?.....i'm not that familiar with arrays.

推荐答案

它们的$ key变量不是数组,请尝试循环打印它,您将看到。
并检查文档和熟悉的foreach循环示例

They $key variable is not array, try to print it in loop and you will see. And check documentation and examples of foreach loop to be familiar with it.

无论如何,您都不需要此循环,请删除循环并尝试一下

You don't need this loop anyway, remove loop and try this

$update_row =mysql_query("UPDATE ActivityProduct SET Actual= {$values['actu']},Reason='{$values['reas']}' WHERE APID='{$values['ids']}'  " )

如果运行此foreach,则更新将执行3次,因为数组中有3个项目。

If you will run this foreach you update will be executed 3 times because you have 3 items in array.

这篇关于用数组更新表列,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆