2015-09-16 139 views
-1

我有一個代碼不會更新。如果我在輸入文本中輸入任何內容並按下提交,那麼'samp.php'仍將顯示我已更新的數據庫的原始信息。PHP不會更新Mysql

代碼的index.php爲connect.php

<?php 
include 'connect.php'; 
?> 
<html> 
    <head> 
    <title>Basic Form Handling PHP</title> 
    </head> 
    <body> 
    <?php 
$result1 = $db->query("select * from chapter where chapter_id='1'"); 
while($row1 = mysqli_fetch_assoc($result1)){ echo "<form action='samp.php' method='POST'>"; 
echo "  <input class='input' type='hidden' name='cid' value='{$row1['chapter_id']}'/>"; 
echo "  Title: <br>"; 
echo "  <input type='text' name='ctitle'><br>"; 
echo "  Body: <br>"; 
echo "  <input type='text' name='cbody'><br>"; 
echo "  <input type='submit' name='submit' value='PRESS ME'>"; 
echo " </form>"; 
} 
    ?> 
    </body> 
</html> 

代碼samp.php

<?php 
$db = new mysqli('localhost', 'root', '2830775', 'unity'); 
?> 

<?php 
include 'connect.php'; 
?> 
<?php 
$id = $_POST['cid']; 
$title = $_POST['ctitle']; 
$body = $_POST['cbody']; 
$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id"); 
$result2 = $db->query("SELECT * FROM chapter where chapter_id='$id'"); 
?> 
<?php $row1 = mysqli_fetch_assoc($result2); 
echo $row1['chapter_body']; 

?> 
+2

看來你沒有'include'connect.php';'在samp.php – KAD

回答

0

更新$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id")$result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id='$id'")

你忘了結束單引號那裏。

+0

謝謝!你甚至看到了你的眼睛! –

1

您需要包括您的連接文件到您的same.php

<?php 
    include 'connect.php';// incliude your connection file 
    $id = $_POST['cid']; 
    $title = $_POST['ctitle']; 
    $body = $_POST['cbody']; 
    $result = $db->query("UPDATE chapter set chapter_title='$title', chapter_body='$body' where chapter_id = '$id'");//missing Singal quote in end 
    $result2 = $db->query("SELECT * FROM chapter where chapter_id='$id'"); 
?> 
<?php 
    $row1 = mysqli_fetch_assoc($result2); 
    echo $row1['chapter_body']; 

?>