if() and else() help needed

if() and else() help needed

am 28.03.2006 02:55:09 von jusa_98

--0-877361029-1143507309=:35005
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

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?

Here is the code:

if ($REQUEST_METHOD == "POST") {
$usr = "" . $mysql_user . "";
$pwd = "" . $mysql_password . "";
$db = "" . $mysql_database . "";
$host = "" . $mysql_hostname . "";
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);

// NOTE that form fields automatically become variables
$sql = "update round" . $round_number . " SET
game1='$game1',game2='$game2',game3='$game3',game4='$game4', game5='$game5',game6='$game6',game7='$game7',game8='$game8', misc='y'
WHERE username='$username'";
mysql_query("$sql");
echo("Your tips have been
saved

$game1
$game2
$game3
$game4
$game5
$game6
$game7
$game8
.");
}
else {echo("Ooops, something bad happened! Please try again shortly.");}
?>
if ($REQUEST_METHOD == "POST") {
$usr = "" . $mysql_user . "";
$pwd = "" . $mysql_password . "";
$db = "" . $mysql_database . "";
$host = "" . $mysql_hostname . "";
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);

// NOTE that form fields automatically become variables
$sql = "insert into round" . $round_number . " SET
game1='$game1',game2='$game2',game3='$game3',game4='$game4', game5='$game5',game6='$game6',game7='$game7',game8='$game8', misc='y',username='$username'";
mysql_query("$sql");
echo(" ");
}
else {echo("Ooops, something bad happened! Please try again shortly.");}
?>

As you can see 2 PHP calls. What I want is to have it all in one PHP call. On the update one I deleted the echo statement text. I want the insert one to have the text that appears and the update text output to say "your tips have been updated..."

Of course if the DB is down or something output an error to try again like already in the else() statements.

The help would be great.

Thanks!

J

--0-877361029-1143507309=:35005--

Re: if() and else() help needed

am 28.03.2006 03:06:16 von Chris

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/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: if() and else() help needed

am 28.03.2006 03:32:32 von jusa_98

--0-5778363-1143509552=:88384
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

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 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/


--0-5778363-1143509552=:88384--

Re: if() and else() help needed

am 28.03.2006 03:50:36 von Chris

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/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: if() and else() help needed

am 29.03.2006 02:42:45 von jusa_98

--0-1311904679-1143592965=:1574
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit



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 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/


--0-1311904679-1143592965=:1574--

Re: if() and else() help needed

am 29.03.2006 08:22:10 von robleyd

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 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).
>

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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php