Mysqlを使ったPHPのフォームで下記のエラーが出ます。
ErrorSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null
値が無いとインサートできないという内容だと思うのですが、値はしっかりあるように思います。
typoを確認したりしたのですがどうもうまくインサートできません
どうかご教授いただけると嬉しいです。
post.php
1 2<?php 3 4require_once 'lib/h.php'; 5 6$max_size = 4 * 1024 *1024; //limit is 4MB 7 8$file_uploaded = false; 9 10if(!empty($_POST['name'])){ 11 $errors['name'] = 'Sorry you have to enter name'; 12}else{ 13 $name = htmlspecialchars($_POST['name'],ENT_QUOTES); 14} 15 16if(!empty($_POST['name_kana'])) { 17 $errors['name_kana'] = 'Sorry you have to enter name'; 18}else{ 19 $name_kana = htmlspecialchars($_POST['name_kana'], ENT_QUOTES); 20} 21 22if(!empty($_POST['location'])) { 23 $errors['location'] = 'Sorry you have to enter location'; 24}else{ 25 $location = htmlspecialchars($_POST['location'], ENT_QUOTES); 26} 27 28if(!empty($_POST['tellnumber'])) { 29 $errors['tellnumber'] = 'Sorry you have to enter tell number'; 30}else{ 31 $tellnumber = htmlspecialchars($_POST['tellnumber'], ENT_QUOTES); 32} 33 34if(!empty($_POST['sex'])) { 35 $errors['sex'] = 'Sorry. you have to select your sex'; 36}else{ 37 $sex = htmlspecialchars($_POST['sex'], ENT_QUOTES); 38} 39 40if(isset($_FILES['image'])) { 41 if (! check_image()){ 42 echo 'Sorry this is not Jpeg image file or We cant function image file.'; 43 }elseif ($_FILES['image']['size']>$max_size){ 44 echo 'Sorry You have to upload file is under 4MB'; 45 }elseif (! $file = fopen($_FILES['image']['tmp_name'], 'rb')){ 46 echo 'Cant open file'; 47 }else{ 48 $image = fread($file, $_FILES['image']['size']); 49 fclose($file); 50 if(!$image){ 51 echo 'cant load image'; 52 }else{ 53 $file_uploaded = true; 54 } 55 } 56} 57 58if (!empty($_POST['submit'])){ 59 require_once 'config/database.php'; 60 61 try { 62 $db = new PDO($dsn,$user,$pass); 63 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 64 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 65 66 $sql = 'INSERT INTO apply_list(`id`, `name_kana`, `location`, `tellnumber`, `sex`, `image`) VALUES(NULL, :name, :name_kana, :location, :tellnumber, :sex, :image)'; 67 68 $prepare = $db->prepare($sql); 69 70 $prepare->bindParam(':name', $name, PDO::PARAM_STR); 71 $prepare->bindParam(':name_kana', $name_kana, PDO::PARAM_STR); 72 $prepare->bindParam(':location', $location, PDO::PARAM_STR); 73 $prepare->bindParam(':tellnumber', $tellnumber, PDO::PARAM_STR); 74 $prepare->bindParam(':sex', $sex, PDO::PARAM_STR); 75 $prepare->bindParam(':image', $image, PDO::PARAM_LOB); 76 77 $prepare->execute(); 78 }catch (PDOException $e){ 79 echo 'Error'. h($e->getMessage()); 80 } 81} 82 83function check_image(){ 84 $tmp_name = $_FILES['image']['tmp_name']; 85 86 if(! is_uploaded_file($tmp_name)){ 87 return false; 88 } 89 90 $finfo = new finfo(FILEINFO_MIME_TYPE); 91 $type = $finfo->file($tmp_name); 92 if ($type !== 'image/jpeg') { 93 return false; 94 } 95 96 return true; 97 98} 99 100<!DOCTYPE html> 101<html lang="en"> 102<head> 103 <meta charset="UTF-8"> 104 <title>Form</title> 105</head> 106<body> 107 108<form method="post" action=""> 109 <label>name: </label> 110 <input type="text" name="name" placeholder="Enter your name"> 111 <br> 112 <label>kana: </label> 113 <input type="text" name="name_kana" placeholder="Enter your kana name"> 114 <br> 115 <label>location</label> 116 <input type="text" name="location" placeholder="Enter your location"> 117 <br> 118 <label>tell number: </label> 119 <input type="text" name="tellnumber" placeholder="Enter your tell number"> 120 <br> 121 <label>sex: </label> 122 <input type="radio" name="sex" value="0"> 123 <label>men: </label> 124 <input type="radio" name="sex" value="1"> 125 <label>women: </label> 126 <br> 127 <label>Select your image</label> 128 <input type="file" name="image" value=""> 129 <br> 130 131 <input type="submit" name="submit"> 132</form> 133 134</body> 135</html>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/15 08:27
2018/04/15 13:14