无法将新数据更新到表中。
我的表结构:
Table book:
-id (Primary, Auto Increment, INT)
-title
-publisher_id (Foreign key to Table publisher.pub_id, INT)
Table publisher:
-pub_id (Primary, Auto Increment, INT)
-pub_name 我想要做的是将新数据更新到表book中,但不能。我找不到哪里错了。
代码:
<?php
if($_POST){
$uid = $_GET['id'];
$title = mysqli_real_escape_string($mysqli, $_POST['title']);
$publisher = mysqli_real_escape_string($mysqli, $_POST['publisher']);
// Update publisher
$queryid = "SELECT pub_id FROM publisher WHERE pub_name = '$publisher' ";
$result = $mysqli->query($queryid);
$pub_id = $result->fetch_assoc();
//4Update book
$query = "UPDATE book
SET
title = '$title',
publisher_id = 1
WHERE id = $uid";
$mysqli->query($query) or die();
$msg ='bookinfo updated';
header('Location: index.php?msg=' . urlencode($msg) . '');
exit;
}
?>当我将'$pub_id['pub_id']'更改为'".$pub_id['pub_id']."'时,"WHERE“行变成了黄色,如图所示……

像这样更改代码也不起作用:
$query = "UPDATE book
SET
title='$title',
publisher_id= 1
WHERE id= $uid";发布于 2018-01-20 12:11:42
如果您在MAC上进行开发,请在加载页面时尝试跟踪日志文件:
PHP:
tail -f /Applications/MAMP/logs/php_error.log
MYSQL:
tail -f /Applications/MAMP/logs/mysql_error_log.err
在Windows上尝试做同样的事情(显然使用不同的日志文件位置/命令)。
问题可能是您无法连接到MYSQL数据库。此外,在您的帖子数据中可能没有定义这样的发布者。
发布于 2018-01-20 12:57:03
我运行了这个程序,它正在工作。添加一些输出,并让您的代码自动检查。
我在命令行上运行了这个。如果您愿意,您可以针对web进行调整。
书
CREATE TABLE `book` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
`publisher_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;出版商
CREATE TABLE `publisher` (
`pub_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`pub_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`pub_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;数据
INSERT INTO `publisher` (`pub_id`, `pub_name`)
VALUES
(1, 'Penguin'),
(2, 'Random');
INSERT INTO `book` (`id`, `title`, `publisher_id`)
VALUES
(1, 'Wrong', 2);代码
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
// Set up some test data
$_POST = [
'id' => 1,
'title' => 'Right',
'publisher' => 'Penguin'
];
if($_POST) {
// $uid = $_GET['id']; // I'll assume this was a mistake and the data should come from $_POST
$uid = $_POST['id'];
$title = mysqli_real_escape_string($mysqli, $_POST['title']);
$publisher = mysqli_real_escape_string($mysqli, $_POST['publisher']);
// Update publisher
$query = "SELECT pub_id FROM publisher WHERE pub_name = '$publisher' ";
$result = $mysqli->query($query);
$pub = $result->fetch_assoc();
print_r($pub);
echo PHP_EOL;
// Read the data before updating
$q1 = "SELECT title FROM book WHERE id = {$uid} ";
$result = $mysqli->query($q1);
$bookBefore = $result->fetch_assoc();
echo "Before " . print_r($bookBefore, true) . PHP_EOL;
// I assume you need $pub for something but this code does not use it
// Update book
$update ="UPDATE book
SET title='$title',
publisher_id = 1
WHERE id= $uid";
$mysqli->query($update) or die();
$msg = 'bookinfo updated';
echo "$msg\n";
$result = $mysqli->query($q1);
$bookAfter = $result->fetch_assoc();
echo "After " . print_r($bookBefore, true) . PHP_EOL;
if ( $bookBefore['title'] != $bookAfter['title'] ) {
echo "Book title changed" . PHP_EOL;
}
else {
echo "Book NOT changed" . PHP_EOL;
}
if ( $bookAfter['title'] == $_POST['title'] ) {
echo "Book title updated correctly" . PHP_EOL;
}
else {
echo "Book title not correctly updated." . PHP_EOL;
}
//header('Location: index.php?msg='.urlencode($msg).'');
//exit;
}https://stackoverflow.com/questions/46626905
复制相似问题