简体   繁体   中英

html text form with php isn't inserting into MySQL server

输出+代码

I am writing code that produces a <textarea> for a user to input something but it isn't giving me any output besides "please include some content" which is what it means to do if there is nothing in the box. This appears even if there is content in the <textarea> and won't even say "post fail" which is what it is meant to do if it can't insert into the database.

I am asking if anybody can see if there is something I have neglected to include, or if there is something that is wrong with my code.

<?php
session_start();
require('connect.php');
if(@$_SESSION["name"]){
    //echo "welcome ".$_SESSION['name'];
 ?>
  <html>
 <link rel="stylesheet" type="text/css" href="styles.css" />
<head>
<title> Welcome to faecesbook</title>
</head>
<?php include('header.php'); ?>
    <form action="post.php" method="POST">
        <br / >
        <br / >
        <br / >
        <br / >
    <center>
        <br/> 
        <br/>
        Type your post here:<br/>(160CharLimit)<br/>
        <textarea style="resize: none;  width: 800px; height: 100px;" name="con" maxlength="160">
       </textarea>
                     <br />
        <input type="submit" name="submit" value="Post" style="width: 800px;" >
    </center>   
    </form>
<body>
</body>
</html>

<?php       
$content = @$_POST['con'];
$post_date = date("d-m-y");


if(isset($_POST['submit'])){
    if($content){
        if($query = mysqli_query($conn, "INSERT INTO post(`postID`, `userID`   , `post_date` , `in_reply_to`, `postContent` ) 
        VALUES ('','".$_SESSION["userID"]."','".$post_date."','','".$content."')") )
            echo "post successful";
        }else{
            echo "post fail";
}   

    }else{
        echo "Please include some content";
    }
  }

  ?>

You have a missing braces in your code

if(@$_SESSION["name"]){
    //echo "welcome ".$_SESSION['name'];

Should read

if(@$_SESSION["name"]){
    //echo "welcome ".$_SESSION['name'];
}

And

if($query = mysqli_query($conn, "INSERT INTO post(`postID`, `userID`   , `post_date` , `in_reply_to`, `postContent` )
    VALUES ('','".$_SESSION["userID"]."','".$post_date."','','".$content."')") ) // <--- here
            echo "post successful";
        }else{
            echo "post fail";
}   

Making it readable also helps reduce errors

$query = mysqli_query($conn, "INSERT INTO post(`postID`, `userID`   , `post_date` , `in_reply_to`, `postContent` )
    VALUES ('','".$_SESSION["userID"]."','".$post_date."','','".$content."')");

if ($query){
    echo "post successful";
}else{
    echo "post fail";
}   

I see your code sends submited data to another page, i checked it through print_r($_POST)

I changed <form action="post.php" method="POST"> to <form action="" method="POST"> and tried and it was working, in case if the code you submited here is not "post.php" do this.

So it means someting is wrong with your insert query. So try this PDO way of inserting data.I thought of suggesting you the following easy pdo insert

    $dbhost = "localhost";
    $dbname = "mydatabase";
    $dbusername = "root";
    $dbpassword = "mypppasss";
    //connection string
    $link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);

Inside the if($content) put the following code and try

$statement = $link->prepare("INSERT INTO post(userID,post_date,postContent)
    VALUES(:inp1,:inp2,:inp3,:inp4)");
$statement->execute(array(
    inp1=>$_SESSION["userID"],
    inp2=>$post_date,
    inp4=>$content
));

EDITED

Add this code to the form submitted page to see the posted data for debugging.

if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';
}

EDITED

Note:@ is used to hide errors, prevent it from displaying error messages. that doesn't mean there is no error. Remove @ for debugging

EDITED Change your whole insert query part to this and try

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO post(userID,post_date,in_reply_to,postContent)
VALUES (".$_SESSION['userID'].",".$post_date.",'',".$content.")";

if (mysqli_query($conn, $sql)) {
    echo "inserted";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

Try this code. It should work.

<?php
session_start();
require('connect.php');
if(@$_SESSION["name"]){
    //echo "welcome ".$_SESSION['name'];
 ?>
  <html>
 <link rel="stylesheet" type="text/css" href="styles.css" />
<head>
<title> Welcome to faecesbook</title>
</head>
<?php include('header.php'); ?>
    <form action="post.php" method="POST">
        <br / >
        <br / >
        <br / >
        <br / >
    <center>
        <br/> 
        <br/>
        Type your post here:<br/>(160CharLimit)<br/>
        <textarea style="resize: none;  width: 800px; height: 100px;" name="con" maxlength="160">
       </textarea>
                     <br />
        <input type="submit" name="submit" value="Post" style="width: 800px;" >
    </center>   
    </form>
<body>
</body>
</html>

<?php       
$post_date = date("d-m-y");
if(isset($_POST['submit'])){
    if(isset($_POST['con']) && $_POST['con'] != ''){
           $content = @$_POST['con'];
            if($query = mysqli_query($conn, "INSERT INTO post(`postID`, `userID`   , `post_date` , `in_reply_to`, `postContent` ) 
            VALUES ('','".$_SESSION["userID"]."','".$post_date."','','".$content."')") )
            echo "post successful";
    }else{
            echo "post fail";
    }   

}else{
        echo "Please include some content";
}
}

  ?>

This is what was wrong. i had two set of <?php ?> , the first one in the code included require('connect.php'); within it, and the second one required it also.

<?php       
require('connect.php'); <<<<<------ NEEDED TO ADD THIS
$content = @$_POST['con'];
$post_date = date("d-m-y");
$userID = mysqli_query($conn,"SELECT userID FROM users WHERE name = '".$_SESSION['name']."'");

if(isset($_POST['submit'])){
    if($content){
        if($query = mysqli_query($conn, "INSERT INTO post(`postID`, `userID` , `post_date` , `in_reply_to`, `postContent` ) 
        VALUES ('','".$userID."','".$post_date."','','".$content."')") )
            echo "post successful";
        }else{
            echo "post fail";
}   

    }else{
        echo "Please include some content";
}
}

?>

So thats what i think the offending piece of code was. And it now will give me error saying the SQLI query was unnsuccessful, which means it is at least attempting that part, whereas before it was not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM