我为我的表单上传图片上传,但这会增加错误时间
致命错误:未捕获的异常' PDOException'消息' SQLSTATE [23000]:完整性约束违规:1048列'图像'不能为空'在/www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php:58堆栈跟踪:#0 /www/data08/users/i/itsiim.planet.ee/htdocs/progemine /system/lisa.php(58):PDOStatement->执行(数组)#1 {main}抛出/www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php on第58行
<?php
require 'conf/db.php';
if ( !empty($_POST)) {
// keep track validation errors
$nimiError = null;
$emailError = null;
$mobiilError = null;
$suguError = null;
// keep track post values
$nimi = $_POST['nimi'];
$email = $_POST['email'];
$mobiil = $_POST['mobiil'];
$sugu = $_POST['sugu'];
// validate input
$valid = true;
if (empty($nimi)) {
$nimiError = 'Palun sisesta nimi';
$valid = false;
}
if (empty($email)) {
$emailError = 'Palun sisesta e-mail';
$valid = false;
} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$emailError = 'Palun sisesta korrektne e-mail';
$valid = false;
}
if (empty($mobiil)) {
$mobiilError = 'Palun sisesta mobiili number';
$valid = false;
}
if (empty($sugu)) {
$suguError = 'Palun vali sugu';
$valid = false;
}
//Pilt
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$folder = "upload/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
$image = $full_path;
}
}
// insert data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu,image) values(?, ?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($nimi,$email,$mobiil,$sugu,$image));
Database::disconnect();
header("Location: index.php");
}
}
?>
<!DOCTYPE html>
<html lang="et">
<head>
<meta charset="utf-8">
<title>Klientide andmed by Siim Aarmaa IS-13</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<div class="row">
<h3>Lisa uus klient</h3>
</div>
<form class="form-horizontal" action="lisa.php" method="post">
<div class="form-group <?php echo !empty($nimiError)?'error':'';?>">
<label class="col-sm-2 control-label">Nimi</label>
<div class="controls">
<input name="nimi" type="text" placeholder="Nimi" value="<?php echo !empty($nimi)?$nimi:'';?>">
<?php if (!empty($nimiError)): ?>
<span class="help-block"><?php echo $nimiError;?></span>
<?php endif; ?>
</div>
</div>
<div class="form-group <?php echo !empty($emailError)?'error':'';?>">
<label class="col-sm-2 control-label">E-mail</label>
<div class="controls">
<input name="email" type="text" placeholder="E-mail" value="<?php echo !empty($email)?$email:'';?>">
<?php if (!empty($emailError)): ?>
<span class="help-block"><?php echo $emailError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-group <?php echo !empty($mobiilError)?'error':'';?>">
<label class="col-sm-2 control-label">Mobiili number</label>
<div class="controls">
<input name="mobiil" type="text" placeholder="Mobiili number" value="<?php echo !empty($mobiil)?$mobiil:'';?>">
<?php if (!empty($mobiilError)): ?>
<span class="help-block"><?php echo $mobiilError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-group <?php echo !empty($suguError)?'error':'';?>">
<label class="col-sm-2 control-label">Sugu</label>
<div class="controls">
<input name="sugu" type="radio" value="<?php echo !empty($mees)?$mees:'Mees';?>">Mees
<input name="sugu" type="radio" value="<?php echo !empty($naine)?$naine:'Naine';?>">Naine
<?php if (!empty($suguError)): ?>
<span class="help-block"><?php echo $suguError;?></span>
<?php endif;?>
</div><br>
<div class="form-group <?php echo !empty($mobiilError)?'error':'';?>">
<label class="col-sm-2 control-label">Pilt</label>
<div class="controls">
<input type="file" name="image" required="required" value=""/>
<?php if (!empty($mobiilError)): ?>
<span class="help-block"><?php echo $mobiilError;?></span>
<?php endif;?>
</div>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-success">Lisa klient</button>
<a class="btn btn-default" href="index.php">Tagasi</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>
答案 0 :(得分:1)
看起来fileupload出了问题。你没有is_uploaded_file()和move_uploaded_file()检查的其他分支。
<?php
require 'conf/db.php';
$errors = array();
if ( !isset($_POST['nimi'],$_POST['email'],$_POST['mobiil'],$_POST['sugu']) ) {
$errors['parameter'] = 'missing POST parameter';
}
else {
// keep track post values
$nimi = $_POST['nimi'];
$email = $_POST['email'];
$mobiil = $_POST['mobiil'];
$sugu = $_POST['sugu'];
// validate input
if (empty($nimi)) {
$errors['nimi'] = 'Palun sisesta nimi';
}
if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$errors['email'] = 'Palun sisesta korrektne e-mail';
}
if (empty($mobiil)) {
$errors['mobiil'] = 'Palun sisesta mobiili number';
}
if (empty($sugu)) {
$errors['suguError'] = 'Palun vali sugu';
}
if ( empty($errors) ) {
//Pilt
if( !is_uploaded_file($_FILES['image']['tmp_name']) ) {
$errors['upload'] = 'no file uploaded';
}
else {
$folder = "upload/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if( !move_uploaded_file($_FILES['image']['tmp_name'], $full_path) ) {
$errors['upload'] = 'cannot move file';
}
else {
$image = $full_path;
}
}
}
}
// insert data
if ( empty($errors) ) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO kliendid (nimi,email,mobiil,sugu,image) values(?, ?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute( array($nimi,$email,$mobiil,$sugu,$image) );
Database::disconnect();
header("Location: index.php");
die;
}
else {
echo '<pre>', join("\r\n", $errors), '</pre>';
}
答案 1 :(得分:0)
问题在于Undefined index: image
,这意味着您的文件没有上传。这是因为您没有在enctype="multipart/form-data"
元素中设置<form>
。它应该是,
<form class="form-horizontal" action="lisa.php" method="post" enctype="multipart/form-data">
// your HTML code
</form>