2016-01-22 81 views
0

我maked圖片上傳我的形式,但隨着加入這個時間將錯誤上傳圖片致命錯誤:未捕獲的異常「PDOException」有消息「SQLSTATE [23000]:完整性約束衝突:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null' in /www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php:58 Stack trace: #0 /www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php(58): PDOStatement->execute(Array) #1 {main} thrown in /www/data08/users/i/itsiim.planet.ee/htdocs/progemine/system/lisa.php on line 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

圖像是否上傳正常,然後移動到正確的目錄? – RamRaider

回答

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>'; 
} 
0

問題是因爲Undefined index: image,這意味着您的文件沒有上傳。這是因爲您沒有在<form>元素中設置enctype="multipart/form-data"。應該是,

<form class="form-horizontal" action="lisa.php" method="post" enctype="multipart/form-data"> 

    // your HTML code 

</form> 
相關問題