Jump to content

uploading an image


Recommended Posts

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 47

Notice: Undefined variable: image_tmp in C:\xampp\htdocs\free\upload.php on line 47

Notice: Undefined variable: image_tmp in C:\xampp\htdocs\free\upload.php on line 49

Fatal 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 post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...