2014-01-26 80 views
1

我有一個php發佈腳本,我需要它從數據庫中獲取數據。這裏是腳本:Php發佈腳本沒有從數據庫獲取數據

<?php 
error_reporting(E_ALL); 
    session_start(); 

    // If the session vars aren't set, try to set them with a cookie 
    if (!isset($_SESSION['user_id'])) { 
    } 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Cheesecake Productions - Post Topic</title> 
    <link rel="stylesheet" type="text/css" href="include/style/content.css" /> 
</head> 
<body> 

<?php 

include ("include/header.html"); 

include ("include/sidebar.html"); 

?> 
<div class="container"> 
<?php 

    require_once('appvars.php'); 
    require_once('connectvars.php'); 

    // Make sure the user is logged in before going any further. 
    if (!isset($_SESSION['user_id'])) { 
    echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>'; 
    exit(); 
    } 
    else { 
    echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '. <a href="logout.php">Log out</a>.</p>'); 
    } 

    // Connect to the database 
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('could not connect to mysql '.mysqli_connect_error()); 

// Grab the profile data from the database 
$query = "SELECT first_name FROM ccp2_user WHERE first_name = '" . $_SESSION['user_id'] . "'"; 
    $data = mysqli_query($dbc, $query); 

    /////////////////////////// 
    ///What must I do after//// 
    //getting the data from//// 
//database. I am new to//// 
//PHP////////////////////// 
////////////////////////// 



    $row = mysqli_fetch_array($data); 
    $first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name'])); 



    if (isset($_POST['submit'])) { 
    // Grab the profile data from the POST 
    $post1 = mysqli_real_escape_string($dbc, trim($_POST['post1'])); 

    // Update the profile data in the database 
    if (!$error) { 
     if (!empty($post1)) { 
     // Only set the picture column if there is a new picture 
    $query = "INSERT INTO `ccp2_posts` (`first_name`, `post_date`, `post`) VALUES ('$first_name', NOW(), '$post1')"; 
     mysqli_query($dbc, $query); 

     // Confirm success with the user 
     echo '<p>Your post has been successfully added. Would you like to <a href="viewpost.php">view all of the posts</a>?</p>'; 

     mysqli_close($dbc); 
     exit(); 
     } 
     else { 
     echo '<p class="error">You must enter information into all of the fields.</p>'; 
     } 
    } 
    } // End of check for form submission 
    else { 
    echo '<p>Grr</p>'; 
    } 

    mysqli_close($dbc); 
?> 

    <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" /> 
    <fieldset> 
     <legend>Post Here:</legend>  
     <label type="hidden" for="post1">Post Content:</label><br /> 
     <textarea rows="4" name="post1" id="post1" cols="50">Post Here...</textarea><br /> 
    </fieldset> 
    <input type="submit" value="Save Post" name="submit" />  
    </form> 
    </div> 
    <?php 

include ("include/footer.html"); 

?> 

</body> 
</html> 

這個腳本應該從數據庫抓first_name,它不是。幫幫我?

編輯:有整個代碼。

+0

檢查$ dbc對象。這是非常不安全的。清理用戶提交的數據並使用準備好的語句。 – datelligence

+0

你做了'print_r($ row)'對嗎? –

+0

而... Cookie是從哪裏生成的?如果沒有創建,則代碼已完成其工作,如「不創建會話」一樣。創建一個。 –

回答

2

很多事有蹊蹺你的代碼

我相信它是空白因爲一個的if/else被搞砸:

if (isset($_POST['submit'])) { 
    .... 
    } 
    else {//here 
    else { 
     echo '<p class="error">There was a problem accessing your profile.</p>'; 
    } 
    } 

,那麼你有沒有意義

$錯誤變量
$error = false; 

然後,你必須在你的形式:

<input type="text" id="first_name" name="first_name" value="" /><br /> 

,但你不要想從那裏抓住它,但數據庫:

$query = "SELECT first_name FROM ccp2_user 
      WHERE user_id = '" . $_SESSION['user_id'] . "'"; 

然後你想抓住$姓氏從後

$姓氏= mysqli_real_escape_string($ DBC,修剪($ _ POST [ '姓']));

,但你沒有在你的形式

而且這一部分:

if (!empty($first_name) && !empty($post1)) { 
    // Only set the picture column if there is a new picture 
    if (!empty($new_picture)) { 
     $query = "INSERT INTO `ccp2_posts` (`first_name`, `post_date`, `post`) 
         VALUES ('$first_name', NOW(), '$post1')"; 
    } 
    else { 
     $query = "INSERT INTO `ccp2_posts` (`first_name`, `post_date`, `post`) 
         VALUES ('$first_name', NOW(), '$post1')"; 
    } 
} 

你你有new_picture一個條件,你在哪裏初始化。爲什麼再次使用相同的插入查詢?

你不需要引號嗎?

你有這麼多問題在這裏,我建議你一步一步解決問題。並重新設計整個事情。

+0

我要從另一個腳本,我有,所以我需要清理我有點我想 – user2544765

+0

@ user2544765是的你應該保持簡單。我有一種感覺,你只需要組織好一點,你應該沒問題。 – meda

+0

我已經編輯了上面的腳本...看看它應該更簡單。 – user2544765

0

它在這裏,

require_once('appvars.php'); 
require_once('connectvars.php'); 

其中一個文件不得設定或php無法找到這些文件。因爲它說'需要'這意味着,直到我們沒有得到這個文件,它將不會繼續。所以它停止了它自己的執行。

嘗試用:

include('appvars.php'); 
include('connectvars.php'); 

它,你看到的網頁,然後問題是在這裏本身。

+0

我其實已經得到它現在顯示它只是沒有發送信息到數據庫 – user2544765

1

我把一些真正快速的東西放在一起,在我的系統上工作。

這是一個基本方法,我的意思是基本的,所以你需要做的休息。

只要改變DB憑據自己,並分配給$_SESSION['user_id']

the_user_id這是我能做的最好的幫助。

<?php 
$DB_HOST = "xxx"; 
$DB_USER = "xxx"; 
$DB_PASS = "xxx"; 
$DB_NAME = "xxx"; 

$dbc = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); 
if($dbc->connect_errno > 0) { 
    die('Connection failed [' . $dbc->connect_error . ']'); 
} 

session_start(); 
$_SESSION['user_id'] = "the_user_id"; // change this to the user's id 

// You can use * also as the line from below 
// $sql = $dbc->query("SELECT * FROM `ccp2_user` WHERE `user_id` = '" . $_SESSION['user_id'] . "'"); 
$sql = $dbc->query("SELECT `first_name` FROM `ccp2_user` WHERE `user_id` = '" . $_SESSION['user_id'] . "'"); 

while($row= mysqli_fetch_array($sql)) 
{ 
echo $row['user_id']; 
} 

// for testing purposes 
// var_dump($_SESSION['user_id']); 
// var_dump($_SESSION); 

mysqli_close($dbc); 
+0

我可能會在將來使用這個 – user2544765

+0

它肯定會/可能會有所幫助。你可以基於你的INSERT方法。 @ user2544765 –