简体   繁体   中英

PHP $_POST empty — not working for local server using MAMP

So currently, this is the code I have

index.php:

        <form action="insert.php" method="post">
            Comments:
            <input type="text" name="comment">
            <input type="submit">
        </form>

insert.php:

<?php

include ('index.php');

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */

$link = mysqli_connect("localhost", "x", "", "comment_schema");

// Check connection
if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$comment = mysqli_real_escape_string($link, $_POST["comment"]);
$sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')";

// attempt insert query execution
if(mysqli_query($link, $sql)){
   echo $comment;
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

When I do echo $comment, nothing gets printed out. However, if I do something like echo "hi" it works. I think for some reason the $_POST is not being recognized. Any suggestions to make this work or if I'm doing it wrong.

My goal is to take a user input and insert into a database on phpmyadmin. Currently, it is able to insert, however it inserts an empty value. I only have two columns in my database. An ID and a Comment column. The ID is auto incremented. The comment is what I get from the user.

Check Once what is your data type of comment in database.(I prefer Varchar() ). Try this as it is:-

<form action="insert.php" method="POST">
                Comments:
                <input type="text" name="comment"/>
                <input type="submit" name="submit" value="submit">
            </form>

<?php

include ('index.php');

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;


$link = mysqli_connect("localhost", "x", "", "comment_schema");


if($link === false){
   die("ERROR: Could not connect. " . mysqli_connect_error());
}


$comment = mysqli_real_escape_string($link, $_POST["comment"]);
$sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')";


if(mysqli_query($link, $sql)){
   echo $comment;
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

try this

<?php
    $user = 'x';
    $password = '';
    $db = 'comment_schema';
    $host = 'localhost';

    $link = mysqli_connect($host, $user, $password, $db);

    if($link === false){
       die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    // Escape user inputs for security
    $comment = mysqli_real_escape_string($link, $_POST["comment"]);
    $sql = "INSERT INTO parentComment(ID, Comment) VALUES(NULL,'$comment')";

// attempt insert query execution
if(mysqli_query($link, $sql)){
   echo $comment;
} else{
 echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

Some debugging suggestions:

  • var_dump($_POST); // before mysqli_real_escape_string
  • var_dump($comment); // after mysqli_real_escape_string

mysqli api may not work well! Fix the query like

  • $sql = "INSERT INTO parentComment(ID, Comment) VALUES('','".$comment."')";
  • echo $sql; // check your sql syntax
  • echo mysqli_error($link); // do you have error
  • Look at your .htaccess file see if you have <Limit POST> tag
  • Remove this line include ('index.php'); . Supposedly, these two files are in one folder. So just run index.php . Tried your code without that line and it worked for me.

Use below code:-

include ('index.php');

$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;    

$link = mysqli_connect("localhost", "x", "", "comment_schema");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if(!empty($_POST["comment"])){
    $comment = mysqli_real_escape_string($link, $_POST["comment"]);    
    // Escape user inputs for security
    $sql = "INSERT INTO parentComment(ID, Comment) VALUES('','$comment')";
    $result = mysqli_query($link, $sql);
    // attempt insert query execution
    if($result){
       echo $comment;
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
    // close connection
    mysqli_close($link);

}else{
    die('comment is not set or not containing valid value');
}

Hope it will help you :-)

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