PHP Form Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

PHP Form Help

Post by nigma »

Hey, I have a form that submits to itself (<form method="POST">) and depending on which form fields were filled out I would like to update data in a mysql database.

I am trying to concieve a way to check to see if the form has been submitted before so that I can start updating the database based on which form fields were filled out, but I am not having any success with this.

Also, since there are no required form fields on the page I cannot do like

Code: Select all

<?php
if (!empty($_POST['requiredField']))
{
// Update DB based on form fields with data
}
?>
I guess I could make the form submit to a different page, but before doing that I would like to know how to do it using this method.

Thanks for any and all help / advice provided.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

so let me see if i understand you right, this would be like a page for a user to update his/her profile.......so the user would fill out any fields he wanted to change, then you would have to check the db to see if anything changed, and if it did then you could write it to the db, i think that what you mean you want to do, or maybe just if the field is set write the field info to the db like;

Code: Select all

if(isset($_POST['email'])){
//db crap
}
elseif(isset($_POST['password'])){
//db crap
}
and so on and so fourth, please be a bit more specific as to what it is that you want to do.....thanks....
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Yes, actually, I could do it that way (which I had overlooked) but I was trying to simplify the whole proccess by doing

Code: Select all

<?php
if (!empty($fname)) { $queries[] = "fname='$fname'"; }		// Q1
	if (!empty($lname)) { $queries[] = "lname='$lname'"; }		// Q2
	if (!empty($email)) { $queries[] = "email='$email'"; }		// Q3
	if (!empty($webpage)) { $queries[] = "webpage='$webpage'"; }		// Q4
	if (!empty($imcontact)) { $queries[] = "imcontact='$imcontact'"; }	// Q5
	if (!empty($location)) { $queries[] = "location='$location'"; }		// Q6
	if (!empty($interests)) { $queries[] = "interest='$interests'"; }		// Q7

	$qID = 1;
	foreach ($queries as $query)
	{
		@mysql_query("update users set $query where user='$user'") or error("DB Query #$qID Failed");
		$qID++;
	}
	header("Location: ?p=tArea");

?>
That way I wouldn't have to manually type out all the queries (although there are other ways to do it). Thanks for the help.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

what if:

Code: Select all

$fields=array(
 "fname"=>"fname=",
 "lname"=> "lname=",
 "email"=> "email="
)
$qs=array();
foreach($_POST as $name=>$value)
  if(isset($fields[$name]))
     $qs[]=$fields[$name]."'".str_replace("'","\''",$value)."'";
if(count($qs))
  mysql_query("update users set ".implode(",",$qs)." where user='".$_POST['user']."'");
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Lol, yea that works. I was actually going to delete that reply and substitute it with the solution I was using (I might reconsider and use wierdans :)).

Anyway, basically I just decided to first setup the array:

Code: Select all

if (!empty($fname)) { $queries[] = "fname='$fname'"; }	
if (!empty($lname)) { $queries[] = "lname='$lname'"; }	
if (!empty($email)) { $queries[] = "email='$email'"; }
if (!empty($webpage)) { $queries[] = "webpage='$webpage'"; }	
if (!empty($imcontact)) { $queries[] = "imcontact='$imcontact'"; }	
if (!empty($location)) { $queries[] = "location='$location'"; }	
if (!empty($interests)) { $queries[] = "interests='$interests'"; }
Then check to see if any of the previous conditions were true, and if they were, then update the db:

Code: Select all

if (isset($queries))
{
	$qID = 1;
	foreach ($queries as $query)
	{
		@mysql_query("update profiles set $query where user='$user'") or error("DB Query #$qID Failed");
		$qID++;
	}
}
Anyway, thanks a bunch weirdan and dull1554, I will experiment with your ideas and try to simplify this proccess even more. Definetely apreciated the fast and accurate responses givin by both of you.
Post Reply