我使用$ _POST,我得到空值

时间:2016-05-01 01:47:05

标签: php mysql post

<html>
<head>
</head>
<body>
<form action="" method="POST">
<input type="submit" name="submit">
</form>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT * FROM Pelates";
$result = $conn->query($sql);
$count=$result->num_rows;  
if ($result->num_rows > 0) {
echo "<table><tr><th></th><th>Name</th><th>Surname</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td><input type='hidden' id='num' name='number[]' value='".$row["number"]."'/></td><td><input type='text' id='nam' name='name[]' value='".$row["name"]."'/></td><td><input type='text' id='surnam' name='surname[]' value='".$row["surname"]."'/></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $number = $_POST['number'];
for($i=1;$i<=$count;$i++){
$sql = "UPDATE Pelates SET name='".$name[$i]."', surname='".$surname[$i]."' WHERE number=".intval($number[$i])."";
if (mysqli_query($conn, $sql)) {
echo "Update ok";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>
</body>
</html>

我有一张包含我的记录的表格,我试图更新所有记录,但是当我按下提交按钮时,所有字段都会更新为空字段。 这是我使用的代码。我把这段代码放在一篇joomla文章中,并且我使用了sourcerer。

1 个答案:

答案 0 :(得分:1)

看起来</form>元素在渲染任何<input>元素之前关闭了表单。