2013-06-20 38 views
-1

我是新來的PHP,我在這個網站上看到一個問題,但是問題的答案並沒有幫助我的情況。我無法用我的php代碼將記錄插入到我的數據庫中。爲什麼我的php代碼不會將其數據發送到我創建的數據庫?

這裏是我的代碼,我已經按照Jason Lengstorf的名爲「PHP For Absolute Begginers」的教科書。如果需要,我可以在註釋中附加其餘代碼,您也可以在源代碼下的http://www.apress.com/9781430224730網站上找到代碼。當我的問題是,我應該是更新數據庫,在那裏開始於第66行,我註釋掉你旁邊else語句顯示:

<?php 

// Include the functions so we can create a URL 
include_once 'functions.inc.php'; 
include_once 'images.inc.php'; 

//$e = array(); 

if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submit']=='Save Entry' 
&& !empty($_POST['page']) && !empty($_POST['title']) && !empty($_POST['entry']))      
{ 
// Create a URL to save in the database 
$url = makeUrl($_POST['title']); 

if(isset($_FILES['image']['tmp_name'])) //line 16 
{ 
    try 
    { 
     $img = new ImageHandler("/simple_blog/images/");//not in the textbook 
     //print_r($_FILES); 
     //exit; 
     $img_path = $img->processUploadedImage($_FILES['image']); 
     echo '<img src="', $img_path,'" /><br />';//This prints out the image 
    } 
    catch(Exception $e) 
    { 
     die($e->getMessage()); 
    } 
} 
else 
{ 
    $img_path = NULL; 
} 

// Include database credentials and connect to the database 
include_once 'db.inc.php'; 
$db = new PDO(DB_INFO, DB_USER, DB_PASS); 

echo "Image Path: ", $img_path, "<br />"; 
//exit; in the book but not on the source 

// Edit an existing entry 
if(!empty($_POST['id'])) 
{ 
    $sql = "UPDATE entries 
      SET title=?, entry=?, url=? 
      WHERE id=? 
      LIMIT 1"; 
    $stmt = $db->prepare($sql); 
    $stmt->execute(
     array(
      $_POST['title'], 
      $img_path, 
      $_POST['entry'], 
      $url, 
      $_POST['id'] 
     ) 
    ); 
    $stmt->closeCursor(); 
} 

// Create a new entry 
else    //line 66 problems with the code dont work until after this line 
{ 
    //Save the entry into the database 

    echo"please work this time"; 
    $con = mysqli_connect("localhost","root","","simple_blog"); 

    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    $page = $_POST['page']; 
    //var_dump($page); 
    $page = mysql_real_escape_string($page); 
    $title = $_POST['title']; 
    //var_dump($title); 
    $title = mysql_real_escape_string($title); 
    $entry = $_POST['entry']; 
    //var_dump($entry); 
    $entry = mysql_real_escape_string($entry); 

    $sql = "INSERT INTO entries(page, title, image, entry, url) 
    VALUES ('$page', '$title', '$img_path', '$entry', '$url')"; 

    mysqli_query($con, $sql); 

    mysqli_close($con); 
} 

// Sanitize the page information for use in the success URL 
$page = htmlentities(strip_tags($_POST['page'])); 

// Send the user to the new entry 
echo "hopefully we get here"; 
header('Location: /simple_blog/'.$page.'/'.$url); 
exit; 
} 

else 
{ 
    header('Location: ../'); 
    exit; 
} 

?> 

任何人誰可以告訴我爲什麼我的代碼不發佈到數據庫,我感謝你的幫助,我感謝你。

+0

什麼問題?你有錯誤嗎?一個空白的屏幕?當您將調用添加到mysqli_error()時,輸出是什麼? – andrewsi

+0

我運行代碼後數據庫爲空 –

+0

我也沒有收到錯誤消息,但我的代碼的其餘部分沒有按照我希望的方式運行,因爲它依賴於數據庫中的數據,這是沒有什麼 –

回答

1

你在混合和匹配不同的數據庫庫。他們中的任何一個都是相互兼容的。從一個結果不能在其他使用,所以以下是平展操作錯誤:

$page = mysql_real_escape_string($page); 
      ^---note the LACK of an i 

mysqli_query($con, $sql); 
    ^---note the PRESENCE of an i 

你的所有DB代碼盲目地假定在成功和失誤,留下混亂和破壞的路徑的第一後失敗。如果你有最小的錯誤處理,你會看到錯誤:

同樣,mysql(沒有我)已棄用,不應再被使用。你從PDO開始,堅持PDO。不要混合匹配db庫。

相關問題