/    Sign up×
Community /Pin to ProfileBookmark

MsgBox in PHP/XHTML

Hi,

Hope someone can help me here! Sounds like a simple problem; I’m still learning …..

I have a form where a user enters his/her name and city. I have the code to insert a record in the MySQL database for that user; and it works fine. What I’m trying to do now is add the record ONLY if there is no such name and city in the DB. So, my logic is:

1) get name and city from user
2) select from DB for name and city
3) if $result = 0 add record to DB
else “pop up a message saying there is such a name/city, and re-edit form entries”

My question is how to do this pop-up message, so that when this shows up, the user will click “ok”, and go back tp the form where he/she can change the name/city.

Thanks in advance!

DL

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@chazzyOct 24.2006 — so something like this...

[code=php]
$sql = "select count(*) AS cnt from table WHERE zipcode='$zipcode' AND name='$name'";
$result = mysql_query($sql) or die("Unable to issue query: $sql " . mysql_error());
$count = mysql_fetch_array($result);
if($count['cnt'] > 0){
echo "It's already there!";
} else {
//do the insert.
}[/code]
Copy linkTweet thisAlerts:
@pcthugOct 24.2006 — or alternatively...

[code=php]
$sql = "select * from table WHERE zipcode='$zipcode' AND name='$name'";
$result = mysql_query($sql) or die("Unable to issue query: $sql " . mysql_error());
if(mysql_num_rows($result) > 0){
echo "It's already there!";
} else {
//do the insert.
} [/code]
Copy linkTweet thisAlerts:
@datalineauthorOct 24.2006 — Thank you both chazzy and pcthug for the very quick reply!

Instead of <<echo "It's already there!"; >> I want to pop-up a window that displays "It's already there!" and with an "OK" button. The user will then click on "OK" and go back to the form where s/he can then change the name/city.

Can what I want even be done?

Thanks!
Copy linkTweet thisAlerts:
@pcthugOct 24.2006 — That's really more of a Javascript question, but here it is anyway:
[code=php]
$sql = "select * from table WHERE zipcode='$zipcode' AND name='$name'";
$result = mysql_query($sql) or die("Unable to issue query: $sql " . mysql_error());
if(mysql_num_rows($result) > 0){
echo <<<eod
<script type="text/javascript">
alert("It's already there!);
history.back();
</script>
eod;
exit;
} else {
//do the insert.
}[/code]
Copy linkTweet thisAlerts:
@chazzyOct 24.2006 — Note that the query that pcthug uses requires a little more processing time, and more data being sent back to the client (PHP) than just doing a count from SQL, which requires a little more programming work to do.
Copy linkTweet thisAlerts:
@datalineauthorOct 25.2006 — Thanks pcthug for the code; works fine!

chazzy, I can see your point. I'm now using select count(*) instead of select * as you suggested.

Again, thanks to both of you!

DL
Copy linkTweet thisAlerts:
@datalineauthorOct 26.2006 — Hi pcthug and chazzy,

Sorry if I'm bugging you!

I had that code that pcthug provided working; that was last night. Earlier today, I was doing some cleaning; I mean indent the code and everything try to make it look pretty, and it's not working now. I've been pulling my hair; I can't see what I may have deleted / added to screw up the code. I've done a copy&paste from my script. Here is:

echo <<<EOD
<script type="text/javascript">
alert("It's already there!");
history.back();
</script>
EOD;
exit;


If you or anyone else can see what's wrong, it would be much apreciated!

DL
Copy linkTweet thisAlerts:
@chazzyOct 26.2006 — that can't be all of your code..
Copy linkTweet thisAlerts:
@datalineauthorOct 26.2006 — Hi chazzy,

Here is:

mysql_select_db('sports');
$query = "select count(*) as cnt from h_poolies where name = '".$H_R0607_PoolieName."' AND groupteam = '".$H_R0607_GroupName."'";
$result = mysql_query($query);
$num_results = mysql_fetch_array($result);
if ($num_results['cnt'] == 0)
{
$query = "insert into h_poolies values
(NULL, '".$H_R0607_PoolieName."', '".$H_R0607_GroupName."',".$picks[0].",".$picks[1].",".$picks[2].",".$picks[3].",".$picks[4].",".$picks[5].",".$picks[6].",".$picks[7].",".$picks[8].",".$picks[9].",".$picks[10].",".$picks[11].",".$picks[12].",".$picks[13].",".$picks[14].",".$picks[15].",".$picks[16].",".$picks[17].",".$picks[18].",".$picks[19].",".$picks[20].",".$picks[21].",".$picks[22].",".$picks[23].",".$picks[24].",".$picks[25].",".$picks[26].",".$picks[27].",".$picks[28].",".$picks[29].")";
$result = mysql_query($query);
if ($result) echo mysql_affected_rows().' picks inserted into database.';
}
else
{
echo <<<EOD
<script type="text/javascript">
alert("It's already there!");
history.back();
</script>
EOD;
exit;
}


When I comment out the else part in above, everything seems to work fine. Thx!

DL
×

Success!

Help @dataline spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...