PDA

View Full Version : One submission per person please!


losse
04-12-2006, 10:21 PM
Hi there
Have a look at this submission form. I got it from ZEND located here:
http://www.zend.com/php/beginners/php101-8.php

When I set this up, I noticed that when you hit SUBMIT it brings up

"New record inserted with ID"

If you hit refresh or visit the page again, it automatically adds an additional entry into the database and I only want people to submit 1 entry...

How can this be done?

Thanks

<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";

// get form input
// check to make sure it's all there
// escape input values for greater safety
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
$animal = empty($_POST['animal']) ? die ("ERROR: Enter an animal") : mysql_escape_string($_POST['animal']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();

// close connection
mysql_close($connection);
}
?>

boeing747fp
04-12-2006, 11:13 PM
you could add an `ip_address` field to the database and have it be UNIQUE, or check for a submission by that ip address before adding new ones.

so something like this

<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";

// get form input
// check to make sure it's all there
// escape input values for greater safety
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
$animal = empty($_POST['animal']) ? die ("ERROR: Enter an animal") : mysql_escape_string($_POST['animal']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query to check for a current form from this IP Address
$check = mysql_query("SELECT * FROM `symbols` WHERE `ip_address` = '{$_SERVER['REMOTE_ADDR']}'") or die(mysql_error());
//If there is no form already submitted by this IP Address, do submission
if(mysql_num_rows($check) <1){
mysql_query("INSERT INTO `symbols` (`ip_address`, `country`, `animal`) VALUES ('{$_SERVER['REMOTE_ADDR']}', '$country', '$animal')") or die ("Error in query: $query. ".mysql_error());
}else{
//There is already a submission from this IP Address... Do nothing further
exit('You have already submitted this form!');
}
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();

// close connection
mysql_close($connection);
}
?>

losse
04-12-2006, 11:18 PM
Is IP address risky in case Joe and his neighbor try to submit?? I take it they would have the same IP no??

Yeah... this seems a little more difficult to manage than I'd like to... I think I'lljust delete duplicate entries...

Thanks for all your help

boeing747fp
04-12-2006, 11:31 PM
you could set a cookie and have it detect whether your browser has submitted already or not.

<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";

// get form input
// check to make sure it's all there
// escape input values for greater safety
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
$animal = empty($_POST['animal']) ? die ("ERROR: Enter an animal") : mysql_escape_string($_POST['animal']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

if(isset($_COOKIE['alreadysubmitted']) && $_COOKIE['alreadysubmitted'] == "yes"){
//You have already submitted! Exit
exit('You have already submitted this form!');
}else{
mysql_query("INSERT INTO `symbols` (`country`, `animal`) VALUES ('$country', '$animal')") or die ("Error in query: $query. ".mysql_error());
setcookie("alreadysubmitted","yes",time()+60*60*24*120);
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
}
// close connection
mysql_close($connection);
}
?>

losse
04-13-2006, 12:45 AM
Nice!!!

So you've inputed this bit of code

if(isset($_COOKIE['alreadysubmitted']) && $_COOKIE['alreadysubmitted'] == "yes"){
//You have already submitted! Exit
exit('You have already submitted this form!');
}else{
mysql_query("INSERT INTO `symbols` (`country`, `animal`) VALUES ('$country', '$animal')") or die ("Error in query: $query. ".mysql_error());
setcookie("alreadysubmitted","yes",time()+60*60*24*120);
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
}

What would be the name of the cookie in this case??? Just to make sure I can delete it when I am testing...

losse
04-13-2006, 12:57 AM
By the way, what's this error all about:

Warning: Cannot modify header information - headers already sent by (output started at xxx/xxx/xxx/xxx/) in xxx/xxx/xxx/xxx/xxx/xxx on line 133
New record inserted with ID

At line 133 this is the snippet of code that exists:

setcookie('alreadysubmitted','yes',time()+60*60*24*120);

FaR_ElitE
04-13-2006, 01:46 AM
add this to the top of your script (below "<?php")

ob_start();

losse
04-13-2006, 03:21 AM
Care to explain what that does?

losse
04-13-2006, 03:25 AM
That ob_start(); didn't work... Still points to an error on the line where this code snippet is:

setcookie('alreadysubmitted','yes',time()+60*60*24*120);

By the way

I failed to mention that the error had this

register.php:4

Warning: Cannot modify header information - headers already sent by (output started at xxx/xxx/xxx/register.php:4) in xxx/xxx/xxx/register.php on line 134
New record inserted with ID

