Page 1 of 1
PHP Form Help
Posted: Mon Dec 15, 2003 8:38 pm
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.
Posted: Mon Dec 15, 2003 8:50 pm
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....
Posted: Mon Dec 15, 2003 9:41 pm
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.
Posted: Mon Dec 15, 2003 9:52 pm
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']."'");
Posted: Tue Dec 16, 2003 8:54 am
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.