从php数组中简单更新MySQl表

时间:2011-10-25 03:13:45

标签: php mysql arrays

我正在尝试组合一个执行以下操作的函数:

  1. 从表单
  2. 中检索JSON编码的字符串
  3. 将字符串解码为php数组
  4. 循环生成的php数组以获取数组每个部分的值,以便我可以更新MySql表
  5. 到目前为止,这是我的功能代码:

    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/');
    
    }
    

    这是存储在$ update_array变量中的数组:

    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.
        )
    
    )
    

    我已经让第1步和第2步工作正常,但我仍然坚持第3步。

    我仍然在学习PHP并且有时会遇到语法困难。 我试图独自完成这个工作,并花了几个小时,但似乎无法想象这一个。

    非常感谢任何帮助。

6 个答案:

答案 0 :(得分:5)

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());
    }
}

答案 1 :(得分:3)

我正在使用这样的东西。可能对你有帮助!

function updateDbRecord($db, $table, $carry, $carryUrl) {   
    mysql_select_db($db) or die("Could not select database. " . mysql_error());
    $resultInsert = mysql_query("SHOW COLUMNS FROM " . $table . " WHERE Field NOT IN ('id')");
    $fieldnames=array();
      if (mysql_num_rows($resultInsert) > 0) {
        while ($row = mysql_fetch_array($resultInsert)) {
            $fieldnames[] = $row['Field'];
            $array = array_intersect_key( $_POST, array_flip($fieldnames) );
        }
      }
      foreach ($array as $key => $value) {
        $value = mysql_real_escape_string($value);
        $value = "'$value'";
        $updates[] = "$key = $value";
      }
      $implodeArray = implode(', ', $updates);
      $sql = sprintf("UPDATE %s SET %s WHERE id='%s'", $table, $implodeArray, $_POST['id']);
      mysql_query($sql);
      if ($carry == 'yes') {
        redirect($carryUrl.'?id='.$_REQUEST['id'].'&'.$table);
      } else { echo "Done!"; }
}

答案 2 :(得分:0)

这些是你在update_array中处理的对象,所以你应该能够像这样访问它们:

$update_array = json_decode($existing_testimonials_update);

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

    }
}

或者,更简单地说,您可以使用箭头运算符($ testimonials-> name)来访问变量。

答案 3 :(得分:0)

您正在从json_decode()获取一个对象。如果将第二个参数传递给json_decode(),则会得到一个关联数组。

http://php.net/manual/de/function.json-decode.php

答案 4 :(得分:0)

尝试这样的事情

 $db = Loader::db();

foreach ($update_array as $key => $testimonials) {
         foreach($testimonials as $testimonial) {

             // escape data to avoid sql injection
             $id = mysql_escape_string($testimonial->id);
             $name = mysql_escape_string($testimonial->name);
             $content = mysql_escape_string($testimonial->content);

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

             // TODO check for database error here

        }
    }

答案 5 :(得分:0)

function update($table_name, $myarray, $my_wheres) {
    $sql = "Update`".$table_name.
    "` SET ";
    $i = 0;
    foreach($myarray as $key => $value) {
        $sql.= $key." = '".$value."'";
        if ($i < count($myarray) - 1) {
            $sql.= " , ";
        }
        $i++;
    }
    if (count($my_wheres) > 0) {
        $sql.= " WHERE ";
        $i = 0;
        foreach($my_wheres as $key => $value) {
            $sql.= $key.
            " = ".$value;
            if ($i < count($my_wheres) - 1) {
                $sql.= " AND ";
            }
            $i++;
        }
    }

    return mysqli_query($sql);
}

希望,这段代码可以帮到你。我认为不要尝试逐个更新。想想表现。