0

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

<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 区域。帮助?

4

1 回答 1

0

您的$row变量应该具有 first_name 和 last_name 的值:

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

于 2014-01-25T20:00:31.933 回答