简体   繁体   English

Php发布脚本无法从数据库获取数据

[英]Php posting script not getting data from database

I have a php posting script and I need it to grab the data from the database. 我有一个php发布脚本,我需要它从数据库中获取数据。 Here's the script: 这是脚本:

    <?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>

This script is supposed to grab first_name from the database and it is not. 该脚本应该从数据库中获取first_name,而不是。 Help? 救命?

Edit: There's the whole code. 编辑:这是整个代码。

Many things are strange with your code 你的代码很多东西都很奇怪

I believe it's blank because one of the if/else is messed up: 我相信它是空白的,因为其中一个if / else搞砸了:

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

then you have $error variable that have no meaning 然后你有$ error变量,没有任何意义

$error = false;

Then you have in your form : 然后你有你的形式:

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

but you dont want to grab it from there, but the database: 但你不想从那里抓住它,但数据库:

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

Then your wanna grab $last_name From the post 然后你想从帖子中获取$ last_name

$last_name = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); $ last_name = mysqli_real_escape_string($ dbc,trim($ _ POST ['last_name']));

but you don't have it in your form 但是你的形式没有它

Also this part: 这一部分:

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')";
    }
}   

You you have a condition on new_picture Where did you initialize that. 你有关于new_picture的条件你在哪里初始化它。 Why is it the same insert query again? 为什么再次使用相同的插入查询?

Don't you need quote around that? 你不需要引用它吗?

you have so many issues here, I advice you to trouble shoot step by step. 你这里有很多问题,我建议你一步一步地拍麻烦。 and redesign tis whole thing. 并重新设计整个事情。

I put something real quick together that works on my system. 我把一些快速的东西放在我的系统上。

This is a basic method and I mean basic, so you'll need to do the rest. 这是一个基本的方法,我的意思是基本的,所以你需要做其余的事情。

Just change the DB credentials to your own, and the the_user_id assigned to $_SESSION['user_id'] 只需将数据库凭据更改为您自己的凭据,并将the_user_id分配给$_SESSION['user_id']

It's the best I can do to help. 这是我能做的最好的帮助。

<?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);

Its here, 它在这里,

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

One of these file must not be set or php cant find these file. 其中一个文件不能设置或php无法找到这些文件。 So as it says 'require' which means till we dont get this file it will not proceed. 所以它说“要求”意味着直到我们没有得到这个文件它不会继续。 so it halt the execution there itself. 所以它停止了那里的执行。

try it with : 试试看:

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

It you see the page then problem is here itself. 你看到页面然后问题就在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM