投稿をデータベースに配置する必要がある投稿スクリプトがありますが、別のテーブルからのデータを投稿テーブルに入れる必要があります。これを行う方法がわかりません。これが私のコードです。
<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 領域に配置する必要があります。ヘルプ?