无法将新数据更新到表格中。
我的桌子结构:
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
我想要做的是将新数据更新到TABLE 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";
答案 0 :(得分:0)
如果您正在开发MAC,请在加载页面时尝试拖尾日志文件:
PHP:
tail -f /Applications/MAMP/logs/php_error.log
MYSQL:
tail -f /Applications/MAMP/logs/mysql_error_log.err
尝试在Windows上执行相同的操作(显然使用不同的日志文件位置/命令)。
问题可能是您无法连接到MYSQL数据库。此外,您的帖子数据中可能没有定义此类发布者。
答案 1 :(得分:0)
我跑了这个,它正在运作。添加一些输出并使代码自行检查。
我在命令行上运行了这个。如果您愿意,可以调整网络。
书
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;
}