Log in

View Full Version : submission counter



pkcidstudio
07-28-2006, 02:56 PM
i have updated this script hoping i am on the right trail any thoughts?

hey all, i wrote a submission counter this mornign and was wondering if one of you php gurus could proof read it for me and tell em if i am on the right track.

what it is suppose to do is, track how many times users submit a form on my website, and if they have already done it than it will not track them more than once. but it tracks how many people have submitted through the function and a session id, and if it reaches x amount than it will produce a error when they submit saying sorry contest is over. i am very thankfull for all the help. i am still working on this but figured i would toss it out for review by some of you pro's.
so here is the php and sql


<?php
$dbhost = 'localhost';
dbuser = 'session';
$dbpass = '123456';
$dbname = 'session';
?>
<?php
/////////////////////Connect to Database///////////////
function dbconnect () {
global $dbhost, $dbuser, $dbpass, $dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;

$link_id = mysql_pconnect ($dbhost, $dbuser, $dbpass);
if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Could not connect to the host $dbhost.";
return 0;
} else if (empty($dbname) && !mysql_select_db($dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} else {
return $link_id;
}
}
?>
<?php
dbconnect();
session_start();
?>
<?php
/////////////////////////Submission Counter///////////////////////
function submissionCounter(){
$check = mysql_query(ppl_submitted);
if(!session_is_registered('submitted')){
$check("INSERT INTO ppl_submitted (session_id, ip_address, user_agent) <br> VALUES ('".session_id()."', '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_USER_AGENT']}')");
session_register('submitted');
} else {
if(session_is_registered('user_id')){
$check("UPDATE ppl_submitted, total_visit SET member='1' WHERE session_id='".session_id()."'");
}
}
if(session_is_registered('submitted')){
$check("UPDATE ppl_submitted WHERE session_id='".session_id()."'");
}
if(session_is_registered('complete')){
$check("SELECT function(SUM(total_visit)) FROM ppl_submitted")
}
}
?>
<?php
//this would go on the submit form
if (isset($_POST["Submit"]))
{
submissionCounter();

for ($i = 0; $i < 0; $i++)
{
if (!empty($check[$i]))
{
echo "contest is over";
}
}
}
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input name="test" type="text" maxlength="5" value="" size="5" >
<input name="test2" type="text" maxlength="3" value="" size="4" >
<input type="submit" value="Submit" >
</form>
</body>
</html>
<!--sqlsqlsqlsqlsqlsqlsqlsqlsqlsqlsqlsqlsql--
CREATE TABLE ppl_submitted (
session_id varchar(255) NOT NULL default '',
total_visit varchar(150) NOT NULL default '0',
member enum('1','0') default '0',
ip_address varchar(255) NOT NULL default '',
user_agent varchar(255) default NULL,
PRIMARY KEY (session_id),
KEY session_id (session_id)
) TYPE=MyISAM;
-------------------------------------------->
feel free to tell me where i am wrong and help out in any way

blm126
07-28-2006, 07:31 PM
just glancing at it ppl_submitted should be $ppl_submitted

pkcidstudio
07-28-2006, 07:57 PM
just glancing at it ppl_submitted should be $ppl_submitted
why do you say that when i put the "$" on it gives a blank screen

jr_yeo
07-28-2006, 08:08 PM
$check = mysql_query(ppl_submitted);
inside of mysql_query() should be an SQL query :p i couldn't find something like:

$ppl_submitted = "SELECT * FROM tablename";
$check = mysql_query($ppl_submitted);

in ur given php code :D

pkcidstudio
07-28-2006, 08:30 PM
cool keep it coming guys ill post current code i changed pp_submitted to submissionCounter you will see. i did this because i built the sql table around this name not thinking.
here is code


<?php
/////////////////////Connect to Database///////////////
$link = mysql_connect('localhost', 'session', '123456');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
<?php

session_start();
?>
<?php
/////////////////////////Submission Counter///////////////////////
function submissionCounter(){

$check = mysql_query($submissionCounter);
$submissionCounter = "SELECT * FROM tablename";

if(!session_is_registered('submitted')){
$check("INSERT INTO submissionCounter (session_id, ip_address, user_agent) <br> VALUES ('".session_id()."', '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_USER_AGENT']}')");
session_register('submitted');
} else {
if(session_is_registered('user_id')){
$check("UPDATE submissionCounter, total_visit SET member='1' WHERE session_id='".session_id()."'");
}
}
if(session_is_registered('submitted')){
$check("UPDATE submissionCounter WHERE session_id='".session_id()."'");
}
if(session_is_registered('complete')){
$check("SELECT function(SUM(total_visit)) FROM submissionCounter");
}
}
?>
<?php
//this would go on the submit form
if (isset($_POST["Submit"]))
{
submissionCounter();

for ($i = 0; $i < 1; $i++)
{
if (!empty($check[$i]))
{
echo "contest is over";
}
}
}
?>
<html>
<head></head>
<body>
<form action="" method="post">
<input name="test" type="text" maxlength="5" value="" size="5" >
<input name="test2" type="text" maxlength="3" value="" size="4" >
<input type="submit" value="Submit" >
</form>
</body>
</html>
<!--sqlsqlsqlsqlsqlsqlsqlsqlsqlsqlsqlsqlsql--
CREATE TABLE submissionCounter (
session_id varchar(255) NOT NULL default '',
total_visit varchar(150) NOT NULL default '0',
member enum('1','0') default '0',
ip_address varchar(255) NOT NULL default '',
user_agent varchar(255) default NULL,
PRIMARY KEY (session_id),
KEY session_id (session_id)
) TYPE=MyISAM;
-------------------------------------------->

thank you agian for the help

jr_yeo
07-28-2006, 08:35 PM
i think the

$submissionCounter = "SELECT * FROM tablename";
should be b4 the

$check = mysql_query($submissionCounter);
? :p

$submissionCounter should be initialized first b4 querying it :p

pkcidstudio
07-28-2006, 08:42 PM
cool how does this section look?
i want it to display the alert when i reach x amount


<?php
//this would go on the submit form
if (isset($_POST["Submit"]))
{
submissionCounter();
$xamount = "1";
for ($i = 0; $i < $xamount; $i++)
{
if (!empty($check[$i]))
{
echo "contest is over";
}
}
}
?>