Home > Archive > PHP DB > March 2006 > Re: [PHP-DB] if() and else() help needed
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: [PHP-DB] if() and else() help needed
|
|
|
| JeRRy wrote:
> Hi,
>
> I'll admit it, this is damned messy. But I want to learn from the list in how to sort it out. Than for future refrence I will know...
>
> Now I am running 2 different queries/statements here completely seperate. I have made the "nickname" field in the database UNIQUE. So than when I have this sort of query setup no matter what their will only be one "nickname" entry for that user. So
when people update their profile a new "nickname" is not inserted but it is updated. But I want it all in one PHP call. How do I do this?
How are you differentiating between the statements?
eg they are logged in, or you check with a database query or .... ?
You could do something like this:
// check for $_GET['tipid'] - if it's there, we're updating our tips.
// if it's not, then we're adding new tips.
if (isset($_GET['tipid'])) {
$query = "UPDATE " ......
$success_message = "Your tips have been updated";
} else {
$query = "INSERT INTO " .....
$success_message = "Your tips have been saved";
}
$result = mysql_query($query);
if ($result) {
echo $success_message;
} else {
echo "Problem!!<br/>";
}
--
Postgresql & php tutorials
http://www.designmagick.com/
| |
|
| Hi,
Yes the user is logged in. The system knows who is tipping by their nickname and unique id system.
So if I put the update query in the else statement would this be the easy fix?
I use something like a field called tipped in the table, so if they have tipped it goes to y and if never tipped it's blank. I think it should show that in the query. Cant remember, I did the scripts 2 or 3 years ago. Just want to tidy it up.
J
Chris <dmagick@gmail.com> wrote:
JeRRy wrote:
> Hi,
>
> I'll admit it, this is damned messy. But I want to learn from the list in how to sort it out. Than for future refrence I will know...
>
> Now I am running 2 different queries/statements here completely seperate. I have made the "nickname" field in the database UNIQUE. So than when I have this sort of query setup no matter what their will only be one "nickname" entry for that user. So when
people update their profile a new "nickname" is not inserted but it is updated. But I want it all in one PHP call. How do I do this?
How are you differentiating between the statements?
eg they are logged in, or you check with a database query or .... ?
You could do something like this:
// check for $_GET['tipid'] - if it's there, we're updating our tips.
// if it's not, then we're adding new tips.
if (isset($_GET['tipid'])) {
$query = "UPDATE " ......
$success_message = "Your tips have been updated";
} else {
$query = "INSERT INTO " .....
$success_message = "Your tips have been saved";
}
$result = mysql_query($query);
if ($result) {
echo $success_message;
} else {
echo "Problem!!
";
}
--
Postgresql & php tutorials
http://www.designmagick.com/
| |
|
| JeRRy wrote:
> Hi,
>
> Yes the user is logged in. The system knows who is tipping by their
> nickname and unique id system.
>
> So if I put the update query in the else statement would this be the
> easy fix?
Don't know - I was offering a suggestion only.
Without the full code we can't really help much (I doubt anyone will
want the full code to help you clean it up, sorry).
--
Postgresql & php tutorials
http://www.designmagick.com/
| |
|
|
Well the way I have it over 2 PHP calls works, as 'nickname' is a UNIQUE key entry in the db. So multiple entries are not an issue. So if it can't insert, as it will try and fail it will update.
But if the insert works I am guessing it's going to do an update straight after it for the same result, So it double entries I persume. But what worry would this be? None I am guessing, it's just doing a double entry as long as the data is not lost.
And only occours on entering tips for the first time.
J
Chris <dmagick@gmail.com> wrote:
JeRRy wrote:
> Hi,
>
> Yes the user is logged in. The system knows who is tipping by their
> nickname and unique id system.
>
> So if I put the update query in the else statement would this be the
> easy fix?
Don't know - I was offering a suggestion only.
Without the full code we can't really help much (I doubt anyone will
want the full code to help you clean it up, sorry).
--
Postgresql & php tutorials
http://www.designmagick.com/
| |
| David Robley 2006-03-29, 3:57 am |
| JeRRy wrote:
>
>
> Well the way I have it over 2 PHP calls works, as 'nickname' is a UNIQUE
> key entry in the db. So multiple entries are not an issue. So if it
> can't insert, as it will try and fail it will update.
>
> But if the insert works I am guessing it's going to do an update
> straight after it for the same result, So it double entries I persume.
> But what worry would this be? None I am guessing, it's just doing a
> double entry as long as the data is not lost. And only occours on
> entering tips for the first time.
>
> J
>
>
> Chris <dmagick@gmail.com> wrote:
> JeRRy wrote:
>
> Don't know - I was offering a suggestion only.
>
> Without the full code we can't really help much (I doubt anyone will
> want the full code to help you clean it up, sorry).
>
You don't specify which database you are using, so I'll just make the
generic suggestion that you look into "INSERT ... ON DUPLICATE KEY UPDATE",
"INSERT IGNORE" and "REPLACE" as possible tools to solve your problem, if
your DB supports these statements.
Cheers
--
David Robley
NUMBER CRUNCHING: Jumping on a Computer.
Today is Pungenday, the 15th day of Discord in the YOLD 3172
|
|
|
|
|