Question:
php mysql update database from list of array?
.................
2009-11-23 05:43:37 UTC
hi,

i am using mysql db.

i have table called setting
in that table i have setting_name, setting_title, setting_description, setting_value.

i like to update title description and value but not the name as i will always be same..

i need to put information in array then pass on to database.

i cant think of how i can do this

public function update( $setting ){
global $database, $db;



$name = $setting['name'];
$title = $setting['title'];
$desc = $setting['desc'];
$value = $setting['value'];

$sql = " UPDATE ".self::$table_name;
$sql.= " SET title='{$title}', description='$desc', value='{$value}'";
$sql.= " WHERE _name = '{$name}'";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}

how can i write a loop which insert data into database.


please help me. i have check few site but i cant find anything which i need.

thanks in advance
Three answers:
treemonster
2009-11-24 02:28:53 UTC
you can use foreach loop to do this. The only thing you need to make sure that key names from form array must be same as mysql table column names



for example

if form variable are

$_POST['form']['id']

$_POST['form']['title']

$_POST['form']['descr']



Then mysql table will have at least three columns named id, title and descr



next do foreach loop something like this



$sql = "UPDATE tablename";

foreach($_POST['form'] as $key => $data){



$sql .= "SET $key='".$data."'";



}

this code is not complete you need to add append concatenate $sql with WHERE statement
anonymous
2016-10-18 17:08:21 UTC
of direction no longer: you replace a record with the final get entry to... :-) you may desire to make a separate table: table acquaintances field username (key) field pal then, for each get entry to interior the pal's record, upload a record (ideally "if no longer already there") Later, to get the record of acquaintances, merely decide on * from table the place username=username
anonymous
2009-11-23 05:51:44 UTC
You simply wrote a loop - nonsense...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...