使用PHP从HTML表单输入数据库中的多个值

时间:2022-09-25 15:47:43

I'm trying to add multiple values into a database using PHP from an HTML. However, I can't just refer to the name attribute of the HTML form because each field in the form is generated by a PHP script. I've tried Googling around, but since I don't exactly know what I'm looking for, my search has been futile.

我正在尝试使用HTML中的PHP将多个值添加到数据库中。但是,我不能只引用HTML表单的name属性,因为表单中的每个字段都是由PHP脚本生成的。我试过谷歌搜索,但由于我不知道我在寻找什么,我的搜索是徒劳的。

Here's the bit of code that I use to generate the HTML form:

这是我用来生成HTML表单的一些代码:

<form action="input_points.php" method="post">
<?php
    while($row = mysql_fetch_array($result)) {
        echo $row['Name'] . ' <input type="text" name="userpoints">';
    }
?>
<button type="submit" name="add_points">Add Points </button>
</form>

I don't know what names are currently in the directory so I need this piece of php to determine what names are in the database. Afterwards, I want to have a bunch of boxes for people to input points (hence the form). I'm having trouble figuring out how to link the particular text box with the user.

我不知道目录中的名称是什么,所以我需要这段PHP来确定数据库中的名称。之后,我希望有一堆盒子供人们输入点(因此形式)。我无法弄清楚如何将特定文本框与用户链接。

For example, if I have a text box for Bob, how would I link up the input text field that contains the number of points Bob earns with Bob's entry in the database?

例如,如果我有一个Bob的文本框,我如何链接输入文本字段,其中包含Bob获取的点数与Bob在数据库中的条目?

I know you can do this with regular form fields:

我知道您可以使用常规表单字段执行此操作:

$userpoints = $_POST['userpoints'];

UPDATE members SET points = $userpoints where $user = "Bob";

But since I have multiple users, how do I link up the correct database entry with the right user? Also, how would I determine which boxes are empty and which boxes are updated with a value?

但由于我有多个用户,如何将正确的数据库条目与正确的用户链接?另外,我如何确定哪些框为空以及哪些框用值更新?

4 个解决方案

#1


0  

If you want to update multiple filed then are using array

如果要更新多个字段,则使用数组

Please changes some code

请更改一些代码

<form action="input_points.php" method="post">
<?php
    $userCount=mysql_num_rows($result);
    echo '<input type="hidden" name="userCount" value="' .$userCount. '">';
    while($row = mysql_fetch_array($result)) {
        echo '<input type="hidden" name="userid[]" value="' .$row['id']. '">'; //Give the uniq id
        echo $row['Name'] . ' <input type="text" name="userpoints[]">';
    }
?>
<button type="submit" name="add_points">Add Points </button>
</form>

PHP Code -

PHP代码 -

$userCount = $_POST['userCount'];
   for($i=1; $i=$userCount; $i++){
        $userpoints = $_POST['userpoints'];
        $userid = $_POST['userid'];
        //UPDATE members SET points = $userpoints where $user = $userid;
        //YOUR CODE HERE
   }

#2


0  

The update you're trying to do is not safe unless you treat values to prevent SQL injection... but if you really want it, instead of mysql_fetch_array(), try using mysql_fetch_assoc().

除非您将值视为阻止SQL注入,否则您尝试执行的更新并不安全...但如果您确实需要它,而不是mysql_fetch_array(),请尝试使用mysql_fetch_assoc()。

Using mysql_fetch_assoc() you can extract the keys (database field names) with array_keys(). The keys will be your the name property of your form fields and the values of will be the fields' values.

使用mysql_fetch_assoc(),您可以使用array_keys()提取密钥(数据库字段名称)。键将是表单字段的name属性,值将是字段的值。

Hope it helps.

希望能帮助到你。

#3


0  

You can use an array to store all the data that you need and add a hidden field that contains the missing data:

您可以使用数组存储所需的所有数据,并添加包含缺失数据的隐藏字段:

<form action="input_points.php" method="post">
<?php
    for($i=0; $row = mysql_fetch_array($result); $i++ ) {
        echo ' <input type="hidden" name="user[0]['name'] value ='". $row['name'] ."'">';
        echo $row['Name'] . ' <input type="text" name="user[$i]['points'] ">';
    }
?>
<button type="submit" name="add_points">Add Points </button>
</form>

#4


0  

Problem when you hit submit userpoints contain only the last value previous all values are overwritten solution name="userpoints" must be different each time why not you define it in database and then fetch it just like you fetch $row['Name']?

当你点击提交用户点时出现问题只包含最后一个值以前所有值都被覆盖解决方案名称=“userpoints”每次都必须不同,为什么不在数据库中定义它然后像获取$ row ['Name']一样获取它?

#1


0  

If you want to update multiple filed then are using array

如果要更新多个字段,则使用数组

Please changes some code

请更改一些代码

<form action="input_points.php" method="post">
<?php
    $userCount=mysql_num_rows($result);
    echo '<input type="hidden" name="userCount" value="' .$userCount. '">';
    while($row = mysql_fetch_array($result)) {
        echo '<input type="hidden" name="userid[]" value="' .$row['id']. '">'; //Give the uniq id
        echo $row['Name'] . ' <input type="text" name="userpoints[]">';
    }
?>
<button type="submit" name="add_points">Add Points </button>
</form>

PHP Code -

PHP代码 -

$userCount = $_POST['userCount'];
   for($i=1; $i=$userCount; $i++){
        $userpoints = $_POST['userpoints'];
        $userid = $_POST['userid'];
        //UPDATE members SET points = $userpoints where $user = $userid;
        //YOUR CODE HERE
   }

#2


0  

The update you're trying to do is not safe unless you treat values to prevent SQL injection... but if you really want it, instead of mysql_fetch_array(), try using mysql_fetch_assoc().

除非您将值视为阻止SQL注入,否则您尝试执行的更新并不安全...但如果您确实需要它,而不是mysql_fetch_array(),请尝试使用mysql_fetch_assoc()。

Using mysql_fetch_assoc() you can extract the keys (database field names) with array_keys(). The keys will be your the name property of your form fields and the values of will be the fields' values.

使用mysql_fetch_assoc(),您可以使用array_keys()提取密钥(数据库字段名称)。键将是表单字段的name属性,值将是字段的值。

Hope it helps.

希望能帮助到你。

#3


0  

You can use an array to store all the data that you need and add a hidden field that contains the missing data:

您可以使用数组存储所需的所有数据,并添加包含缺失数据的隐藏字段:

<form action="input_points.php" method="post">
<?php
    for($i=0; $row = mysql_fetch_array($result); $i++ ) {
        echo ' <input type="hidden" name="user[0]['name'] value ='". $row['name'] ."'">';
        echo $row['Name'] . ' <input type="text" name="user[$i]['points'] ">';
    }
?>
<button type="submit" name="add_points">Add Points </button>
</form>

#4


0  

Problem when you hit submit userpoints contain only the last value previous all values are overwritten solution name="userpoints" must be different each time why not you define it in database and then fetch it just like you fetch $row['Name']?

当你点击提交用户点时出现问题只包含最后一个值以前所有值都被覆盖解决方案名称=“userpoints”每次都必须不同,为什么不在数据库中定义它然后像获取$ row ['Name']一样获取它?