So I started a blog project just to help me out with learning php. This is my post form
<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 <?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: <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: ( ! ) 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
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:
<?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>