Jump to content

article adds using a URL but not my upload box


Go to solution Solved by fastsol,

Recommended Posts

Im creating a CMS for my site and in my admin page I have an add page that adds new content to my site LOCATED HERE and have added a few form fields.

 

2 of these are:

 

IMAGE URL (text box) & UPLOAD IMAGE (select file button)

 

When I fill in all the fileds and select an image using IMAGE URL and hit add article, it works fine and my form is saved to my database and is then displayed on my site.

 

When I fill in all the fileds and select an image using UPLOAD IMAGE and hit add article, it adds the image to my selected folder in my cpanel but DOES NOT ADD TO DATABASE.

 

My question is:

 

How can I get it to add to the database? and save the new images location to the image field on the database?

 

I have followed this tutorial when adding the upload file button to my page.

 

Please do not show me links on how to do this as I already have read through them but I stuggle when it comes to adding this to my code.

 

my add.php code is this.

 

<?php

session_start
();

include_once('../include/connection.php');

if (isset($_SESSION['logged_in'])){
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (!empty($_POST['image']))
{
$image = $_POST['image'];
}
else
{
$image = $_POST['imageupload'];

if (isset($_FILES['imageupload']))
{
$errors = array();
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

$file_name = $_FILES['imageupload'] ['name'];
$file_ext = strtolower (end (explode ('.', $file_name)));
$file_size = $_FILES['imageupload'] ['size'];
$file_tmp = $_FILES['imageupload'] ['tmp_name'];

if (in_array ($file_ext, $allowed_ext) === false) {
$errors[] = 'File extension not allowed';
}

if ($file_size > 2097152) {
$errors[] = 'File size must be under 2mb';
}

if (empty($errors)) {
if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
echo 'File uploaded';
}
}else{
foreach ($errors as $error)
echo $error, '<br />';
}

}
}
$link = $_POST['link'];
$category = $_POST['category'];
$brand = $_POST['brand'];


if (empty($title) or empty($content)) {
$error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES(?, ?, ?, ?, ?, ?)');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $image);
$query->bindValue(4, $link);
$query->bindValue(5, $category);
$query->bindValue(6, $brand);



$query->execute();
header('location: index.php');
}

}
?>
<?php

if (isset($_FILES['Filedata']))
{
// And if it was ok
if ($_FILES['Filedata']['error'] !== UPLOAD_ERR_OK)
exit('Upload failed. Error code: ' . $_FILES['image']['error']);

$filename = $_FILES['Filedata']['name'];
$targetpath = "../img/news/" . $filename; //target directory relative to script location

$copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);
}
?>

<html>
<head>
<title>Add Article</title>
<link rel="stylesheet" href="../other.css" />
</head>

<body>
<div class="container">
<a href="index.php" id="logo"><b>← Back</b></a>

<br />

<div align="center">
<h4>Add Article</h4>

<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data">

<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input name="imageupload" type="file" id="image" placeholder="Imageupload" />
<input type="text" name="image" placeholder="Image" /><br /><br />
<input type="link" name="link" placeholder="Link" /><br /><br />
<input type="category" name="category" placeholder="Category" /><br /><br />
<input type="category" name="brand" placeholder="Brand" /><br /><br />
<input type="submit" value="Add Article" />

</form>
</div>
</div>
</body>
</html>


<?php
}else{
header('location: index.php');
}

?>

 

please help. thanks.

Can anyone help?

 

you haven't clearly stated exactly what the current problem is (we only see the information that you put in your posts.) you previously stated that the file was being uploaded, but not being added to the database table. the code change that fastsol handed you would have fixed that problem. if it didn't correct that problem, you would need to tell us what it did do, and if the problem changed, you would need to tell us that too.

I think it has all been explained above. Just not that clear.

 

In a nut shell.

 

The upload button with the code above works by adding the image to my images folder so thank you for that.

 

But on a seperate note, let's say for example that I upload an image called "image123.jpg". It saves the image as image.jpg but not http://www.mysite.com/images/image123.jpg. Which means it does not display on my site.

 

How can I upload the "image123.jpg" and have it saved In mysql as the full site link? Thanks.

  • Solution

Ok, you need to move the $brand var higher in the script so that it's defined before the move_uploaded_file, good place would likely be where you define $title.

$brand = $_POST['brand']; // Put this up by where you define $title


$up_file = $brand.'.'.$file_ext;
if (move_uploaded_file($file_tmp, 'images/'.$up_file)) {
echo 'File uploaded';
$image = 'www.mysite.com/images/'.$up_file;
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.