我有从数据库填充字段的表格,你能告诉我php将数据插入数据库,每个分数在数据库中自己排(id,name,score)
更新:如下所示打印的白衣代码:
更新到数据库的行:7 - James - 15
更新到数据库的行:7 - James - 15
更新到数据库的行:7 - James - 15
现在我使用此表单:
<form action="insert_action2.php" id="form2" title="form2" method="post">
<table>
<?php
$link = mysqli_connect("localhost", "form", "form", "form");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM form2" ;
$players = $link->query($sql);
while($player = $players->fetch_assoc()){
?>
<tr>
<td>
<input type="text" name="id" id="id" value="<?php echo $player["id"]; ?>">
<input type="text" name="name" id="name" value="<?php echo $player["name"]; ?>">
</td>
<td>
<input type="text" name="score" id="score" size="2" value="<?php echo $player["score"]; ?>">
</td>
</p>
<?php
}
$link->close();
?>
</tr>
</table>
<input type="submit" value="update scores">
</form>
插入数据库-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);
?>
答案 0 :(得分:-1)
您需要使用:
$sql = "insert into `form2`(`name`, `score`) VALUES ('{$name}', '{$score}')";
当你在字符串中使用某个变量时,你需要改变变量。
或者用这种方式:
$sql = "insert into `form2`(`name`, `score`) VALUES ('" . $name . "', '" . $score . "')";