简体   繁体   中英

MySQL PHP - Inserting form data into tables. Unknown Error

So as the title stated, I'm trying to insert data into 2 tables if the form passes a few conditions (the user is logged in, the user hasn't voted on that product before, all of the fields have been filled in).

It was inserting the data into both tables perfectly when the condition was just:

$sql = "SELECT productid FROM votes WHERE username='$username' LIMIT 1";

But I realized that if the user had voted on any of the products, their username would appear in the table and they would fail the condition without having voted on the product. So I just added:

$sql = "SELECT productid FROM votes WHERE username='$username' AND productid='$id' LIMIT 1";

and now if I try to submit data to the database, it always returns the 'Error inserting into votes table' message but doesn't return the mysql_error() and obviously doesn't insert a new row into the votes table, but strangely it does update the products table.

I just can't figure out what's going on, so if anyone could help me diagnose the problem, I'd really appreciate it! Here's the code:

<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if($_POST['slider_surface'] !== "0" && $_POST['slider_edgewear'] !== "0" && $_POST['slider_centering'] !== "0" && $_POST['slider_corners'] !== "0"){
            $dbhost = 'localhost';
            $dbuser = 'root';
            $dbpass = 'root';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
            if(! $conn )
                {
                    die('Could not connect: ' . mysql_error());
                }

            $slider_surface = $_POST['slider_surface'];
            $slider_edgewear = $_POST['slider_edgewear'];
            $slider_centering = $_POST['slider_centering'];
            $slider_corners = $_POST['slider_corners'];
            $id = preg_replace('#[^a-z0-9]#i', '', $_GET['id']);
            session_start();
            $username = $_SESSION['username'];
            //check if user has already voted
            mysql_select_db('products');
            $sql = "SELECT productid FROM votes WHERE username='$username' AND productid='$id' LIMIT 1";
            $query = mysql_query( $sql, $conn );
            $uname_check = mysql_num_rows($query);
            if ($username){
                if ($uname_check < 1) {


                    $sql =  "INSERT INTO votes ".
                            "(username,productid,votesurface,voteedgewear,votecentering,votecorners,datetime) ".
                            "VALUES('$username','$id','$slider_surface','$slider_edgewear','$slider_centering','$slider_corners', now())";

                    $retval = mysql_query( $sql, $conn );

                    $id='';

                    // Make sure the _GET product ID is set, and sanitize it
                    $id = preg_replace('#[^a-z0-9]#i', '', $_GET['id']);

                    //Retrieves data from MySQL 
                    $data = mysql_query("SELECT * FROM products WHERE id='$id'") or die(mysql_error()); 
                    $product = mysql_fetch_array( $data );

                    $newvotecount = $product['votecount'] + 1;
                    $newsum_surface = $product['sumsurface'] + $slider_surface;
                    $newsum_edgewear = $product['sumedgewear'] + $slider_edgewear;
                    $newsum_centering = $product['sumcentering'] + $slider_centering;
                    $newsum_corners = $product['sumcorners'] + $slider_corners;

                    $sql =  "UPDATE products SET votecount='{$newvotecount}', sumsurface='{$newsum_surface}', sumedgewear='{$newsum_edgewear}', sumcentering='{$newsum_centering}', sumcorners='{$newsum_corners}' WHERE id='$id'";

                    $retval2 = mysql_query( $sql, $conn );


                    if(! $retval){
                        die('Error inserting into votes table: ' . mysql_error());
                    }
                    else if(! $retval2){
                        die('Error inserting into products table: ' . mysql_error());
                    }
                    $grading_error = 'success';
                    mysql_close($conn);
                } else
                    $grading_error = 'duplicateuser';
        } else
            $grading_error = 'nouser';
        }
    else
        $grading_error = 'emptyfields';}
?>

There's a problem when inserting data to table, so there must be an error with the INSERT statement. As @user2910809 says in previous comments, the sql evaluates as:

INSERT INTO votes (username,
                   productid,
                   votesurface,
                   voteedgewear,
                   votecentering,
                   votecorners,
                   datetime) 
           VALUES ('magmar',
                   '52',
                   '7',
                   '6',
                   '5',
                   '5',
                   now())

This sentence is syntactically correct. So, if there's an error, it must be with the inserted values.

As @user2910809 states on his comments, there's a UNIQUE key on username column, and it is what was throwing the error.

So the solution is to modify table's indexes to allow multiple rows with the same username .

Edit : as suggested in comments, the SQL to solve this problem was: ALTER TABLE votes DROP INDEX username;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM