Lacy9265 Posted April 7, 2022 Share Posted April 7, 2022 (edited) 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)". <!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 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); } ?> Edited April 7, 2022 by Lacy9265 Quote Link to comment https://forums.phpfreaks.com/topic/314679-how-to-insert-values-from-form-to-table/ Share on other sites More sharing options...
Lacy9265 Posted April 7, 2022 Author Share Posted April 7, 2022 New 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/314679-how-to-insert-values-from-form-to-table/#findComment-1595119 Share on other sites More sharing options...
Barand Posted April 7, 2022 Share Posted April 7, 2022 Start by trying to find out if and why any of your queries are failing. Put this line of code before you create your database connection... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); Quote Link to comment https://forums.phpfreaks.com/topic/314679-how-to-insert-values-from-form-to-table/#findComment-1595129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.