简体   繁体   English

MYSQL / PHP:通过HTML形式插入数据,相同的“输入名称”

[英]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. 我是MySQL的新手,作为一个学习项目,我想创建一个配方数据库。 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. 我希望用户能够通过一个简单的HTML表单输入配料,但是我仍然停留在如何标记表单的标签上,这样我就可以一次将几种配料输入数据库。

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. 我知道在表单中多次使用“ ingredient”(要在其中添加成分名称的表值)与我有关,但我只是不知道如何解决。

I would absolutely love some input on how to make it work. 我绝对会喜欢如何使它起作用的一些意见。

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 当您在php中获得REQUEST数组时,您实际上将获得一个名称数组

like 喜欢

$ing = $_POST['ingredient']; // $ing will be indexed array

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 . 因此, $_REQUEST['ingredient']$_POST['ingredient']将是一个普通的PHP数组,您可以从中获取每个文本框的值,例如$_REQUEST['ingredient'][x] ,其中x是某个整数索引,只要count($_REQUEST['addCart']) > x

Working Code : 工作代码:

index.html 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 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 . 创建一个数据库,并将其命名为recipe ,将表命名为data Table data structure: data结构:

在此处输入图片说明

Output : 输出:

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM