cry of war Posted November 12, 2007 Share Posted November 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
Pavel11 Posted November 12, 2007 Share Posted November 12, 2007 $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); Quote Link to comment Share on other sites More sharing options...
cry of war Posted November 13, 2007 Author Share Posted November 13, 2007 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"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.