简体   繁体   中英

Inputting multiple values in database from HTML form using PHP

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.

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

<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.

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?

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?

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() .

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.

Hope it helps.

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>

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 -

$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
   }

当你点击提交userpoints 问题只包含最后一个值前面的所有值都将被覆盖解决方案的名称=“userpoints”每次都必须是不同的,为什么不是你在数据库中定义它,然后把它拿来就像你取$行[“名称”]?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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