dpDesignz
dpDesignz

Reputation: 1959

Add to total of field from Database in PHP

I have a script that I'm using that when the user enters a code I want it to add to the total in the database, however nothing is happening.

This is my code so far:

$err = array();
if (isset($_POST['doSubmit4']) === true ) // Was if ($_POST['access']=='submit')
{
$code = mysql_real_escape_string($_POST['access-key']); // Was $data['access-key']
$result = mysql_query("SELECT `akid`,`key`,`total_access` FROM access_keys WHERE id='$_SESSION[user_id]' AND type='1'") or die (mysql_error()); 
$num = mysql_num_rows($result);
// Match row found with more than 1 results  - the key exists. 
if ( $num > 0 ) {   
    list($akid,$key,$total_access) = mysql_fetch_row($result);
    if ($code == $key) { 
        if(empty($err)){
            $total_access++;
            mysql_query("update access_keys set total_access='$total_access' where akid='$akid'") or die(mysql_error());
            header("Location: ./");
        }
    } else {
        $err[] = "Invalid Access Key. Please try again with correct access key.";
    }
} else {
    $err[] = "Error - Invalid Access Key. No access exists for your user ID.";
}       
}

I'm wanting it to add to the Total Access field each time the user enters the correct code, but it's not working.

This is my form code:

<form name="postAccess" id="postAccess" method="post" action="access.php">
    <input type="password" name="access-key" id="access-key" style="background-color:black;color:white;" size="40" /><br/>
    <input name="doSubmit4" type="submit" id="doSubmit4" value="submit">
</form>

Upvotes: 1

Views: 303

Answers (1)

user849137
user849137

Reputation:

It might be your parent if brackets.

Try:

if (isset($_POST['access']) === true )

instead of

if ( $_POST['access']=='submit' )

If that doesn't seem to do it, do some debugging.

Throw some echo statements in those if brackets to see what conditions are true and what are not.

EDIT:

I just realized another problem.

Your form action is invalid. This should be the path to the page you are posting the data to. So:

Instead of:

<form name="access" id="access" method="post" action="access">

You should have:

<form name="access" id="access" method="post" action="your-php-path-here.php">

Notice how action = "your-php-path-here.php" in the above code.

Upvotes: 1

Related Questions