Juan Montes
Juan Montes

Reputation: 33

Making a query issue

i'm making a query to insert some values to a table and update other in other table all this in the same function.

The problem i have is with the Insert query, when has been execute, give me a syntax error that you can see below.

capture

I reviewed the code many times but i don't see any error. When I remove the Insert query works fine and if I remove de update query and leave just the insert give me the same error.

Here the query

if (isset($_POST['goodalt']) && isset($_POST['gengood'])){
    $goodalt = mysqli_real_escape_string($con, $_POST['goodalt']);
    $genid = mysqli_real_escape_string($con, $_POST['gengood']);
        $res = mysqli_query($con, "SELECT * FROM `generators` WHERE `name` = '$genid'") or die(mysqli_error($con));
        while($row = mysqli_fetch_assoc($res)) {
            $genname = $row['name'];
        }
    mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
    mysqli_query($con, "UPDATE `generator$genid` SET `status` = '3' WHERE `alt` = '$goodalt'") or die(mysqli_error($con));

}

Any help? Thanks.

Upvotes: 0

Views: 57

Answers (3)

ingconti
ingconti

Reputation: 11646

Apart form error, there is a BIG issue about concurrency and threading model. Every time you split INSERT/UPADATE in more separate PHP calls two mysql, you can analyze CAREFULLY if separate queries maintain DB consistent.

Consider carefully is two (or more) web requests (executing the same PHP script) can be safely run in parallel, especially on code where You did:

mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));

mysqli_query($con, "UPDATE `generator$genid` SET `status` = '3' WHERE `alt` = '$goodalt'") or die(mysqli_error($con));

Insert and Updates are atomic, but here you call two separate queries

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57121

Your missing a close bracket in your insert...

mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1')") or die(mysqli_error($con));

BUT you should also be using prepared statements and bind variables...

Upvotes: 0

N00b Pr0grammer
N00b Pr0grammer

Reputation: 4647

The issue that you're facing is because of the unnecessary usage of backticks or single quotation marks. You can follow the following article to make the changes and the issue should be sorted out.

Article here

Change these from:

 mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));

to

mysqli_query($con, "INSERT INTO user_accounts (username, accs, genid, gen-name, date, status) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));

Hope that helps!

Upvotes: 0

Related Questions