简体   繁体   中英

PHP / MySQL query stopped working for no apparent reason

I've got a page that alters a large quantity of variables in my MySQL database, and so far they've all worked great, but now all of the queries inside of a single logic-gate have stopped working for no apparent reason.

I've confirmed the following:
- The variable posted and used in the "if" statement of the gate is as it was intended
- The logic gate is triggered as intended (I can echo stuff and etc inside of it).
- The database connection is established, I am successfully running queries of various types before and after this logic gate on the same connection variable.
- The connection user has ALL PRIVILEDGES enabled, the aforementioned queries surrounding this logic gate are using similar functions successfully.

Here's the logic gate:

if (!empty($_POST["addqual"])){
    $coladqual = $_POST["addqual"];
    $sqlf = "ALTER TABLE users ADD UNIQUE ('$coladqual') INT( 2 ) NOT NULL";
    $conn->query($sqlf);
    $sqlc = "INSERT INTO competencebonus (competence,bonus)
    VALUES ($coladqual,0)";
    $conn->query($sqlc);
}

I've tried multiple alterations, but they don't seem to execute no matter what I do. I've got at least 20 other queries in other logic gates before and after these two and there seems to be virtually no difference between them, apart from these two just not working at all.

EDIT - Here's the error (Thanks to all of you who provided me with the error report syntax)

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''TestAA') INT( 2 ) NOT NULL UNIQUE' at line 1

What strikes me as odd is that only the closing parenthesis is around the post input (TestAA). Is it supposed to have both or neither?

I tried changing the syntax and got the following error:

Duplicate entry '0' for key 'TestAB'

The syntax was:

$sqlf = "ALTER TABLE users ADD `$coladqual` INT( 2 ) NOT NULL UNIQUE";

FINAL EDIT:

Made it work. Deleted the "NOT NULL" statement as recommended by Jeff Pucket II. Somehow this combined with the deletion of the parenthesis and use of backticks instead of apostrophe made the thing work. Thanks for those of you who had the patience to help me with this.

It looks like you're trying to alter an existing table with a unique not null column. I would expect this to fail if any rows already exist in the table, unless your engine imputes zero. Even then this would fail if there were more than one record because of the unique constraint. Also make sure that the column name being added doesn't already exist.

To get the error using MySQLi, try: $result = $conn->query($sqlf) or die($conn->error);

The 'unique' constraint should go at the end of your query

$sqlf = "ALTER TABLE users ADD ('$coladqual') INT( 2 ) NOT NULL UNIQUE";

Error checking depends on what flavor of MySQL you're running

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