Can't insert values when form action="display.php"

I’m trying to insert values the user has typed from a form into table. When I enter the values into the form and submit the phpAdmin in XAMPP does not show the values in the table and gives me this message “MySQL returned an empty result set (i.e. zero rows)”. Both the database and table are created.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page </title>
    <link href="css/main.css" rel="stylesheet">
</head>
<body>
    <section>
 <header></header>   
 <main>
 <h3> Animals</h3>
 <h4> Enter your Favorite Animal</h4>
 <hr />
<form action="display.php" method="post" enctype="multipart/form-data" >

    <label for="">Animal Name: </label><br />
    <input type="text" name="animal_name" required placeholder="Type Animal Name"><br /><br />
    <label for="">Description: </label><br />
    <textarea cols="20" rows="9" name="comments"> </textarea><br /><br />
    <label for="">Image: </label><br />
    <input type="file" name="upload" id=""></input><br />
    <hr />
    <button type="submit" name="submit">Submit Info</button>
</form>
 </main>
</section>
</body>
</html>

<?php  
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host, $username, $password);
$sql = "CREATE DATABASE IF NOT EXISTS newsad_hw2;  ";
$result = mysqli_query($conn, $sql);

if(!$result){
    echo "Database creation failed";
}
else{
    echo "Connection established, database created";
}
$conn2 = mysqli_connect($host, $username, $password, "newsad_hw2");

$sql2 = "CREATE TABLE IF NOT EXISTS tbl_newsad (id INT PRIMARY KEY AUTO_INCREMENT, 
        animal_name VARCHAR(50) NOT NULL,
        comments VARCHAR(255) NOT NULL,
        photo BLOB NOT NULL);";
$query = mysqli_query($conn2, $sql2);

if(!$query){
    echo "Table creation failed";
}else{
    echo "Table created";
}


if(isset($_POST['sumbit']))
{
    

    $animal = $_POST['animal_name'];
    $comment = $_POST['comments'];
    $imageName =  $_FILES['upload']['name'];
    $tempName  =  $_FILES['upload']['tmp_name'];

    // need the name of the images folder
    $folderName = 'images/';
    // create instructions to direct images to the folder images
    move_uploaded_file( $tempName, $folderName.$imageName);


    $sql3 = "INSERT INTO 'tbl_newsad'('animal_name', 'comments', 'photo') 
    VALUES ('$animal','$comment','$imageName')";


    $run = mysqli_query($conn2 , $sql3);


}

?>

What is this line? SumBIT?
and

<button type="submit" name="submit">Submit Info</button>

NOT the same ! Ha!

Did not notice that. I have been looking at the is code for hours trying to figure out what I did wrong. Thanks

After making the change of the ‘submit’. The values are now being inserted. Here is my revised code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page </title>
    <link href="css/main.css" rel="stylesheet">
</head>
<body>
    <section>
 <header></header>   
 <main>
 <h3> Animals</h3>
 <h4> Enter your Favorite Animal</h4>
 <hr />
<form action="display.php" method="post" enctype="multipart/form-data" >

    <label for="">Animal Name: </label><br />
    <input type="text" name="animal_name" required placeholder="Type Animal Name"><br /><br />
    <label for="">Description: </label><br />
    <textarea cols="20" rows="9" name="comments"> </textarea><br /><br />
    <label for="">Image: </label><br />
    <input type="file" name="upload" id=""></input><br />
    <hr />
    <button type="submit" name="submit">Submit Info</button>
</form>
 </main>
</section>
</body>
</html>

<?php  
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host, $username, $password);
$sql = "CREATE DATABASE IF NOT EXISTS newsad_hw2;  ";
$result = mysqli_query($conn, $sql);

if(!$result){
    echo "Database creation failed";
}
else{
    echo "Connection established, database created";
}
$conn2 = mysqli_connect($host, $username, $password, "newsad_hw2");

$sql2 = "CREATE TABLE IF NOT EXISTS tbl_newsad (id INT PRIMARY KEY AUTO_INCREMENT, 
        animal_name VARCHAR(50) NOT NULL,
        comments VARCHAR(255) NOT NULL,
        photo BLOB NOT NULL);";
$query = mysqli_query($conn2, $sql2);

if(!$query){
    echo "Table creation failed";
}else{
    echo "Table created";
}

if(isset($_POST['submit']))
{
    $animal = $_POST['animal_name'];
    $comment = $_POST['comments'];
    $imageName =  $_FILES['upload']['name'];
    $tempName  =  $_FILES['upload']['tmp_name'];

    // need the name of the images folder
    $folderName = 'images/';
    // create instructions to direct images to the folder images
    move_uploaded_file( $tempName, $folderName.$imageName);


    $sql3 = "INSERT INTO `tbl_newsad`(`animal_name`, `comments`, `photo`) 
    VALUES ('$animal','$comment','$imageName');";

    $run = mysqli_query($conn2 , $sql3);


}


