Search the Community
Showing results for tags 'php mysql images'.
-
I'm creating an admin side to my website. I have created an add page and within this page is a form with a few fields. 2 of these are an upload button for me to upload an image. And the other one is a fill box for me to enter a URL. I have set it up so that both of them update the same mysql field and also the fill box is priority for when both are entered. However. The search box is fine as it saves the link correctly but selecting an image using the select box only saves the file name in this field in mysql for example: xxxxxxxxxxxxx.png So with my website displaying these images, it does not display the uploaded ones. Obviously because these are saved on to my computer and not on to my cpanel. So my question is: How can I upload an image using the upload box and have it save to my /images folder then have mysql display that URL link in the relevant field? 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['Filedata'])) { $filename = $_FILES['Filedata']['name']; $targetpath = "/images/" . $filename; //target directory relative to script location $copy = copy($_FILES['Filedata']['tmp_name'], $targetpath); if (!$copy) $error = "Image was not uploaded successfully"; } } $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"> <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.