Jump to content


Can this part of my script be consolidated somehow?


  • Please log in to reply
4 replies to this topic

#1 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 20 August 2006 - 05:29 PM

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!

    
           $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'");


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 05:34 PM

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

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 August 2006 - 05:51 PM

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:

// 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 . "'");
}

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.

#4 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 20 August 2006 - 08:25 PM

This was just what I needed!  Thank you so much!

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 20 August 2006 - 08:37 PM

too many queries in there!!!!


<?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 . "'");

?>

follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users