MyBB Community Forums

Full Version: PHP Blog Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\test\index.php on line 14
 Call Stack
 #    Time    Memory    Function    Location
 1    0.0000    240560    {main}( )    ..\index.php:0
 2    0.0590    251064    mysqli_query ( )    ..\index.php:14
 
 ( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\test\index.php on line 16
 Call Stack
 #    Time    Memory    Function    Location
 1    0.0000    240560    {main}( )    ..\index.php:0
 2    0.0620    251232    mysqli_query ( )    ..\index.php:16
 
 ( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\test\index.php on line 18
 Call Stack
 #    Time    Memory    Function    Location
 1    0.0000    240560    {main}( )    ..\index.php:0
 2    0.0660    251496    mysqli_query ( )    ..\index.php:18

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>
mysqli_query is for querying and inserting. You're using it for mysqli_connect. Change to connect and that should fix it.
Alright I did, thanks.