0

I'm new to MySQL and as a learning project I'd like to make a recipe database. I'd like to the user to be able to enter ingredients through a simple HTML form but I'm stuck in how to label the form so that I can enter several ingredients into the database at once.

I'd like to do something like this:

<form method="post" action="insert.php">
Ingredient 1: <input type="text" name="ingredient"><br />
Ingredient 2: <input type="text" name="ingredient"><br />
Ingredient 3: <input type="text" name="ingredient"><br />
<input type="submit" value="Submit">
</form>

When I do this, I add rows to the table but they're all empty. I know it's got something to do with me using "ingredient" (the table value where I want to add the ingredient name) several times in the form, but I just don't know how to solve it.

I would absolutely love some input on how to make it work.

3 Answers 3

2

write it like

Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />

and when you will get the REQUEST array in php, you will actually get an array of names

like

$ing = $_POST['ingredient']; // $ing will be indexed array
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm. Thanks for your quick response! How would I configure my PHP page to send it on to the database for this to work? Right now the INSERT part looks like this: $name = mysqli_real_escape_string($con, $_POST['ingredient']); $sql="INSERT INTO ingredient (ingredient) VALUES ('$ingredient'), ('$ingredient'), ('$ingredient');";
You should run a loop for $_POST['ingredient'] this. b/c it will be an array. and write your query inside the loop.
0

You can't use same name for multiple textboxes like you have done. The last input box's value will overwrite all of the other ones' since they have the same name. Either you have to use different names for each input texts or define name as array like :

Ingredient 1: <input type="text" name="ingredient[]"><br />
Ingredient 2: <input type="text" name="ingredient[]"><br />
Ingredient 3: <input type="text" name="ingredient[]"><br />

So $_REQUEST['ingredient'] or $_POST['ingredient'] will be a normal PHP array from which you can get the value of each textbox like $_REQUEST['ingredient'][x] where x is some integer index, which is valid as long as count($_REQUEST['addCart']) > x.

Comments

0

Working Code :

index.html

<!DOCTYPE html>
<html>
<head>
<title> Recipes </title>
</head>
<body>
<form method="post" action="register.php">
Ingredient 1 : <input type="text" name="ing1"/><br/>
Ingredient 2 : <input type="text" name="ing2"/><br/>
Ingredient 3 : <input type="text" name="ing3"/><br/><br/>
<input type="submit" value="Enter" name="submit"/>
</form>
</body>
</html>

register.php

<?php

// coding to check database connection
$connection = mysqli_connect('localhost','root','adm','recipe');

/*
root -  username
adm -  passowrd
recipe - database name
*/

//checking whether submit button is clicked or not

if(isset($_POST['submit'])) 
{
   // Escaping special char & getting the values we entered in form through POST method
   $ing1 = mysqli_real_escape_string($connection,$_POST['ing1']);
   $ing2 = mysqli_real_escape_string($connection,$_POST['ing2']);
   $ing3 = mysqli_real_escape_string($connection,$_POST['ing3']);

      //data is table name

      $query = "INSERT into data VALUES('','$ing1','$ing2','$ing3')";
      $result = mysqli_query($connection,$query);
    echo "<p>Successfully Entered</p>"; 
    ?>

   <a href="index.html"> Click here to go to home </a>

<?
    }
?>

Create a database and name it as recipe and a table as data. Table data structure:

enter image description here

Output :

enter image description here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.