Blog Help

Taz

Seasoned Veteran
Joined
Mar 1, 2009
Messages
3,652
Reaction score
2
FP$
1,032
So I started a blog project just to help me out with learning php.
This is my post form
Code:
<form action="insert.php" method="post">
Title: <input type="text" name="title">
<br>
Post: <input type="text" name="post">
<br>
Author: <input type="text" name="author">
<br>
<input type="submit">
</form>

Insert.php
Code:
<?php
$con = mysqli_connect("localhost","test","","test");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$title = mysqli_real_escape_string($con, $_POST['title']);
$content = mysqli_real_escape_string($con, $_POST['post']);
$author = mysqli_real_escape_string($con, $_POST['author']);

$sql="INSERT INTO article (title, content, author)
VALUES ('$title', '$content', '$author')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

But when I try to display that information here:
Code:
<h1>Title: </h1> <?php echo $title; ?>
<h2>Content: </h1> <?php echo $content; ?>
<h3>Posted by: </h1> <?php echo $author; ?>
It doesn't work and I get this:
Code:
( ! ) Notice: Undefined variable: database in C:\wamp\www\test\index.php on line 14
Call Stack
#	Time	Memory	Function	Location
1	0.0000	241200	{main}( )	..\index.php:0

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\index.php on line 14
Call Stack
#	Time	Memory	Function	Location
1	0.0000	241200	{main}( )	..\index.php:0
2	0.0510	251104	mysqli_query ( )	..\index.php:14

( ! ) Notice: Undefined variable: database in C:\wamp\www\test\index.php on line 18
Call Stack
#	Time	Memory	Function	Location
1	0.0000	241200	{main}( )	..\index.php:0

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\index.php on line 18
Call Stack
#	Time	Memory	Function	Location
1	0.0000	241200	{main}( )	..\index.php:0
2	0.0570	251368	mysqli_query ( )	..\index.php:18

( ! ) Notice: Undefined variable: database in C:\wamp\www\test\index.php on line 20
Call Stack
#	Time	Memory	Function	Location
1	0.0000	241200	{main}( )	..\index.php:0

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\index.php on line 20
Call Stack
#	Time	Memory	Function	Location
1	0.0000	241200	{main}( )	..\index.php:0
2	0.0650	251504	mysqli_query ( )	..\index.php:20
The form works because when I looked at the database, the information was there. The problem is getting that information and displaying it in the right place, how can i fix that?

Just in case, this is my index:
Code:
<?php
include ('connect.php');
include ('header.php');

?>
<div id="container">
<div id="rightcol">

<form action="insert.php" method="post">
Title: <input type="text" name="title">
<br>
Post: <input type="text" name="post">
<br>
Author: <input type="text" name="author">
<br>
<input type="submit">
</form>

</div>


<div id="content">

<h1>Title: </h1> <?php echo $title; ?>
<h2>Content: </h1> <?php echo $content; ?>
<h3>Posted by: </h1> <?php echo $author; ?>

</div>
</div>

<?php
include "footer.php";

?>

</div>
<br /><br />-- Sat Aug 30, 2014 11:15 pm --<br /><br />The error changed to
Code:
( ! ) Notice: Undefined variable: title in C:\wamp\www\test\index.php on line 33
Call Stack
#	Time	Memory	Function	Location
1	0.0000	239144	{main}( )	..\index.php:0
Content:

( ! ) Notice: Undefined variable: content in C:\wamp\www\test\index.php on line 34
Call Stack
#	Time	Memory	Function	Location
1	0.0000	239144	{main}( )	..\index.php:0
Posted by:

( ! ) Notice: Undefined variable: author in C:\wamp\www\test\index.php on line 35
Call Stack
#	Time	Memory	Function	Location
1	0.0000	239144	{main}( )	..\index.php:0
 
Code:
<?php
include "footer.php";

?>

Remove the space so it looks like this:
Code:
<?php
include "footer.php";
?>

That should fix the second error... You have to look at what line # the error is telling you. If your editor doesn't have line #'s you can either count the lines yourself or download Notepad ++ (that's what I did).

Your first error dealt with this line:
Code:
Author: <input type="text" name="author">

You may want to learn valid HTML first cause that would help your PHP skills (trust me, they go hand in hand) as some of that invalid HTML you have could cause the issues.
Intro to HTML- http://helpmecode.club/topic/367-introduction-to-html/
That is a tutorial I wrote on my forum on valid HTML.
Intro to PHP- http://helpmecode.club/topic/405-introduction-to-php/
That is a tutorial I wrote on my forum on the beginnings to PHP.

You can post there and get some help as well. 🙂

EDIT/UPDATE: After looking at your error, it also could be that you are using MySQLi instead of regular ole' MySQL... you could try swapping those out. Though MySQLi should work, sometimes it doesn't.
 
Thanks for helping Taz, DudeThats Erin! It could be the MySQLi issue like you said at the end of your post, instead of using MySQL. I had that issue with a different website in the past. I also recommend Notepad ++ as well, I use it all the time, and being able to use it with different file types is awesome! 🙂
 
MySQL_ functions are deprecated! don't use them.

I'll message you on Skype.
 
Collin said:
Thanks for helping Taz, DudeThats Erin! It could be the MySQLi issue like you said at the end of your post, instead of using MySQL. I had that issue with a different website in the past. I also recommend Notepad ++ as well, I use it all the time, and being able to use it with different file types is awesome! 🙂

No problem. 🙂
 
Well I'm still having some trouble with those errors at different times, I don't have a definite fix for it yet.
 
I was trying to display the information retrieved from the database.
 
From what I can understand looking at your code, the bolded variables aren't actually ever set?

<h1>Title: </h1> <?php echo $title; ?>
<h2>Content: </h1> <?php echo $content; ?>
<h3>Posted by: </h1> <?php echo $author; ?>

You need to at least be setting them up, for example in PDO it looks like so:
Code:
<?
$stmt = $pdo->prepare("SELECT blah blah FROM table WHERE blah = :blah LIMIT blah");
$stmt->bindValue(":blah", $variable);
if ($stmt->execute()) {	
     foreach($stmt as $posts) { ?>
           <h1>Title: </h1> <?php echo <b>$posts['title'];</b>; ?>
           <h2>Content: </h1> <?php echo <b>$posts['content'];</b>; ?>
           <h3>Posted by: </h1> <?php echo <b>$posts['author'];</b>; ?>
      }
}
?>

I mean you don't even seem to be making any sort of SQL statement to fetch the blog posts in index.php. You've actually got to make a fetch to the database.

I think something like that is more what you are looking for, but that's in PDO, not MYSQLi. Hopefully it helps you understand what you need to be doing.
 
I don't know what I am doing. Haha I am getting tips from procedure and oop php so its like a big mix.
 
Back
Top Bottom