2017-10-08 103 views
0

無法將新數據更新到表中。無法使用PHP將數據更新到MySQL中

我的表結構:

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 

我想要做的就是更新新的數據到桌上的書,但不能。 我找不到錯在哪裏。

代碼:

<?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']."', 「去哪兒」 行變成黃色喜歡的圖片,...

Picture

改變這樣的代碼沒工作:

$query = "UPDATE book 
      SET 
      title='$title', 
      publisher_id= 1 
      WHERE id= $uid"; 
+0

什麼了你的日誌FIL e告訴你 – Webdesigner

+0

@Webdesigner對不起,我是PHP新手,你的意思是錯誤信息還是別的?運行代碼後沒有錯誤消息.. – Rufus7

+1

像@Webdesigner說的那樣,檢查你的日誌。此外,你可以[** SQL注入**](https://www.owasp.org/index.php/SQL_Injection)。您需要使用準備好的語句,而不是將變量連接到查詢中。請參閱[如何防止PHP中的SQL注入?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1)。 –

回答

0

I F你正在開發一個MAC嘗試尾日誌文件而加載頁面:

PHP:

tail -f /Applications/MAMP/logs/php_error.log

MYSQL:

tail -f /Applications/MAMP/logs/mysql_error_log.err

嘗試做同樣的事情當你在Windows上(顯然有不同的日誌文件位置/命令)。

問題可能是您無法連接到您的MYSQL數據庫。您的發佈數據中也可能沒有定義這樣的發佈商。

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; 
}