Jump to content

MYSQL query problem I think


Finker92

Recommended Posts

Hi all,

I have a form for users to input posts into their blog. This works fine with the INSERT INTO query in my code. However, when I test it and check the database the entry is going in fine but there is no id attached to it. I'm not really sure how to use an INNER JOIN with an INSERT. As you will see, I have tried to work around it, but I am getting a parameter error "mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given". I suspect this is because the query is not working. I have echoed out the SESSION['name'] variable $whichuser and it works fine. Any help appreciated.

<?php
session_start(); // always on the top of the script when user should be logged in
if(isset($_SESSION['loggedin'])){
}else{
header("location: index.php");
}
require('connection.php');

$whichuser=$_SESSION['name'];

if(empty($_POST['title'])){
$title=NULL;
echo "<b>Warning:</b>You haven't given your post a title.</br>";
}else{
$title=mysqli_real_escape_string($connection, trim($_POST['title']));
}	
if(empty($_POST['content'])){
$content=NULL;
echo "<b>Warning:</b>You haven't entered any content.</br>";
}else{ 
$content=mysqli_real_escape_string($connection, trim($_POST['content']));
}
if(($title) && ($content)){
$newentry = "INSERT INTO blogposts (title, content, timeofpost) 
VALUES ('$title', '$content', NOW())";
if($newentry){
$join= "SELECT * FROM blogposts INNER JOIN users ON blogposts.post_number = users.username WHERE username='$whichuser'";
if($join){
$entry=mysqli_query($connection, $join) or die(mysqli_error($connection));
}else{
echo "THERE'S A PROBLEM WITH THE QUERY.";
}

while($newpost = mysqli_fetch_array($connection, $entry)){
extract($newpost);
echo "<h3>$title</h3></br>";
echo "$content</br>";
echo "$whichuser</br>";
}
}
}
?>

Link to comment
Share on other sites

Right, I have had a rethink and have come up with this, but I'm still getting the same error message. Anybody?

 

<?php
session_start(); // always on the top of the script when user should be logged in
if(isset($_SESSION['loggedin'])){
}else{
header("location: index.php");
}
require('connection.php');

$whichuser=$_SESSION['name'];

if(empty($_POST['title'])){
$title=NULL;
echo "<b>Warning:</b>You haven't given your post a title.</br>";
}else{
$title=mysqli_real_escape_string($connection, trim($_POST['title']));
}	
if(empty($_POST['content'])){
$content=NULL;
echo "<b>Warning:</b>You haven't entered any content.</br>";
}else{ 
$content=mysqli_real_escape_string($connection, trim($_POST['content']));
}
if(($title) && ($content) && ($whichuser)){
$newentry = "INSERT INTO blogposts (author_id, title, content, timeofpost) 
VALUES ('$whichuser', '$title', '$content', NOW()) INNER JOIN users ON blogposts.author_id=users.user_id";
if($newentry){
$entry=mysqli_query($connection, $newentry);
}else{
echo "THERE'S A PROBLEM WITH THE QUERY.";
}

while($newpost = mysqli_fetch_array($connection, $entry)){
extract($newpost);
echo "<h3>$title</h3></br>";
echo "$content</br>";
echo "$whichuser</br>";
}
}
?>

Link to comment
Share on other sites

Doh! That was pretty silly of me. Thanks. I am trying to use the $_SESSION['name'] variable from check_login.php (first piece of code below) correctly in newposts.php. Please, please, please can someone point out exactly what I am doing wrong. I've been trying to figure this out for hours now. I can get this to work if I add a username field to the add post form but really, once the user is logged in, that shouldn't be necessary right? First piece of code in login check, second is form, and third is the code attached to the form. 

<?php
require('connection.php');
if(empty($_POST['name'])){
$name=NULL;
echo "Sorry, you forgot to enter your username.</br>";
}else{
$name=mysqli_real_escape_string($connection, trim($_POST['name']));
}	
if(empty($_POST['password'])){
$password=NULL;
echo "Sorry, you forgot to enter a password.</br>";
}else{ 
$password=mysqli_real_escape_string($connection, trim($_POST['password']));
}
if(($name) && ($password)){
session_start();
$info = "SELECT * FROM users WHERE username='$name' and password='$password'";
$return=mysqli_query($connection, $info) or die(mysqli_error($connection));

$count_rows=mysqli_num_rows($return);
if($count_rows==1){	
$_SESSION['loggedin']=true;
$_SESSION['name']=$_POST['name'];
$_SESSION['password']=$_POST['password'];
}
if(isset($_SESSION['loggedin'])){
header("Location:admin.php");
}else{
header("Location:index.php");
}
}
?>

My form for entering a new post

?>
<a href = 'logout.php'><h3>Log out</h3></a>
</br>
<a href = 'allposts.php'><h3>My profile and posts</h3></a>
</br>
<form method="POST" action="newposts.php">
<p><h2>Add a new post</h2>
<p><b>Title</b></p>
<p><input type="text" name="title" size="40" maxlength="200"/></p>
<p><b>Enter your new post here</b></p>
<p><textarea name="content" rows="5" cols="40"></textarea></p> 
<p> <input type="submit" name="submit" value="Post"></p>
</form>
</br></br>

...and the newposts.php script. Should the $_SESSION['name'] set as $name not enter into author_id in my query. Please help. It's driving me mad!

<?php
session_start(); // always on the top of the script when user should be logged in
if(isset($_SESSION['loggedin'])){
}else{
header("location: index.php");
}
require('connection.php');
if(isset($_SESSION['name'])){
$name=$_SESSION['name'];
}else{
echo "There is a problem.";
}
if(empty($_POST['title'])){
$title=NULL;
echo "<b>Warning:</b>You haven't given your post a title.</br>";
}else{
$title=mysqli_real_escape_string($connection, trim($_POST['title']));
}	
if(empty($_POST['content'])){
$content=NULL;
echo "<b>Warning:</b>You haven't entered any content.</br>";
}else{ 
$content=mysqli_real_escape_string($connection, trim($_POST['content']));
}
if(($name) && ($title) && ($content)){
$newentry= mysqli_query($connection,"INSERT INTO blogposts (author_id, title, content, timeofpost) VALUES ('$name', '$title', '$content', NOW())");	
if($newentry){
echo "Your post has now been entered";
}else{
echo "Your post could not be handled due to a system error.";
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.