?>

Figured that was it. Obvious really. I find I have that problem a lot or a missing end brace } or something…

So, you might want to look into PREPARED-STATEMENTS. The type of INSERT you used is not used by most programmers these days because it is not secure. Most professionals are using PDO now instead of MySQLi. But, with either, you should at least use PREPARED-STATEMENTS.

Here is a link to MySQLi Prepared Statements for inserts that might help you.
MySQLi Prepared Statements
Or, to learn PDO, go here: PDO Done Right!

The values are not being inserted again. The only thing I changed is I created a new PHP page called display.php that will display the data from the table to a web page. When the remove display.php from the action form the values are inserted in the database.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page </title>
    <link href="css/main.css" rel="stylesheet">
</head>
<body>
    <section>
 <header></header>   
 <main>
 <h3> Animals</h3>
 <h4> Enter your Favorite Animal</h4>
 <hr />
<form action="display.php" method="post" enctype="multipart/form-data" >

    <label for="">Animal Name: </label><br />
    <input type="text" name="animal_name" required placeholder="Type Animal Name"><br /><br />
    <label for="">Description: </label><br />
    <textarea cols="20" rows="9" name="comments"> </textarea><br /><br />
    <label for="">Image: </label><br />
    <input type="file" name="upload" id=""></input><br />
    <hr />
    <button type="submit" name="submit">Submit Info</button>
</form>
 </main>
</section>
</body>
</html>

<?php  
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host, $username, $password);
$sql = "CREATE DATABASE IF NOT EXISTS newsad_hw2;  ";
$result = mysqli_query($conn, $sql);

if(!$result){
    echo "Database creation failed";
}
else{
    echo "Connection established, database created";
}
$conn2 = mysqli_connect($host, $username, $password, "newsad_hw2");

$sql2 = "CREATE TABLE IF NOT EXISTS tbl_newsad (id INT PRIMARY KEY AUTO_INCREMENT, 
        animal_name VARCHAR(50) NOT NULL,
        comments VARCHAR(255) NOT NULL,
        photo BLOB NOT NULL);";
        
$query = mysqli_query($conn2, $sql2);

if(!$query){
    echo "Table creation failed";
}else{
    echo "Table created";
}

if(isset($_POST['submit']))
{
    $animal = $_POST['animal_name'];
    $comment = $_POST['comments'];
    $imageName =  $_FILES['upload']['name'];
    $tempName  =  $_FILES['upload']['tmp_name'];

    // need the name of the images folder
    $folderName = 'images/';
    // create instructions to direct images to the folder images
    move_uploaded_file( $tempName, $folderName.$imageName);


    $sql3 = "INSERT INTO `tbl_newsad`(`animal_name`, `comments`, `photo`) 
    VALUES ('$animal','$comment','$imageName');";

    $run = mysqli_query($conn2 , $sql3);


}

?>

//display.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>List of Favorite Animals</title>
    <link href="css/display.css" rel="stylesheet">
</head>
<body>
  <h3> List of Current Favorite Animals</h3>  

<?php
$host = "localhost";
$username = "root";
$password = "";
$conn4 = mysqli_connect($host, $username, $password, "newsad_hw2");

$mysql = "SELECT * FROM tbl_newsad ;";

$result = mysqli_query($conn4, $mysql);
$check = mysqli_num_rows($result);
?>

<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Photo</th>
</tr>

<?php
if ($check > 0)
{
    while ($row = mysqli_fetch_array($result)){
        echo "<tr>"; 
         echo "<td>" .  $row['animal_name']  .  "</td>";
        echo "<td>" .  $row['comments']  .  "</td>";
        echo "<td> <img src='images/" .  $row['photo']. "'></td>";
        
        echo"</tr>";
    }
}

?>
</table>
</body>
</html>

Well, if you assign a page to the form for the action, as soon as the page is submitted, it switched to the other page. Therefore, the insert code is never processed. This is confusing to beginners. Two ways to fix it.

#1: Move your INSERT code into the beginning of the display.php page and process the insert before it shows the displayed results.

#2: Remove the display.php from the action, like action="#"; and change the page to display the results on the same page when posted.

The data input, insert and display can all be on the same page if you want to. It depends on the flow of the website and what you are attempting to do. Since it is a list of favorite animals and you plan to show them in a table, it can all be done on just one page. Make the first row of the table be the “add-new-animal” section and show all the rest below it or above it. For simple things like this list, one page is all you need and just post the form to itself… Do you understand that?

Okay figured it out. Did option #1

Nice! Always nice when you solve a programming puzzle! See you in your next post!

Sponsor our Newsletter | Privacy Policy | Terms of Service