skip to Main Content

I am going to start this off by saying — yes I know there are other links similar to this and topics similar to this and I have read all of them and incorporated them into my code. However, I cannot figure it out and have tried everything I can.

Basically my goal is to take a users input from an html form called socialmedia.html:

<html>
<body>
<h1> Pulse submission page </h1><br>
<form action="action.php" method="post">
Title: <input type="text" name="posttitle"><br><br>
Content: <input type="text" name="content"><br><br>
<input type="submit">
</form>

</body>
</html>

and then send it to a php file called action.php:

<?php
$mysqli = new mysqli("DB HOST IP", "USER", "PASS", "DB NAME");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "n";



$posttitle = $_POST["posttitle"];
$content = $_POST["content"];

if(isset($_POST['submit'])){
$sql = "INSERT INTO `posts` (posttitle, content) VALUES ('$posttitle', '$content')";
echo 'post added to database';
}
if($sql){
echo 'success';
}
else{
echo 'failure';
}

$sql = "SELECT * FROM `posts`";

$res = $mysqli->query($sql);

if($res->num_rows > 0){
 while($row = $res->fetch_assoc()){
 echo "ID". $row["id"]. "<br/>";
 echo "Title". $row["posttitle"]. "<br/>";
 echo "Content". $row["content"]. "<br/>";
 }
}
else
{
 echo "No Record Found!";
}
?>

This file is SUPPOSED to insert the user’s form values into the table posts:
this is the table posts

and then print the whole table to a webpage– action.php this is what it prints (with the error checks and all):

this is the page, I blurred out the IP

NOTE: I manually inserted the first title and content to see if the code could read from the database (which it can)

honestly, I do not know where I went wrong and I have die extensive research at this point. It’s probably going to end up being a syntax error and I’m gonna be kicking myself. It could have something to do with me using a Godaddy server and the phpMyAdmin and database being through there. I am using mysqli instead of PDO because PLESK and Godaddy do not support PDO yet.

3

Answers


  1. several things to get you started
    1) missing quote after PASS

     mysqli("DB HOST IP", "USER", "PASS, "DB NAME");
    

    2) you are not executing your INSERT query, missing $mysqli->query($sql);

    if(isset($_POST['submit'])){
    $sql = "INSERT INTO `posts` (posttitle, content) VALUES ('$posttitle', 
    '$content')";
    echo 'post added to database';
    }
    
    Login or Signup to reply.
  2. <input type="submit" name="submit" /> try with this

    if(isset($_POST['submit'])){
       $sql = "INSERT INTO `posts` (posttitle, content) VALUES ('$posttitle', '$content')";
       $save = $mysqli->query($sql);
    
        if($save)
            echo 'success';
        else
            echo 'failure';
    }
    
    Login or Signup to reply.
  3. You have to give name of the submit butto as

    input type=”submit” name=”submit”

    "INSERT INTO  posts (posttitle, content) VALUES ('$posttitle', '$content')"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search