我正在尝试使用PHP创建一个crud应用程序,我已经成功地从postgres显示我的数据表但是当我尝试时我遇到了一些错误。这是我的代码:
<?php require 'database.php'; $id = null; if ( !empty($_GET['Code_Espece'])) { $id = $_REQUEST['Code_Espece']; } if ( null==$id ) { header("Location: index.php"); } if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST)) {
$firstnameError = null;
if (isset($_POST['Nom_Scien'])) {
$firstname = $_POST['Nom_Scien'];
}
$valid = true;if (empty($firstname)) { $firstnameError = 'Please enter firstname'; $valid = false; }
if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'UPDATE espece SET Nom_Scien = ? WHERE Code_Espece = ?';
$q = $pdo->prepare($sql);
$q->execute(array($firstname));
Database::disconnect();
header("Location: index.php");
}
}else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM espece where "Code_Espece" = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$firstname = $data['Nom_Scien'];
Database::disconnect();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Crud-Update</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
</div>
<form method="post" action="update.php?Code_Espece=<?php echo $id ;?>">
</div>
<div class="control-group<?php echo!empty($firstnameError) ? 'error' : ''; ?>">
<label class="control-label">Nom Scientifique</label>
<br />
<div class="controls">
<input type="text" name="nomscientifique" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">
<?php if (!empty($firstnameError)): ?>
<span class="help-inline"><?php echo $firstnameError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-success" name="update" value="update">
<a class="btn" href="index.php">Retour</a>
</div>
</form>
</div>
</body>
</html>
问题是当我把我想要更改的内容点击并按下按钮时更新&#39;它返回以下消息:&#39;请输入名字&#39;尽管输入不是空的。
谢谢;
答案 0 :(得分:1)
将
<input type="text" name="nomscientifique" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">
更改为<input type="text" name="Nom_Scien" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">