Hi Guys,
This is my first attempt at doing something like this.
My client sells cars and they add info about the cars into a script which inserts the data including up-to 4 images, into a mysql db (the image names are inserted, the images are stored in a directory).
Up until now, they have had no way of editting the images, only removing the car entirely and reposting it with new images.
I am trying to build a script for editing the images, the problem is that i cant get it to work (fair sized problem 🙂 )
Now, from the 'edit car' page i provide a link for each image uploaded to 'edit this image' that link is formated as such - edit_cars_image.php?carid=id&picid=picid
My table for these cars has a seperate column for each image name, so my query has to be dynamic for each image they edit... anyway, this is my code - its messy and its all over the place because it has been chopped and changed a hundred times over the past hour... and its starting to irritate me.
<?php
ob_start();
include('admin_functions.php');
db_connect();
$today = date("F j, Y");
$query = "SELECT * from cars WHERE id='{$_GET['carid']}'";
$pplay = mysql_query($query) or die(mysql_error());
$row_pplay = mysql_fetch_assoc($pplay);
$p_id = $_GET['picid'];
$c_id = $_GET['carid'];
echo 'The current image in this position is<br />';
if ($p_id == "pic1") {
echo '<img src="../images/cars/'.$row_pplay['pic1'].'" border="0" />';
$input = '<input type="file" name="pic1" />';
} else if ($p_id == "pic2") {
echo '<img src="../images/cars/'.$row_pplay['pic2'].'" border="0" />';
$input = '<input type="file" name="pic2" />';
} else if ($p_id == "pic3") {
echo '<img src="../images/cars/'.$row_pplay['pic3'].'" border="0" />';
$input = '<input type="file" name="pic3" />';
} else if ($p_id == "pic4") {
echo '<img src="../images/cars/'.$row_pplay['pic4'].'" border="0" />';
$input = '<input type="file" name="pic4" />';
}
if ($_FILES["pic1"]["name"] != "" && $_GET['picid'] == "pic1")
{
$pic1_name = "1_" . date("d_m_Y_H_i") . "_" . $_FILES['pic1']['name'];
move_uploaded_file($_FILES['pic1']['tmp_name'], "../images/cars/{$pic1_name}");
} else {
$pic1_name = "";
}
if ($_FILES["pic2"]["name"] != "" && $_GET['picid'] == "pic2")
{
$pic2_name = "2_" . date("d_m_Y_H_i") . "_" . $_FILES['pic2']['name'];
move_uploaded_file($_FILES['pic2']['tmp_name'], "../images/cars/{$pic2_name}");
} else {
$pic2_name = "";
}
if ($_FILES["pic3"]["name"] != "" && $_GET['picid'] == "pic3")
{
$pic3_name = "3_" . date("d_m_Y_H_i") . "_" . $_FILES['pic3']['name'];
move_uploaded_file($_FILES['pic3']['tmp_name'], "../images/cars/{$pic3_name}");
} else {
$pic3_name = "";
}
if ($_FILES["pic4"]["name"] != "" && $_GET['picid'] == "pic4")
{
$pic4_name = "4_" . date("d_m_Y_H_i") . "_" . $_FILES['pic4']['name'];
move_uploaded_file($_FILES['pic4']['tmp_name'], "../images/cars/{$pic4_name}");
} else {
$pic4_name = "";
}
if (isset($_POST['submit'])) {
if ($p_id == "pic1") {
$updateSQL = "UPDATE cars SET pic1='{$pic1_name}' WHERE id='{$_POST['car_id']}'";
} else if ($p_id == "pic2") {
$updateSQL = "UPDATE cars SET pic1='{$pic2_name}' WHERE id='{$_POST['car_id']}'";
} else if ($p_id == "pic3") {
$updateSQL = "UPDATE cars SET pic1='{$pic3_name}' WHERE id='{$_POST['car_id']}'";
} else if ($p_id == "pic4") {
$updateSQL = "UPDATE cars SET pic1='{$pic4_name}' WHERE id='{$_POST['car_id']}'";
}
$Result1 = mysql_query($updateSQL) or die('Query was '.$updateSQL.'');
echo $updateSQL;
}
echo '
<form action="edit_car_pic.php" method="post" enctype="multipart/form-data">
Add new image into this position:<br />'.$input.'<br />
<input type="hidden" name="car_id" value="'.$c_id.'">
<input type="submit" name="submit" value="Add New Image">
';
?>
I would very, VERY much appreciate anybodys assistance with this.
Thanks very much in advance!
Christian