php - 将多个文本框的数据插入到单列

标签 php mysql

enter image description here我想一次将多个文本框值插入到表中的单个列中

应该有名字,名字前面有输入学生年龄的文本框 所以当我们提交然后在学生姓名前面输入的年龄时,它将与学生姓名和年龄一起存储在表中。 (一次多个学生数据馈送)

最佳答案

您好尝试根据您的更改 table 名称和 column 名称。 我还查询的所有数据,如果年龄已经存在,这将更新所有字段年龄,并且您再次填写表格。因此,要么在您的查询中应用 where condition 以便它仅过滤没有价格的名称,或者在 database< 中显示年龄的表单中添加值字段。(这是根据您在评论中提供的代码)

<?php
$conn = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password,$mysql_database) or die("Could not connect database");
$result = mysqli_query($conn,"select * from user");
if(isset($_POST['submit'])){    
    $sql = "UPDATE user SET age = (CASE name ";
        foreach($_POST['age'] as $key=>$value){
        $sql = "UPDATE user SET age = '$value' where name = '$key'";
        mysqli_query($conn,$sql);
    }
    $success = "updated successfully";  
}
if(isset($success) && !empty($success)){    
    echo '<h3>'.$success.'</h3>';
}
echo '<form method="post">';
echo '<table>';
while($row = mysqli_fetch_assoc($result)){?>
    <tr>
    <td><input type="text" value="<?php echo $row['name']; ?>" name="name" readonly="true"></td>
           <td><input type="number" name="age[<?php echo $row['name']; ?>]" placeholder='enter age'></td>
        </tr>       

<?php } ?>
<tr><td colspan="2">
 <input type="submit"   name="submit" value="Save"></td></tr>

 </table>
</form>

关于php - 将多个文本框的数据插入到单列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068289/

相关文章:

mysql - 在读取速度和写入速度方面,InnoDB 与 MyISAM 的比较是否仍然适用?

mySQL - 将多个列上的不同列计数为一个

MySQL - 连接 2 个表和组值

php - 从android发送数据到mysql

php - 使用 Li3 为 MongoDB 设置安全 => 'majority'

php - 我可以通过写入标准输出来调试我的 Laravel 迁移吗?如何?

php - 如何保护 PHP 脚本以使其仅在购买时可用?

mysql - SQL错误[1054] [42s22] : unknown column 'orderdetails.productCode in ' on clause'