chaseman Posted February 25, 2011 Share Posted February 25, 2011 I'm trying to achieve that my script checks in the database if the user has submitted the data twice. The script takes the user submission and checks it with the column in the database with != and == But it ALWAYS sees it as UNEQUAL even if the data is EQUAL. Here's the script <?php $con_submit = $_POST['submit']; $user_id = $_SESSION['user_id']; if ($con_submit && isset($user_id)){ // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Grab the score data from the POST $knuffix_name = strip_tags(trim($_POST['knuffix_name'])); $knuffix_contribution = $_POST['knuffix_contribution']; $knuffix_category = strip_tags(trim($_POST['sort_category'])); $query = "SELECT contribution FROM con WHERE user_id = '$user_id'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); $assoc = mysqli_fetch_assoc ($query_run) or die (mysqli_error ($dbc)); $db_user_con = $assoc['contribution']; $num_rows = mysqli_num_rows ($query_run) or die (mysqli_erro ($dbc)); if ($knuffix_contribution != $db_user_con) { $query = "SELECT contribution FROM con"; $query_run = mysqli_query ($dbc, $query); $assoc = mysqli_fetch_assoc ($query_run); $db_con = $assoc['contribution']; if ($knuffix_contribution != $db_con) { if (!empty($knuffix_name) && !empty($knuffix_category) && !empty($knuffix_contribution)) { // Write the data into the database $query = sprintf("INSERT INTO con (user_id, name, contribution, category, contributed_date) VALUES ('$user_id', '%s', '%s', '%s', now())", mysqli_real_escape_string($dbc, $knuffix_name), mysqli_real_escape_string($dbc, $knuffix_contribution), mysqli_real_escape_string($dbc, $knuffix_category)); mysqli_query($dbc, $query) or die (mysqli_error($dbc)); mysqli_close($dbc); } else { echo "<p class='error'>Please enter all of the information to add your contribution.</p>"; } } else { echo "You have to be signed-in to do a contribution."; } } else { echo "This contribution already exists in the database. To keep this place clean we do not allow duplicate submissions."; } } else { echo "You cannot make the same submission twice."; } ?> Where is the problem? Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 25, 2011 Author Share Posted February 25, 2011 i think the problem is that the script is not looking in the correct row, it simply selecting a random row and thus it's unequal, I'd have to rewrite the query into: SELECT contribution WHERE user_id = '$user_id' AND contribution = '$submitted_contribution' And then if ($num_rows == 0) { // run script } else { echo "already exists"; } This may work, should have thought of that earlier. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.