Hi all been working on this for a couple of days, but it has been ticking me off. I want to upload multiple image names to the mysql database and upload the images to a folder on the local server. I also want to display my images that go with the text data. I know I could use imagemanager from tinymce plug-in, but the person I am creating it for wouldn’t pay the licence.
So far I can add text to mysql and display it, but not the images. So to my question how would I add multiple images or in my case 4 images to the mysql database and to also store them in a the local server folder?
Much appreciated and many thanks for the help in advance
syntax below;
The add-content.php
<?php
session_start();
if(isset($_SESSION['user'])) {
include '../_class/cms_class.php';
$obj = new modern();
// Connect To Our Database
$obj->connect();
?>
<html>
</header>
<form method="post" action="index.php">
<div>
<input type="hidden" name="add" value="true" />
<div>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</div>
<div>
<label for="title">Message:</label>
<textarea id="body" name="body" rows="15" cols="80" style="width: 80%">
</textarea>
</div>
<div>
<label for="title">Image 1:</label>
<input type="file" name="image1" id="title" /><br /><br />
<label for="title">Image 2:</label>
<input type="file" name="image2" id="title" /><br /><br />
<label for="title">Image 3:</label>
<input type="file" name="image3" id="title" /><br /><br />
<label for="title">Image 4:</label>
<input type="file" name="image4" id="title" />
</div>
<br />
<input type="submit" name="save" value="Submit" />
</div>
</form>
</div>
The main function page class.php
//Add function
function add_content($p) {
$title = mysql_real_escape_string($p['title']);
$body = mysql_real_escape_string($p['body']);
$image1 = mysql_real_escape_string($p['image1']);
$image2 = mysql_real_escape_string($p['image2']);
$image3 = mysql_real_escape_string($p['image3']);
$image4 = mysql_real_escape_string($p['image4']);
//This is the directory where images will be saved
$target = "./images";
$target = $target . basename( $_FILES['image1']['image2']['image3']['image4']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['image1']['image2']['image3']['image4'], $target))
//file properties
$file = $_FILES['image1']['temp_name1']['image2']['temp_name2']['image3']['temp_name3']['image4']['temp_name4'];
if(!$title || !$body):
if(!$title):
echo "<p>The title is required</p>";
endif;
if(!$body):
echo "<p>The body is required</p>";
endif;
echo '<p><a href="add-content.php">Please Try Again!</a></p>';
else:
$sql = "INSERT INTO cms_content VALUES (null, '$title', '$body', '$image1', '$image2', '$image3', '$image4')";
$res = mysql_query($sql) or die(mysql_error());
echo "Added Successfully!";
endif;
}
//Get or Display function
function get_content($id = '') {
if($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM cms_content WHERE id = '$id'";
$return = '<p><a href="index.php">Go Back to Content</a></p>';
else:
$sql = "SELECT id, title FROM cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
echo '<p>' . $row['body'] . '</p>';
echo '<table><tr><td>' . $row['image1'] . '</td><td>' . $row['image2'] . '</td><td>' . $row['image3'] . '</td><td>' . $row['image4'] . '</td></tr></table>';
}
else:
echo '<p>Uh Oh!, this doesn\'t exist!';
endif;
echo $return;
}