Alfraganus Posted December 24, 2015 Share Posted December 24, 2015 I am trying to upload my full context into web page, however, my articles are being uploaded without image, I have plenty of errors while showing images, please could you kindly check what I have done wrong and can you give me some solutions to my code? here are codes: <!DOCTYPE html> <html> <head> <title>uploading a file</title> </head> <body bgcolor="blue"> <form method="post"> <table align="center" width="700" border="2" bgcolor="green"> <tr align="center"> <td align="center"> <td colspan="8"><h2>upload whatever you want mr Alfraganus</h2></td> </td> <tr> <td colspan="8" align="center"><input type="text" name="title" size="60"><b>Title</b></td> </tr> <tr> <td colspan="8" align="right"><b> image:</b></td> <td align="left" colspan="8"><input type="file" name="image" /> </td> </tr> <tr> <td colspan="8"><textarea name="body" cols="80" rows="20"></textarea></td> </tr> <tr> <td colspan="8"><center><input type="submit" name="upload" value="upload the text"> </center></td> </tr> </table> </body> </html> <?php if (isset($_POST['upload'])) { define('DB_SERVER', "localhost"); define('DB_USER', "root"); define('DB_PASSWORD', ""); define('DB_DATABASE', "alfy"); define('DB_DRIVER', "mysql"); $title = $_POST['title']; $body = $_POST ['body']; $image=$_FILES['image']['name']; $$image_tmp=$_FILES ['image'] ['tmp_name']; move_uploaded_file($image_tmp, "images1/$image"); try { $db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER, DB_USER, DB_PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $db->prepare("INSERT INTO tables(title,body,image) VALUES (:title, :body,:image)"); $stmt->bindParam(':title', $title, PDO::PARAM_STR); $stmt->bindParam(':body', $body, PDO::PARAM_STR); $stmt->bindParam(':image', $image, PDO::PARAM_LOB); if($stmt->execute()) { echo "<script>alert('success')</script>"; } $db = null; } catch(PDOException $e) { trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR); } } number of errors: Notice: Undefined index: image in C:\xampp\htdocs\free\upload.php on line 46 Notice: Undefined index: image in C:\xampp\htdocs\free\upload.php on line 47Notice: Undefined variable: image_tmp in C:\xampp\htdocs\free\upload.php on line 47Notice: Undefined variable: image_tmp in C:\xampp\htdocs\free\upload.php on line 49Fatal error: Error occured while trying to insert into the DB:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null in C:\xampp\htdocs\free\upload.php on line 67 Link to comment Share on other sites More sharing options...
dsonesuk Posted December 24, 2015 Share Posted December 24, 2015 If you a uploading files you must use enctype="multipart/form-data" for the form element looking at these (now look, compare each variable name) $title = $_POST['title']; $body = $_POST ['body']; $image=$_FILES['image']['name']; $$image_tmp=$_FILES ['image'] ['tmp_name']; why do you think move_uploaded_file($image_tmp, "images1/$image"); will fail, that's my to dollar signs worth (clue). Link to comment Share on other sites More sharing options...
Alfraganus Posted December 24, 2015 Author Share Posted December 24, 2015 I did what u told me, now image is being uploaded, however, my image is not going to image folder, it is not moving anywhere, what should I do right now? Link to comment Share on other sites More sharing options...
dsonesuk Posted December 24, 2015 Share Posted December 24, 2015 Have you created the folder 'images1'?, and try using "/images1/$image" 1 Link to comment Share on other sites More sharing options...
Alfraganus Posted December 24, 2015 Author Share Posted December 24, 2015 it worked, thank you bro Link to comment Share on other sites More sharing options...
sandeepm Posted December 29, 2015 Share Posted December 29, 2015 Please add enctype="multipart/form-data" in form opening tag (<fortm enctype="multipart/form-data"></form>) Then you can check while submitting form in post method Link to comment Share on other sites More sharing options...
dsonesuk Posted December 29, 2015 Share Posted December 29, 2015 You mean like as already POINTED OUT! In post#2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now