簡體   English   中英

來自php數組的簡單UPDATE MySQl表

[英]Simple UPDATE MySQl table from php array

我正在嘗試將執行以下操作的函數放在一起:

  1. 從表單檢索JSON編碼的字符串
  2. 將字符串解碼為php數組
  3. 遍歷生成的php數組以獲取數組每個部分的值,以便我可以更新MySql表

到目前為止,這是我的功能代碼:

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並有時會在語法上苦苦掙扎。 我試圖自己解決這個問題,並花了幾個小時,但似乎無法弄清楚。

非常感謝您的協助。

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

我正在使用這樣的東西。 可能對您有幫助!

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!"; }
}
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);
}

希望這段代碼可以對您有所幫助。 我想不要一一嘗試更新。 考慮性能。

這些是您要在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)訪問變量。

您正在從json_decode()獲取對象。 如果將true作為第二個參數傳遞給json_decode(),則會得到一個關聯數組。

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

嘗試這樣的事情

 $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

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM