Home>

I want to register my input in the database and display it, but it doesn't work.

Error message
Notice: Undefined variable: sql in C: \ xampp \ htdocs \ graduate \ M_class1.php on line 87
Warning: mysqli_query (): Empty query in C: \ xampp \ htdocs \ graduate \ M_class1.php on line 87
Applicable source code

M_register.php (input form)

<html>
    <head>
        <meta http-equiv = "Content-Type" content = "text/html;charset =" UTF-8 ">
        <title>Assign assignment</title>
    </head>
    <body>
     Please select an assignment to register. 
     Back 
    <form action = "M_class1.php" method = "POST">
    <table border = "1">
        <tr>
            <th width = "100" nowrap>FY</th>
            <th width = "100" nowrap>Class</th>
            <th width = "100" nowrap>times</th>
            <th width = "100" nowrap>File format</th>
            <th width = "100" nowrap>Issue Genre</th>
            <th width = "100" nowrap>Difficulty</th>
        </tr>
        <tr>
        <th width = "100" nowrap>
        <select name = "year">
            <option value = "">Please select</option>
            <option value = "2020">2020</option>
            <option value = "2019">2019</option>
        </select>
        <th width = "100" nowrap>
        <select name = "class">
            <option value = "">Please select</option>
            <option value = "1">1</option>
            <option value = "2">2</option>
            <option value = "3">3</option>
            <option value = "4">4</option>
            <option value = "5">5</option>
            <option value = "6">6</option>
            <option value = "7">7</option>
            <option value = "8">8</option>
            <option value = "9">9</option>
        </select>
        <th width = "100" nowrap>
        <select name = "number">
            <option value = "">Please select</option>
            <option value = "1st">1st</option>
            <option value = "2nd-3">2nd-3</option>
            <option value = "4-5">4-5</option>
            <option value = "6-7-7">6-7-7</option>
            <option value = "8-9">8-9</option>
            <option value = "10th">10th</option>
            <option value = "11th-12th">11th-12th</option>
            <option value = "13-14">13-14</option>
            <option value = "15th">15th</option>
        </select>
        <th width = "100" nowrap>
        <select name = "mso">
            <option value = "">Please select</option>
            <option value = "Word">Word</option>
            <option value = "Excel">Excel</option><option value = "PowerPoint">PowerPoint</option>
            <option value = "Other">Other</option>
        </select>
        <th width = "100" nowrap>
        <select name = "genre">
            <option value = "">Please select</option>
            <option value = "attendance assignment">attendance assignment</option>
            <option value = "Basic assignment">Basic assignment</option>
            <option value = "Development Issues">Development Issues</option>
        </select>
        <th width = "100" nowrap>
        <select name = "level">
            <option value = "">Please select</option>
            <option value = "easy">easy</option>
            <option value = "Normal">Normal</option>
            <option value = "difficult">difficult</option>
        </select>
        </tr>
    </table>
    <br>
    <p>Title of the assignment<br>
        <input type = "text" name = "practice" value = "" size = "50">
        <p>Deadline<br>
        <input type = "text" name = "time" value = "" size = "50">
        <p>Included content (target)<br>
        <input type = "text" name = "purpose" value = "" size = "200">
        <p>Samples and other points to consider<br>
        <input type = "text" name = "other" value = "" size = "200">
        <p>Agenda file<br>
        <input type = "text" name = "file" value = "" size = "50">
        <p><input type = "submit" value = "Registering an assignment"></p>
    </form>
    </body>
</html>


M_class1.php (display screen)

<? php
    //Variable initialization
    $error = "";
    $new_year = "";
    $new_class = "";
    $new_number = "";
    $new_mso = "";
    $new_genre = "";
    $new_level = "";
    $new_practice = "";
    $new_time = "";
    $new_purpose = "";
    $new_other = "";
    $new_file = "";
    // When POSTed
    if ($_ SERVER ["REQUEST_METHOD"] == "POST") {
        $link = mysqli_connect ("localhost", "root", "2019minato");
        mysqli_query ($link, "set names UTF8");
        mysqli_select_db ($link, "test");
        // ■ New addition processing
    if (isset ($_ POST ["submit_add"])) {
        $new_year = htmlspecialchars ($_ POST ["new_year"],

 ENT_QUOTES, 'UTF-8');
        $new_class = htmlspecialchars ($_ POST ["new_class"],

 ENT_QUOTES, 'UTF-8');$new_number = htmlspecialchars ($_ POST ["new_number"],

 ENT_QUOTES, 'UTF-8');
        $new_mso = htmlspecialchars ($_ POST ["new_mso"],

 ENT_QUOTES, 'UTF-8');
        $new_genre = htmlspecialchars ($_ POST ["new_genre"],

 ENT_QUOTES, 'UTF-8');
        $new_level = htmlspecialchars ($_ POST ["new_level"],

 ENT_QUOTES, 'UTF-8');
        $new_practice = htmlspecialchars ($_ POST ["new_practice"],

 ENT_QUOTES, 'UTF-8');
        $new_time = htmlspecialchars ($_ POST ["new_time"],

 ENT_QUOTES, 'UTF-8');
        $new_purpose = htmlspecialchars ($_ POST ["new_purpose"],

 ENT_QUOTES, 'UTF-8');
        $new_other = htmlspecialchars ($_ POST ["new_other"],

 ENT_QUOTES, 'UTF-8');
        $new_file = htmlspecialchars ($_ POST ["new_file"],

 ENT_QUOTES, 'UTF-8');
        if ($error == "") {
            $sql = "INSERT INTO class1 VALUES ('$new_year', '$new_class',' $new_number ',' $new_mso ',' $new_genre ',' $new_level ',' $new_practice ',' $new_time ',' $new_purpose ',' $new_other ',' $new_file ') ";
        }
    }
    // ■ sql statement processing
    if ($error == "") {
        mysqli_query ($link, $sql);
        $new_year = "";
        $new_class = "";
        $new_number = "";
        $new_mso = "";
        $new_genre = "";
        $new_level = "";
        $new_practice = "";
        $new_time = "";
        $new_purpose = "";
        $new_other = "";
        $new_file = "";
    }
    }
?>

<html>
    <head>
        <meta http-equiv = "Content-Type" charset = "UTF-8">
        <title>Class 1</title>
    </head>
    <body>
     Class 1 
     Back 
     First lecture 
    <? php
    $link = mysqli_connect ('localhost', 'root', '○○○○');
    if (! $link) {
        die ('Connection failure.'. mysqli_error ());
    }$db_selected = mysqli_select_db ($link, 'test');
    if (! $db_selected) {
        die ('Database selection failure.'. mysqli_error ());
    }
    mysqli_query ($link, 'SET NAMES utf8');
    $result1 = mysqli_query ($link, 'SELECT id, year, class, number, mso, genre, level, practice, time, purpose, other, file FROM class1 WHERE');
        while ($row = mysqli_fetch_assoc ($result1)) {
            print ('<table border = "1">');
                print ('<tr>');
                print ('<th width = "100" nowrap>'). ('FY'). ('</th>');
                print ('<th width = "100" nowrap>'). ('class'). ('</th>');
                print ('<th width = "100" nowrap>'). ('twice'). ('</th>');
                print ('<th width = "100" nowrap>'). ('file format'). ('</th>');
                print ('<th width = "100" nowrap>'). ('Issue Genre'). ('</th>');
                print ('<th width = "100" nowrap>'). ('Difficulty'). ('</th>');
                print ('</tr>');
                print ('<tr>');
                print ('<td width = "100" nowrap>'). ($row ['year']). ('</td>');
                print ('<td width = "100" nowrap>'). ($row ['class']). ('</td>');
                print ('<td width = "100" nowrap>'). ($row ['number']). ('</td>');
                print ('<td width = "100" nowrap>'). ($row ['mso']). ('</td>');
                print ('<td width = "100" nowrap>'). ($row ['genre']). ('</td>');
                print ('<td width = "100" nowrap>'). ($row ['level']). ('</td>');
                print ('</tr>');
            print ('</table>');
            print ('<table border = "1">');
                print ('<tr>');
                print ('<th width = "100" nowrap>'). ('Title title'). ('</th>');
                print ('<th width = "100" nowrap>'). ('deadline'). ('</th>');
                print ('<th width = "250" nowrap>'). ('Included content'). ('</th>');
                print ('<th width = "200" nowrap>'). ('Other notes'). ('</th>');
                print ('<th width = "180" nowrap>'). ('file'). ('</th>');
                print ('</tr>');
                print ('<td width = "200" nowrap>'). ($row ['practice']). ('</td>');
                print ('<td width = "100" nowrap>'). ($row ['time']). ('</td>');
                print ('<td width = "250" nowrap>'). ($row ['purpose']). ('</td>');
                print ('<td width = "200" nowrap>'). ($row ['other']). ('</td>');
                print ('<td width = "180" nowrap>'). ($row ['file']). ('</td>');
                print ('</tr>');
            print ('</table>');
        print ('<br>');
        }
        $close_flag = mysqli_close ($link);
    ?>
     2nd-3 lectures 
     The 4th-5th lecture 
     The 6th-7th Lecture 
     8th-9th lectures 
     The 10th lecture 
     The 11th-12th Lecture 
     The 13th-14th Lecture 
     The 15th lecture 
    </body>
</html>

Please describe what you tried for the problem here.

Supplemental information (FW/tool version etc.)

There is a table called class1, and the contents are empty. Is it the cause?


I would like to know where and how to do it. Thank you.

  • Answer # 1

    By the way, htmlspecialchars the value received in post and insert it into DB
    This is one of the most difficult things to do.
    Please register the received data as it is in the DB
    (Except for cases such as passwords that require irreversible security)

    In order to do so, it is necessary to prepare with mysqli even if you use mysqli

  • Answer # 2

    As shown in the error message

      

    Notice: Undefined variable: sql
      Warning: mysqli_query (): Empty query

    How to read and deal with error messages, search and question principles

    Check whether the condition for defining the variable $sql is satisfied.

    I feel like it's pretty verbose.

      

    There is a table called class1, and the contents are empty. Is it the cause?

    By the way, this is not relevant. As an error, it is caused by using a variable that is not defined as a required argument.

    If possible, use PDO instead of mysqli.

    Summary when connecting to a database with PHP

  • Answer # 3

    For now, let's name the button submit_add.

Related articles