Question

I have a posting script where I need to put the post in a database but I need some data from another table to put into the post table, and I do not know how to do this, here's my code.

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

I need to take the first_name and last_name data from my ccp2_user table and put it in the first_name and last_name area in the ccp2_posts table. Help?

No correct solution

OTHER TIPS

Your $row variable should have the values of first_name and last_name:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top