Guest kilbad Posted August 20, 2006 Share Posted August 20, 2006 This is part of my address book script that updates existing db entries. Is there someway this part of my script be consolidated somehow? Thanks in advance![code] $id=mysql_escape_string($_POST["id"]); $letter=$_POST["letter"]; $first=mysql_escape_string($_POST["first"]); $last=mysql_escape_string($_POST["last"]); $address1=mysql_escape_string($_POST["address1"]); $address2=mysql_escape_string($_POST["address2"]); $city=mysql_escape_string($_POST["city"]); $state=mysql_escape_string($_POST["state"]); $zip=mysql_escape_string($_POST["zip"]); $country=mysql_escape_string($_POST["country"]); $phone1=mysql_escape_string($_POST["phone1"]); $phone2=mysql_escape_string($_POST["phone2"]); $email1=mysql_escape_string($_POST["email1"]); $email2=mysql_escape_string($_POST["email2"]); $website1=mysql_escape_string($_POST["website1"]); $website2=mysql_escape_string($_POST["website2"]); $notes=mysql_escape_string($_POST["notes"]); $aim_sn=mysql_escape_string($_POST["aim_sn"]); mysql_query("UPDATE contactbook SET first = '$first' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET last = '$last' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET address1 = '$address1' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET address2 = '$address2' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET city = '$city' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET state = '$state' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET zip = '$zip' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET country = '$country' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET phone1 = '$phone1' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET phone2 = '$phone2' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET email1 = '$email1' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET email2 = '$email2' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET website1 = '$website1' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET website2 = '$website2' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET notes = '$notes' WHERE id = '$id'"); mysql_query("UPDATE contactbook SET aim_sn= '$aim_sn' WHERE id = '$id'");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18119-can-this-part-of-my-script-be-consolidated-somehow/ Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 [code]foreach ($_POST as $key => $value) { if ($key != "Submit") { $$key = mysql_real_escape_string($value); }}$query = " UPDATE contactbook SET first = '$first', last = '$last', address1 = '$address1', address2 = '$address2', city = '$city', state = '$state', zip = '$zip', country = '$country', phone1 = '$phone1', phone2 = '$phone2', email1 = '$email1', email2 = '$email2', website1 = '$website1', website2 = '$website2', notes = '$notes', aim_sn= '$aim_sn', WHERE id = '$id'";$result = mysql_query($query) or die(mysql_error());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18119-can-this-part-of-my-script-be-consolidated-somehow/#findComment-77658 Share on other sites More sharing options...
wildteen88 Posted August 20, 2006 Share Posted August 20, 2006 As you are doing the same thing over and over on each line of code. Use a foreach loop which will do all the hard work for you:[code=php:0]// get the id from the POST array. This will be used latter$id = mysql_real_escape_string($_POST['id']);// remove the id from $_POST array. As we dont need it any more.unset($_POST['id']);// now the fun starts! Put PHP into auto mode!foreach($_POST as $key => $value){ $value = mysql_real_escape_string($value); mysql_query("UPDATE contactbook SET " . $key . " = '" . $value . "' WHERE id='" . $id . "'");}[/code]Thats it!NOTE in order for this code to work the form fields will have to have the same name as the name of the fields in your contactbook table. Quote Link to comment https://forums.phpfreaks.com/topic/18119-can-this-part-of-my-script-be-consolidated-somehow/#findComment-77666 Share on other sites More sharing options...
Guest kilbad Posted August 20, 2006 Share Posted August 20, 2006 This was just what I needed! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/18119-can-this-part-of-my-script-be-consolidated-somehow/#findComment-77725 Share on other sites More sharing options...
ToonMariner Posted August 20, 2006 Share Posted August 20, 2006 too many queries in there!!!![code]<?php// get the id from the POST array. This will be used latter$id = mysql_real_escape_string($_POST['id']);// remove the id from $_POST array. As we dont need it any more.unset($_POST['id']);$str = NULL;// now the fun starts! Put PHP into auto mode!foreach($_POST as $key => $value){ $value = !empty($value) ? "'" . mysql_real_escape_string($value) . "'" : "NULL"; $ftr .= is_null($str) ? "`" . $key . "` = " . $value: ", `" . $key . "`= " . $value; }$qry = mysql_query("UPDATE contactbook SET " . $str . " WHERE id='" . $id . "'");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18119-can-this-part-of-my-script-be-consolidated-somehow/#findComment-77730 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.