Camen
Camen

Reputation: 375

MYSQL/PHP: Inserting data via HTML form, same "input name"

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.

Upvotes: 0

Views: 4524

Answers (3)

Yuva Raj
Yuva Raj

Reputation: 3881

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

Upvotes: 0

Jenz
Jenz

Reputation: 8369

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.

Upvotes: 0

developerCK
developerCK

Reputation: 4506

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

Upvotes: 2

Related Questions