Jump to content

Recommended Posts

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 by Lacy9265
Link to comment
https://forums.phpfreaks.com/topic/314679-how-to-insert-values-from-form-to-table/
Share on other sites

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);




}



?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.