Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:填充字段和数据库插入_Php_Mysql - Fatal编程技术网

PHP:填充字段和数据库插入

PHP:填充字段和数据库插入,php,mysql,Php,Mysql,我有从数据库中填充字段的表单,您能告诉我php如何将数据插入数据库,每个分数在数据库id、名称、分数中都有自己的行吗 更新:whit theis代码打印如下: 行更新到数据库:7-James-15 行更新到数据库:7-James-15 行更新到数据库:7-James-15 现在我用这个表格: 插入到数据库-insert_action2.php 我已经尝试了几个数组和foreach,但无法让它们正常工作 <?php foreach($_POST as $players => $val

我有从数据库中填充字段的表单,您能告诉我php如何将数据插入数据库,每个分数在数据库id、名称、分数中都有自己的行吗

更新:whit theis代码打印如下:

行更新到数据库:7-James-15

行更新到数据库:7-James-15

行更新到数据库:7-James-15

现在我用这个表格:

插入到数据库-insert_action2.php 我已经尝试了几个数组和foreach,但无法让它们正常工作

<?php

foreach($_POST as $players => $value) {

$id = mysqli_real_escape_string($link, $_POST['id']);
$name = mysqli_real_escape_string($link, $_POST['name']);
$score = mysqli_real_escape_string($link, $_POST['score']);

    $sql = "UPDATE form2 SET score='$score', name='$name' WHERE id=$id";        
        if(mysqli_query($link, $sql)){
                echo "lines updated to database: <br>$id - $name - $score <br><br><p><p>";
            } else{
                echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
            }
}

mysqli_close($link);


?>
您需要使用:

$sql = "insert into `form2`(`name`, `score`) VALUES ('{$name}', '{$score}')";
在字符串中使用某个变量时,需要对该变量进行scape

或者用这种方式:

$sql = "insert into `form2`(`name`, `score`) VALUES ('" . $name . "', '" . $score . "')";

真的吗?我不知道。谢谢。您的表单是否使用方法POST?@storm5用户似乎会向您提交多个分数。在这种情况下,需要将[]添加到id、name和score输入的name属性的末尾。是否已执行查询??是否确实要根据表单输入插入新记录?在我看来,你似乎更想更新它们。那不是真的。仅当变量是更复杂的表达式(例如对象的属性)或字符串中紧跟其后的字符是变量名称中允许的字符时,才需要执行此操作。
$sql = "insert into `form2`(`name`, `score`) VALUES ('" . $name . "', '" . $score . "')";