我正在研究PHP CRUD操作,我已经在PHP中创建了一个基本的编辑表单。我没有使用任何字段验证,我想要的只是编辑信息。
我遵循这个教程
一旦用户点击编辑链接,他就会被引导到下面的表单,用户应该在这个表单上编辑他的数据。
代码
<?php
include_once './functions.php';
include_once './database.php';
function renderForm($firstName,$lastName,$age){
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="edit.php" method="post">
First Name<input type="text" name="firstname" value="<?php $firstName ;?>"><br/>
Last Name<input type="text" name="lastname" value="<?php $lastName ;?>"><br/>
Age<input type="text" name="age" value="<?php $age ;?>"><br/>
<input type="submit" name="submit" value="Edit">
<a href="main_menu.php">Cancel</a>
</form>
<?php
}
?>
<?php
if (isset($_POST['submit'])) {
$firstName = cleanData($_POST['firstname']);
$lastName = cleanData($_POST['lastname']);
$age = (int) $_POST['age'];
$id = $_GET['id'];
$query = "UPDATE basic ";
$query.="SET first_name='$firstName',last_name='$lastName',age=$age ";
$query.="WHERE id=$id";
confirmQuery($query);
closeDatabase();
}else{
$id=cleanData($_GET['id']);
$query="SELECT * FROM basic WHERE id= {$id} ";
$result=confirmQuery($query);
$rows= mysqli_fetch_assoc($result);
$firstName=$rows['first_name'];
$lastName=$rows['last_name'];
$age=$rows['age'];
renderForm($firstName, $lastName, $age);
}
?>
</body>
</html>
//Additional information
//functions included in other files
function cleanData($input){
global $connection;
return mysqli_real_escape_string($connection,$input);
}
function confirmQuery($query){
global $connection;
$result=mysqli_query($connection, $query);
if(!$result){
return "Query failed : ".mysqli_error($connection);
}
else{
return $result;
}
}
function closeDatabase(){
global $connection;
mysqli_close($connection);
}
//I have not included the file which I am using to
//connect to the DB. I am sure there is no error with that file since it works
//properly with other php files
的问题,我有我的编辑表单是它不显示以前输入的数据,只是显示只有一个空白的表单(类似于创建表单)。(当我在上面提到的教程中运行演示时,它不会发生)
Netbenas IDE说HTML输入标签内的变量似乎在其范围中未使用。我在谷歌上搜索了这个问题,发现警告可以被简单地忽略。
但是我哪里做错了?我很感激任何人谁可以通过我的代码,并指出我的错误。
谢谢你
我已经将您的PHP code
更改为以下代码在您的edit.php
中使用。如果您有任何问题请发表评论。
<?php
include_once './functions.php';
include_once './database.php';
if (isset($_POST['submit'])) {
$firstName = cleanData($_POST['firstname']);
$lastName = cleanData($_POST['lastname']);
$age = (int) $_POST['age'];
$id = $_GET['id'];
$query = "UPDATE basic ";
$query.="SET first_name='$firstName',last_name='$lastName',age=$age ";
$query.="WHERE id=$id";
$r=mysql_query($query);
if($r)
{
echo "Record updated";
}
}
$id=$_GET['id'];
$query="SELECT * FROM basic WHERE id='$id' ";
$result=confirmQuery($query);
$rows= mysqli_fetch_assoc($result);
$firstName=$rows['first_name'];
$lastName=$rows['last_name'];
$age=$rows['age'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="edit.php" method="post">
First Name<input type="text" name="firstname" value="<?php echo $firstName ;?>"><br/>
Last Name<input type="text" name="lastname" value="<?php echo $lastName ;?>"><br/>
Age<input type="text" name="age" value="<?php echo $age ;?>"><br/>
<input type="submit" name="submit" value="Edit">
<a href="main_menu.php">Cancel</a>
</form>
</body>
</html>