Home>

I want to create something like a bulletin board, manage posts in the DB, add and display them.

I want to satisfy the following conditions.
-Users can speak by entering their name and comment.
・ Manage the user's past statements in a database.
・ Past utterances of all users can be viewed in a list, and at least three of "name, comment, utterance date" are displayed on one line.
・ User names can be up to 20 characters long. If the name is longer than that, an error message is displayed so that the user cannot speak.
-User comments can be up to 100 characters long, and if it exceeds that, an error message is displayed so that the user cannot speak.
-If either or both of the user's name and comment are not entered, an error message is displayed so that the user cannot speak.

Error message
I feel like I can't get data with POST.
The error is not displayed and the cause is unknown.
Applicable source code
<? php
$name = "";
$comment = "";
$data = array ();
$error = array ();
$log = date ('Y-m-d H: i: s');
$host = "IP address";
$username = "username";
$passwd = "password";
$dbname = "DB name";
// Connect to sql. Assign to variable
$link = mysqli_connect ($host, $username, $passwd, $dbname);
if ($link) {
    // Preventing garbled characters
    mysqli_set_charset ($link, 'utf8');
    // Execute when transmission data is POST
    if ($_SERVER ['REQUEST_METHOD'] === 'post') {
        // Substitute transmission data for name
        if (isset ($_ POST ['name']) === TRUE) {
            $name = $_POST ['name'];
        }
        // If the send data name is incomplete, assign it to the array errorif (mb_strlen ($name)>20) {
            $error [] = 'Please enter a name within 20 characters. ';
        } else if (mb_strlen ($name) === 0) {
            $error [] = 'Please enter a name';
        }
        if (isset ($_ POST ['comment']) === TRUE) {
            $comment = $_POST ['comment'];
        }
        // If the send data comment is incomplete, assign it to the array error
        if (mb_strlen ($comment)>100) {
            $error [] = 'Please enter a comment using 100 characters or less. ';
        } else if (mb_strlen ($comment) === 0) {
            $error [] = 'Please enter a comment. ';
        }
        var_dump ($name);
        var_dump ($comment);
        // Get the number of array $error.
        if (count ($error) === 0) {
            $query = "INSERT INTO bulletion_bord (user_name, user_comment, data) VALUES ('$name', '$comment', '$log')";
            $result = mysqli_query ($link, $query);
            if ($result === FALSE) {
                $error [] = 'Registration failed. ';
            }
        }
    }
    $query = "SELECT * FROM bulletion_bord";
    $result = mysqli_query ($link, $query);
    while ($row = mysqli_fetch_array ($result)) {
        $data [] = $row;
    }
    mysqli_free_result ($result);
    mysqli_close ($link);
} else {
    $error [] = 'Connection failure';
}?>
<! DOCTYPE html>
<html lang = "en">
    <head>
        <meta chaarset = "utf-8">
        <title>Assignment (Intermediate)</title>
    </head>
    <body>
         Short Message Board 
        <!-Display error->
        <? php foreach ($error as $value) {?>
                <p><? php print $value;?></p>
        <? php}?>
        <form method = "post">
            Name:<input type = "text" name = "name" size = "15" value = "">
            Comment:<input type = "text" name = "comment" size = "40" value = "">
            <input type = "submit" value = "post">
        </form>
        <ul>
            <? php foreach ($data as $value) {?>
                <li>
                <? php print $value ['user_name'];?>
                <? php print $value ['user_comment'];?>
                <? php print $value ['data'];?>
                </li>
            <? php}?>
        </ul>
    </body>
</html>

Check if there is data using var_dump ().
I thought that it was not done from the processing in IF ($link).

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

      

    I thought that it was not done from the processing in IF ($link).

    If it is

    , check the value of $link.
    Use var_export () to check the conditions.

    Set up a debugging environment and step by step. Increase learning efficiency