PHP发布表需要来自用户表的信息

时间:2014-01-25 19:54:08

标签: php database posting

我有一个发布脚本,我需要将帖子放在数据库中,但是我需要将另一个表中的一些数据放入post表中,我不知道该怎么做,这是我的代码。

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

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

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

        // Confirm success with the user
        echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php">view your profile</a>?</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
      }
    }
  } // End of check for form submission
  else {


    $error = false;

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


    //DO I NEED TO PUT SOMETHING HERE TO SET A VARIABLE//
    //FOR THE FIRST_NAME AND LAST_NAME VALUES TO PUT IN//
    //THE DATABASE?//


           if (!empty($new_picture)) {
    $query = "INSERT INTO `ccp2_posts` (`first_name`, `last_name`) VALUES ('$first_name', '$last_name')";
        }
        else {
          $query = "INSERT INTO `ccp2_posts` (`first_name`, `last_name`) VALUES ('$first_name', '$last_name')";
        }
        mysqli_query($dbc, $query);

    else {
      echo '<p class="error">There was a problem accessing your profile.</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>Personal Information</legend>
      <label type="hidden" for="post1">Post:</label><br />
      <textarea rows="4"  name="post1" id="post1" cols="50">Post Here...</textarea><br />
    </fieldset>
    <input type="submit" value="Save Profile" name="submit" />     
  </form>
   </div>
  <?php

include ("include/footer.html");

?>

</body> 
</html>

我需要从ccp2_user表中获取first_name和last_name数据,并将其放在ccp2_posts表的first_name和last_name区域中。帮助

1 个答案:

答案 0 :(得分:0)

您的$row变量的值应为first_name和last_name:

$first_name = $row['first_name']; $last_name = $row['last_name'];