2013-07-06 55 views
0

我目前正在創建我的網站,已經開始在那裏我將內容添加到我的數據庫表中未添加到數據庫

我add.php代碼點是這樣的:

<?php 

session_start(); 

include_once('../include/connection.php'); 

if (isset($_SESSION['logged_in'])){ 
     if (isset($_POST['title'], $_POST['content'])) { 
      $title = $_POST['title']; 
      $content = nl2br($_POST['content']); 
      $image = $_POST['Image URL']; 
      $link = $_POST['Link']; 
      $price = $_POST['Price']; 

if (empty($title) or empty($content)) { 
      $error = 'All Fields Are Required!'; 
}else{ 
    $query = $pdo->prepare('INSERT INTO apps (app_title, app_content, app_img, app_link, app_price) VALUES(?, ?, ?, ?, ?)'); 
    $query->bindValue(1, $title); 
    $query->bindValue(2, $content); 
    $query->bindValue(3, $image); 
    $query->bindValue(4, $link); 
    $query->bindValue(5, $price); 

    $query->execute(); 
    header('location: index.php'); 
} 

} 
      ?> 

<html> 
<head> 
<title>testing</title> 
<link rel="stylesheet" href="../style.css" /> 
</head> 

<body> 
<div class="container"> 
<a href="index.php" id="logo">CMS</a> 

<br /> 


<h4>Add Article</h4> 

<?php if (isset($error)) { ?> 
    <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br /> 
<?php } ?> 

<form action="add.php" method="post" autocomplete="off"> 

<input type="text" value="" name="title" placeholder="Title" /><br /><br /> 
<textarea rows="15" cols="50" value="" placeholder="Content" name="content"></textarea><br /><br /> 
<input type="text" value="" name="Image URL" placeholder="Image URL" /><br /><br /> 
<input type="text" value="" name="Link" placeholder="Link" /><br /><br /> 
<input type="text" value="" name="Price" placeholder="Price" /><br /><br /> 
<input type="submit" name="submit" value="Add Article" /> 

</form> 

</div> 
</body> 
</html> 


<?php 
}else{ 
     header('location: index.php'); 
} 

?> 

它加載確定並在顯示器上顯示工作文件,但它不會將信息添加到我的數據庫中,稱爲apps。

請問有人能告訴我爲什麼嗎?

謝謝。

+0

你在哪裏創建'$ pdo'?你有錯誤報告打開嗎? –

+0

嗨。我唯一的錯誤日誌是在頁面沒有加載時創建的cPanel。在頁面加載時,在此錯誤日誌中不會顯示任何內容。有沒有另一種方法來獲取所有錯誤? – kevstarlive

+0

上面的文件被稱爲add.php是,如上所述。 – kevstarlive

回答

1

實際上你的變量看起來不對。

試試這個

  $image = $_POST['Image URL']; 
     $link = $_POST['Link']; 
     $price = $_POST['Price']; 

編輯。你應該添加value="something"您輸入

<input type="text" value="" name="title" placeholder="Title" /><br /><br /> 
    <textarea rows="15" cols="50" value="" placeholder="Content" name="content"></textarea><br /><br /> 

編輯:2

提供一個名稱提交按鈕

<input type="submit" name="submit" value="Add Article" /> 
+0

只是在添加代碼的最後一位時纔會收到此錯誤消息。 PHP解析錯誤:語法錯誤,意外T_ELSE – kevstarlive

+0

即使第一部分的更改也不起作用 – kevstarlive

+0

我在這裏添加它:$ query-> execute(); header('location:index.php'); } } else {echo「somthing goes wrong」; }它只是顯示在我的頁面頂部http://www.xclo.co.uk/mobile/appdonate/admin/add.php – kevstarlive

0

ECHO_ME-

此代碼:

<?php 

session_start(); 

include_once('../include/connection.php'); 

if (isset($_SESSION['logged_in'])){ 
     if (isset($_POST['title'], $_POST['content'])) { 
      $title = $_POST['title']; 
      $content = nl2br($_POST['content']); 

if (empty($title) or empty($content)) { 
      $error = 'All Fields Are Required!'; 
}else{ 
    $query = $pdo->prepare('INSERT INTO apps (app_title, app_content, app_timestamp) VALUES(?, ?, ?)'); 
    $query->bindValue(1, $title); 
    $query->bindValue(2, $content); 
    $query->bindValue(3, time()); 

    $query->execute(); 
    header('location: index.php'); 
} 

} 
      ?> 

<html> 
<head> 
<title>testing</title> 
<link rel="stylesheet" href="../style.css" /> 
</head> 

<body> 
<div class="container"> 
<a href="index.php" id="logo">CMS</a> 

<br /> 


<h4>Add Article</h4> 

<?php if (isset($error)) { ?> 
    <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br /> 
<?php } ?> 

<form action="add.php" method="post" autocomplete="off"> 

<input type="text" name="title" placeholder="Title" /><br /><br /> 
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br /> 
<input type="submit" value="Add Article" /> 

</form> 

</div> 
</body> 
</html> 


<?php 
}else{ 
     header('location: index.php'); 
} 

?> 

但是當我去添加導致問題的新字段時。你所要求的所有過去的小事都不會影響我認爲的現行代碼。

還有更多建議嗎?謝謝。

+0

另外:我添加了$ query-> execute(); exit(); header('location:index。PHP'); 查詢並嘗試填寫表單後,點擊提交,它顯示一個空白的白色屏幕。這告訴我錯誤是查詢。 – kevstarlive