简体   繁体   English

PHP / MySQL 查询无缘无故停止工作

[英]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.我有一个页面可以更改我的 MySQL 数据库中的大量变量,到目前为止它们都运行良好,但是现在单个逻辑门内的所有查询都无缘无故地停止工作。

I've confirmed the following:我已经确认了以下内容:
- The variable posted and used in the "if" statement of the gate is as it was intended - 门的“if”语句中张贴和使用的变量与预期一致
- 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.在这两个之前和之后,我在其他逻辑门中至少有 20 个其他查询,它们之间似乎几乎没有区别,除了这两个根本不起作用。

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;您的 SQL 语法有错误; 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检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的 ''TestAA') INT(2) NOT NULL UNIQUE' 附近使用的正确语法

What strikes me as odd is that only the closing parenthesis is around the post input (TestAA).令我感到奇怪的是,只有右括号在后输入 (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'密钥“TestAB”的重复条目“0”

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.删除了 Jeff Pucket II 推荐的“NOT NULL”语句。 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);要使用 MySQLi 获取错误,请尝试: $result = $conn->query($sqlf) or die($conn->error);

The 'unique' constraint should go at the end of your query 'unique' 约束应该放在查询的末尾

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

Error checking depends on what flavor of MySQL you're running错误检查取决于您正在运行的 MySQL 版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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