我通过mysqli(php)更新表时遇到了奇怪的行为。我有一个简单的记录表:
mysql> describe reports_1;
+-------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+-------------------+-----------------------------+
| line_number | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(30) | YES | | NULL | |
| id | int(10) unsigned | YES | | NULL | |
| stamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| content | varchar(1000) | YES | | NULL | |
+-------------+------------------+------+-----+-------------------+-----------------------------+
line_number是主键(而id是外键)。当我在CLI中更新此表时,一切都很好:
mysql> update reports_1 set content='works fine' where line_number='24';
Query OK, 1 row affected (0.00 sec)
然而,当我从php中执行完全相同的查询时,即
$q = "update reports_1 set content='works fine' where line_number='24'";
$update = $connection->query($q);
我的表中有一个双重(!)更新:
mysql> select * from reports_1;
+-------------+----------+------+---------------------+------------+
| line_number | username | id | stamp | content |
...
| 24 | user | 210 | 2016-02-04 11:56:14 | works fine |
| 25 | user | 0 | 2016-02-04 11:56:14 | works fine |
+-------------+----------+------+---------------------+------------+
可以看出,非常意外(对我来说)更新被复制,给定一个新的密钥,时间戳是相同的,外键设置为零。
我无法绕过导致这种情况的原因!有没有人可以对这种行为有所了解?这是预期的吗?我错过了什么吗?
非常感谢您的帮助。
根据要求修改 php脚本接收从js函数发送的数据,该函数获取textarea字段的值:
if (isset($_POST['line_number']))
{
$line_nr = $_POST['line_number'];
$update = $_POST['record'];
$q15 = "update reports_1 set content='$update' where line_number=$line_nr";
$edit = $connection2->query($q15);
if (!$edit) die ("Database access failed: " . $connection2->error);
}
这就是真的所有......