Forum Moderators: coopster

Message Too Old, No Replies

Inserting && displaying form data (MySql DB) on same php-site

         

redbanditos2005

12:00 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



One PHP-Newbie question:

I need to create MySql based web form to store and display data at the same php-page. How can I manage that the form data will not be inserted again (as new record) into the DB in case of using the RELOAD-browser function?
I'm tired of trying it out :-<

Here is the code sample:


<html>
<head><title>impressions.php</title><head>
<body>
<?php
if ($valider) {
$db = mysql_connect("localhost", "user", "passwd");
mysql_select_db("db",$db);
$continu=1;
if ($nom == "")
{
print("Your name is needed!<br>");
$continu=0;
}
[...]
if ($continu == 1)
{
$date=date("Y-m-d");
$sql="INSERT INTO impression (name, email, impression, date, comments) VALUES ('$nom', '$email', '$impression', '$date', '$comments')";
mysql_query($sql, $db);
print("Thank you for giving me your impressions!<br>") ;
}
else
{
print("<a href=impressions.php>back</a>");
}
}
?>
[...]
<form method="post" action=<?php echo $PHP_SELF?>>
Your name : <input type="text" name="nom"><br>
Your e-mail : <input type="email" name="email"><br>
Did you <input type="radio" name="impression" value="aime"> lode
<input type="radio" name="impression" value="pasaime"> or not this Website.
<br>
Your comments : <textarea name="comments"></textarea>
<br><br>
<input type="submit" name="valider" value="Send">
</form>
[...]
</body>
</html>

Many thanx for the answers...

Robber

1:23 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



Hi redbanditos2005,

Welcome to WW. I think you would need to perform an sql SELECT befored the INSERT to make sure the record is not already in the database. Perhaps using the email address to check for uniqueness.

You could use the mysql_num_rows function to test how may rows the select query returned.

if(mysql_num_rows($queryResult) == 0){
// no results returned so that email isnt in so we can do the insert
}

Or you could use the SQL count() aggregate to do effectively the same thing.

Cheers

redbanditos2005

1:59 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



That was the thing ;-)!

Lots of thx Robber...

I'm sitting here working on my diploma and sometimes I dont have a clue to do the basic things :-((
Bad for me, then I'm just like absolutely beginner on PHP and I have only something like 20-30 days to programm my web frontend for generating HTML/PDF reportfiles from <FORM> data.

SHOULD I BE IN PANIC?
Nooooo - because there is lot of great help out there ;-)!

Greeeeetz and stay tuned until the next question ;-)

redbanditos2005

Robber

3:14 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



No problem,

I you're on a steep learning curve you will find some great help round here, but also check out the documentation on sites like php.net and mysql.com - across these resources you'll find just about everything you need.