Jump to content

Update array


cry of war

Recommended Posts

I am trying to update my a table on my DB using update and an array i have the following funtion that i found on the net but i dont understand how to use it could anyone help?

 

function mysql_update_array($table, $data, $id_field, $id_value) {
foreach ($data as $field=>$value) {
	$fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));
}
$field_list = join(',', $fields);

$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, intval($id_value));

return $query;
}

 

or if you have any other ways that are easy to do then the above i would love to hear about them

Link to comment
Share on other sites

$table - table's name

$data - array('field1' => 'value1',

      'filed2' => 'value2',

      etc.), where field* is a field name and value* is new value to this field.

$id_field - name of your field in table which you use for identify user (ex. userName, of userId or any field with unique value).

$id_value - value this field.

 

This function only create sql query and return it.

 

For example, you update user's name in database:

$data = array('name' => 'Nick');

$query = mysql_update_array('user_table', $data, 'id', 3);

$res = mysql_query($query);

Link to comment
Share on other sites

ok this is my code and i thought i understood it from what pavel said but i try using it and it doesnt update.....

 

function mysql_update_array($table, $data, $id_field, $id_value) {
$id_field="Name";
$id_value="$EditAlchemy";
$value= array($Ingredientname,$Ingredientpic,$Ingredientdis,$Ingredientweight,$Ingredientlevel1,$Effect1,$Alignment1,$Ingredientstrenth1,$Ingredientduration1,$Ingredientlevel2,$Effect2,$Alignment2,$Ingredientstrenth2,$Ingredientduration2,$Ingredientlevel3,$Effect3,$Alignment3,$Ingredientstrenth3,$Ingredientduration3,$Ingredientlevel4,$Effect4,$Alignment4,$Ingredientstrenth4,$Ingredientduration4,$Ingredientlevel5,$Effect5,$Alignment5,$Ingredientstrenth5,$Ingredientduration5,$Ingredientlevel6,$Effect6,$Alignment6,$Ingredientstrenth6,$Ingredientduration6,$Ingredientlevel7,$Effect7,$Alignment7,$Ingredientstrenth7,$Ingredientduration7,$Ingredientlevel8,$Effect8,$Alignment8,$Ingredientstrenth8,$Ingredientduration8,$Ingredientlevel9,$Effect9,$Alignment9,$Ingredientstrenth9,$Ingredientduration9,$Ingredientlevel10,$Effect10,$Alignment10,$Ingredientstrenth10,$Ingredientduration10);
$field= array('Ingredientname','Ingredientpic','Ingredientdis','Ingredientweight','Ingredientlevel1','Effect1','Alignment1','Ingredientstrenth1','Ingredientduration1','Ingredientlevel2','Effect2','Alignment2','Ingredientstrenth2','Ingredientduration2','Ingredientlevel3','Effect3','Alignment3','Ingredientstrenth3','Ingredientduration3','Ingredientlevel4','Effect4','Alignment4','Ingredientstrenth4','Ingredientduration4','Ingredientlevel5','Effect5','Alignment5','Ingredientduration5','Ingredientlevel6','Effect6','Alignment6','Ingredientstrenth6','Ingredientduration6','Ingredientlevel7','Effect7','Alignment7','Ingredientstrenth7','Ingredientduration7','Ingredientlevel8','Effect8','Alignment8','Ingredientstrenth8','Ingredientduration8','Ingredientlevel9','Effect9','Alignment9','Ingredientstrenth9','Ingredientduration9','Ingredientlevel10','Effect10','Alignment10','Ingredientstrenth10','Ingredientduration10');
foreach ($data as $field=>$value) {
	$fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));
}
$field_list = join(',', $fields);

$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, intval($id_value));
$resulttest=mysql_query("$query");
if (!is_resource($resulttest)){
                echo "Table was not updated or there was an error";
die;
}
echo "table has been updated";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.