从PHP数组简单的更新MySQL表 [英] Simple UPDATE MySQl table from php array

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

问题描述

我想放在一起,做如下的功能:


  1. 从表格检索JSON EN codeD字符串

  2. 德code中的字符串到PHP数组

  3. 循环通过生成的PHP数组来获取数组中的每个部分的值,这样我可以更新MySQL表

下面是我的函数code迄今:

 公共职能saveTestimonials(){
    $ existing_testimonials_update = $这个 - >后(数据);    $ update_array = json_de code($ existing_testimonials_update);    的foreach($ update_array为$关键=> $推荐){
         的foreach($证明书为$关键=> $值){
            //回声$键= $值\\ N的;        }
    }    $ DB =装载机:: DB();
    $ SQL =UPDATE证言集名称= VAR,内容= VAR WHERE ID = VAR;
    $ DB-GT&;查询($的SQL);    $这个 - >重定向('/仪表板/推荐/');}

下面是存储在$ update_array变量阵列

 阵列

[0] => stdClass的对象
    (
        [ID] => 1
        [名] =>李四先生,经理,ABC有限公司
        [内容] =>我的内容1。
    )[1] => stdClass的对象
    (
        [ID] => 2
        [名] =>乔·史密斯先生,经理,ABC产业
        [内容] =>我的内容2。
    )[2] => stdClass的对象
    (
        [ID] => 3
        [名] =>迈克·史密斯先生,经理,ABC产业
        [内容] =>我的内容3。
    )[3] => stdClass的对象
    (
        [ID] => 4
        [名] =>李四女士,经理,ABCD有限公司
        [内容] =>我的内容4。
    ))

我有步骤1和2工作正常,但是我被困在第3步。

我仍然在学习PHP和有时语法斗争。
我试图来解决这个一出来我自己,并在其上​​花了几个小时,但似乎无法推测这一个。

任何帮助非常多AP preciated。


解决方案

 的foreach($ update_array为$关键=> $推荐){
    $名称= mysql_real_escape_string($ testimonials->名);
    $内容= mysql_real_escape_string($ testimonials->内容);
    的$ id = INTVAL($ testimonials-和SEQ ID);    $ SQL =UPDATE证言集名称='$名称',内容='$内容'WHERE ID = $编号;
    $结果= mysql_query($的SQL);
    如果($结果=== FALSE){
        死亡(mysql_error());
    }
}

I am trying to put together a function that does the following:

  1. retrieve a JSON encoded string from a form
  2. decode the string to a php array
  3. loop through the generated php array to get the values for each part of the array so that I can update a MySql table

Here is my function code so far:

public function saveTestimonials() {


    $existing_testimonials_update = $this->post('data');

    $update_array = json_decode($existing_testimonials_update);

    foreach ($update_array as $key => $testimonials) {
         foreach($testimonials as $key => $value) {
            //echo "$key = $value\n";

        }
    }

    $db = Loader::db();
    $sql = "UPDATE testimonials SET name=var, content=var WHERE id=var";
    $db->query($sql);

    $this->redirect('/dashboard/testimonials/');

}

Here is the array stored in the $update_array variable:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [name] => Mr. John Doe, Manager, ABC Ltd
        [content] => my content 1.
    )

[1] => stdClass Object
    (
        [id] => 2
        [name] => Mr. Joe Smith, Manager, ABC Industries
        [content] => my content 2.
    )

[2] => stdClass Object
    (
        [id] => 3
        [name] => Mr. Mike Smith, Manager, ABC Industries
        [content] => my content 3.
    )

[3] => stdClass Object
    (
        [id] => 4
        [name] => Ms. Jane Doe, Manager, ABCD Ltd
        [content] => my content 4.
    )

)

I have got steps 1 and 2 working fine, however I am stuck on step 3.

I still learning PHP and struggle with syntax at times. I have tried to work this one out on my own and have spent several hours on it, but just can't seem to figure this one out.

Any assistance is very much appreciated.

解决方案

foreach ($update_array as $key => $testimonials) {
    $name = mysql_real_escape_string($testimonials->name);
    $content = mysql_real_escape_string($testimonials->content);
    $id = intval($testimonials->id);

    $sql = "UPDATE testimonials SET name='$name', content='$content' WHERE id=$id";
    $result = mysql_query($sql);
    if ($result === FALSE) {
        die(mysql_error());
    }
}

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

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