Jump to content

uploading an image


Alfraganus

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...