register.php is the name of the file containing this code...

boeing747fp
04-13-2006, 04:07 AM
try
@setcookie

losse
04-13-2006, 04:10 AM
I don't get the error anymore but it's like that cookie portion of the code didn't exist... it still enters an additional submision every time I refresh or revisit that page...

boeing747fp
04-13-2006, 04:15 AM
ok well take the @ off.... if you're getting the "Headers already sent" error it's because there's some sort of HTML output above the setcookie, which shouldnt be happening.

boeing747fp
04-13-2006, 04:16 AM
can i see your entire page code?

losse
04-13-2006, 04:19 AM
Here's what I have above that php code that you posted further up... Think there's something there screwing things up?

<?php

if (!isset($_POST['submit'])) {
// form not submitted
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table width="387" border="1">
<tr>
<td><span class="style10">First Name:</span></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><span class="style10">Last Name:</span></td>
<td><input type="text" name="last"></td>
</tr>
<tr>
<td><span class="style10">Address:</span></td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td><span class="style10">Address 2:</span></td>
<td><input type="text" name="address2"></td>
</tr>
<tr>
<td><span class="style10">City:</span></td>
<td><input type="text" name="city"></td>
</tr>
<tr>
<td><span class="style10">Province:</span></td>
<td><input type="text" name="province"></td>
</tr>
<tr>
<td><span class="style10">Postal Code:</span></td>
<td><input type="text" name="zip"></td>
</tr>
<tr>
<td><span class="style10">Telephone Number:</span></td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td><span class="style10">Email: </span></td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"> </td>
</tr>
</table>
</form>

boeing747fp
04-13-2006, 04:24 AM
try setting it up backwards from the way you have it... like this

<?php
if(isset($_POST['submit'])){
// form submitted
// set server access variables
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";

// get form input
// check to make sure it's all there
// escape input values for greater safety
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
$animal = empty($_POST['animal']) ? die ("ERROR: Enter an animal") : mysql_escape_string($_POST['animal']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

if(isset($_COOKIE['alreadysubmitted']) && $_COOKIE['alreadysubmitted'] == "yes"){
//You have already submitted! Exit
exit('You have already submitted this form!');
}else{
mysql_query("INSERT INTO `symbols` (`country`, `animal`) VALUES ('$country', '$animal')") or die ("Error in query: ".mysql_error());
setcookie("alreadysubmitted","yes",time()+60*60*24*120);
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
}
// close connection
mysql_close($connection);
}else{
// form not submitted
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table width="387" border="1">
<tr>
<td><span class="style10">First Name:</span></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><span class="style10">Last Name:</span></td>
<td><input type="text" name="last"></td>
</tr>
<tr>
<td><span class="style10">Address:</span></td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td><span class="style10">Address 2:</span></td>
<td><input type="text" name="address2"></td>
</tr>
<tr>
<td><span class="style10">City:</span></td>
<td><input type="text" name="city"></td>
</tr>
<tr>
<td><span class="style10">Province:</span></td>
<td><input type="text" name="province"></td>
</tr>
<tr>
<td><span class="style10">Postal Code:</span></td>
<td><input type="text" name="zip"></td>
</tr>
<tr>
<td><span class="style10">Telephone Number:</span></td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td><span class="style10">Email: </span></td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"> </td>
</tr>
</table>
</form>
<?php
}
?>

felgall
04-13-2006, 05:58 AM
If you use cookies then they can still sign up a second time if they delete the cookie.

hadoken
04-13-2006, 11:54 AM
Hello,
I was thinking about another technique to avoid the refresh things.
You can unset the $_POST variable and make a redirection after you fill the database.
<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";

// get form input
// check to make sure it's all there
// escape input values for greater safety
$country = empty($_POST['country']) ? die ("ERROR: Enter a country") : mysql_escape_string($_POST['country']);
$animal = empty($_POST['animal']) ? die ("ERROR: Enter an animal") : mysql_escape_string($_POST['animal']);

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "INSERT INTO symbols (country, animal) VALUES ('$country', '$animal')";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// close connection
mysql_close($connection);

// unset your POST
unset($_POST['country']);
unset($_POST['animal']);

// Redirection
header("Location: /success.php");
exit();

}
?>
And on your success.php, you can put a message that everything was ok.

What do you think everybody ?

Cya,
Hadoken

losse
04-13-2006, 11:55 AM
Nope... Still get the same error message...

Would it be because there are 2

if(isset statements?