Forum Moderators: coopster
$connect = mysqli_connect('localhost', 'admin', '', 'social_net')
or die('Could not connect to database: ' . mysqli_error()); [size=3]<?php
session_start();
// has function for sanitzing data
include 'includes/functions.inc.php';
// needed to connect to DB
include 'includes/connect.inc.php';
// user ID needed to be passed for query - converted to integer for convience
$id = intval($_SESSION['id']);
if (isset($_POST['make_post'])) {
$body = $_POST['user_post'];
$body = sanitize_data($body);
$date_added = date('d-M-Y g:H a');
$stmt = $connect->prepare("SELECT `first_name`, `last_name`
FROM `users` WHERE `id` = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($col1, $col2);
while ($stmt->fetch()) {
$first_name = $col1;
$last_name = $col2;
}
$stmt->close();
$added_by = $first_name . ' ' . $last_name;
$posted_to = $first_name . '\'s page';
}
echo $body . '<br>';
echo gettype($body) . '<br>';
echo $date_added . '<br>';
echo gettype($date_added) . '<br>';
echo $added_by . '<br>';
echo gettype($added_by) . '<br>';
echo $posted_to . '<br>';
echo gettype($posted_to) . '<br>';
echo $id . '<br>';
echo gettype($id) . '<br>';
$stmt2 = $connect->prepare("INSERT INTO `posts` (`body`, `date_added`,
`added_by`, `posted_to`, `user_id`) VALUES(?, ?, ?, ?, ?)");
$stmt2->bind_param('ssssi', $body, $date_added, $added_by, $posted_to, $id);
$stmt2->execute();
$stmt2->close();[/size] I have tried var_dump() and got the answer that nothing is being posted. I tried adding an error checker, and no error is being echoed.
error_reporting(-1); I put var_dump($stmt) after $stmt2->execute();
The stmtm2 = prepare... originally had "or die(mysqli_error()" attached to the end.
[size=3]$stmt2 = $connect->prepare("INSERT INTO `posts` (`body`, `date_added`,
`added_by`, `posted_to`, `user_id`) VALUES(?, ?, ?, ?, ?)");
if (false == $stmt2) {
die('prepare() failed ' . htmlspecialchars($stmt2->error));
}
$rc = $stmt2->bind_param('ssssi', $body, $date_added, $added_by, $posted_to, $id);
if (false == $rc) {
die('bind_param() failed ' . htmlspecialchars($stmt2->error));
}
$stmt2->execute();
if (false == $rc) {
die('execute() failed ' . htmlspecialchars($stmt2->error));
}
var_dump($stmt2);
$stmt2->close();
}[/size]
[size=3]$stmt2 = $connect->prepare("INSERT INTO `posts` (`body`, `date_added`,
`added_by`, `posted_to`, `user_id`) VALUES(?, ?, ?, ?, ?)");
if (false == $stmt2) {
die('prepare() failed ' . htmlspecialchars($stmt2->error));
}
$rc = $stmt2->bind_param('ssssi', $body, $date_added, $added_by, $posted_to, $id);
if (false == $rc) {
die('bind_param() failed ' . htmlspecialchars($stmt2->error));
}
$stmt2->execute();
if (false == $rc) {
die('execute() failed ' . htmlspecialchars($stmt2->error));
}
var_dump($stmt2);
$stmt2->close();
}[/size] $date_added = date('d-M-Y g:H a');