Jump to content

Finker92

Members
  • Posts

    17
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Finker92's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Not sure. I don't think that I changed anything - the variables are the same, GET_id is the same, etc. so can't figure. Could it be single quotes in the SELECT query? This is the error I'm getting "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C:\wamp\www\BlogProject\recentposts.php on line 9"
  2. This was working before but now it's not. Can anyone please, please tell me why? <?php require('pages/connection.php'); $latestposts= mysqli_query($connection,"SELECT title, content, post_number FROM blogposts ORDER BY timeofpost DESC LIMIT 9");//Query the database while($displayposts=mysqli_fetch_array($latestposts)){ //go and get the information echo "{$displayposts['title']}</br>"; echo "{$displayposts['content']}</br>"; echo "<a href='recentposts.php?id={$displayposts['post_number']}'>Read more</a></br>"; } ?> [code] <?php require('pages/connection.php'); $ID_number = $_GET['id']; $content= mysqli_query($connection,"SELECT title, content, username FROM blogposts INNER JOIN users ON blogposts.author_id = users.user_id WHERE post_number='$ID_number'"); if(!$content){ echo mysqli_error(); echo "I am sorry but there was an error connecting to the server."; } while($singlepage=mysqli_fetch_array($connection, $content)){ extract($singlepage); echo "<h3>$title</h3></br>"; echo "$content</br>"; echo "$username</br>"; } ?>
  3. Went back out tested it again and it is now working.
  4. 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."; } } ?>
  5. 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>"; } } ?>
  6. Sorry, I haven't had a chance to get back to it. Fair point on the coding, but I'm new and working around the logic is still difficult for me. Anyway, I reworked it and got it going. Thanks. I can post it up if anyone is interested.
  7. 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>"; } } } ?>
  8. Hi all, Thanks for your help on this. I have to leg it to work now so will return to my conundrum later. Cheers.
  9. OK, I swapped the positions of the $connection and $info variables in the query and was getting no errors but blank page. Then I added the code you kindly gave me and I get the same result - now I am just getting a blank page with no errors but the code is not working because the url is still the same. Maybe there is something wrong with how I am setting the $SESSION??
  10. Hi thanks, Yeah I thought it might be a problem with quotes in the SELECT too but tried putting them on and taking them off and am still getting the same result. Still getting the same two error messages when I try it.
  11. Thanks, took them off and now I am getting this error Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean I think this may be down to having the $connection in my query???? but when I remove it then I get two errors: mysqli_query() expects parameter 1 to be mysqli, string and mysqli_num_rows() expects parameter 1 to be mysqli_result, null given.
  12. Hi, I created the login page below. However, when I enter details on the form I am getting a blank page. I can't figure out what is wrong. Any help/suggestions really welcome. Thanks in advance. <html> <html lang="en"> <head> <meta charset="utf-8" /> <title>LOGIN FUNCTION</title> </head> <body> <?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)){ $info = "SELECT * FROM users WHERE username=$name and password=$password"; $return=@mysqli_query($connection, $info); $count_rows=@mysqli_num_rows($return); if($count_rows==1){ session_start(); if(isset($_SESSION['login'])){ header("Location:admin.php"); }else{ header("Location:login_fail.php"); } } } ?>
  13. OK, so now I am getting undefined index errors. I feel that I probably need to use $_GET somewhere here but I don't know where I am going wrong. From reading around it seems to me I should probably use isset somewhere? If this is very obvious please be patient... everybody starts somewhere. Any help or suggestions appreciated. <?php $connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') //Make a connection to the database or die(mysqli_connect_error("Could not connect to the server")); //If it doesn't connect give the error message $latestposts= mysqli_query($connection,"SELECT author_id, title FROM blogposts ORDER BY timeofpost DESC LIMIT 9");//Query the database while($displayposts=@mysqli_fetch_array($latestposts)){ //go and get the information echo "{$displayposts['title']}</br>"; echo "<a href='recentposts.php?id=content'>Read more</a></br>"; } ?> and then the other bit. It is running as far as the echo but I think I am missing something earlier? <?php $connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') or die(mysqli_connect_error("Could not connect to the server")); $content= mysqli_query($connection,"SELECT ('author_id''title''content') FROM blogposts ORDER BY timeofpost DESC LIMIT 9");; while($singlepage = mysqli_fetch_array($content)){ extract($singlepage); echo "<h3>{$singlepage['title']}</h3></br>"; echo "{$singlepage['content']}</br>"; echo "{$singlepage['timeofpost']}"; } ?>
  14. Hi all, I have written a piece of code querying the database to display the last ten posts from different users on a forum/blog. This is displaying the titles of the posts but I am unsure how to create a link (complete novice) to read the content of those posts. I get the syntax etc but can't really get my head around the target, if you catch my drift. I have created a new php file called recentposts but am unsure of how to take it from there. The code for both is below. The first piece of code works fine but when I click read more I am getting an undefined variable message on line 11 (I'm guessing there should be a link between the two pieces of code) and I am getting mysqli_fetch_array() expects parameter 1 to be mysqli_result on line 12. Any thoughts/hints/suggestions welcome. Thanks in advance. <?php $connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') //Make a connection to the database or die(mysqli_connect_error("Could not connect to the server")); //If it doesn't connect give the error message $latestposts= mysqli_query($connection,"SELECT author_id, title FROM blogposts ORDER BY timeofpost DESC LIMIT 9");//Query the database while($displayposts=@mysqli_fetch_array($latestposts)){ //go and get the information echo "{$displayposts['title']}</br>"; echo "<a href='recentposts.php'>Read more</a></br>"; } ?> And the recentposts.php is <?php $connection = @mysqli_connect('localhost','root','','BLOG_PROJECT') or die(mysqli_connect_error("Could not connect to the server")); $detail= mysqli_query($connection,"SELECT * FROM blogposts WHERE 'title', 'content', 'timeofpost' = $content"); while($singlepage = mysqli_fetch_array($connection,$detail)){ extract($singlepage); echo "<h2>{$singlepage['title']}</h2></br>"; echo "{$singlepage['content']}</br>"; echo "{$singlepage['timeofpost']}"; } ?>
  15. Ok, thanks for the tips. Seems this pretty much needs a rewrite so it's back to the drawing board I suppose. Thanks.
×
×
  • 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.