Jump to content

Image update & URL update


joshgarrod

Recommended Posts

Hi all, I have a script that is supposed to upload an image and then update a field in the table with the image's path in it. The image uploads okay but the details do not UPDATE in the table... any ideas? I have echoed after the sql statement which doesn't appear upon submitting but it shows the query printed out doing the right thing (I think)

UPDATE `fleet` SET `fleetimage4` = '../fleet/last.JPG' WHERE `fleetref` = '9' LIMIT 1

 

<?php

$ref = (!empty($_GET['ref']))?trim($_GET['ref']):"";
$image = (!empty($_GET['image']))?trim($_GET['image']):"";
        
	 $idir = "../fleet/";   // Path To Images Directory

if (isset ($_FILES['fupload'])){

//upload the image to tmp directory
$url = $_FILES['fupload']['name'];   // Set $url To Equal The Filename For Later Use 
	if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { 
		$file_ext = strrchr($_FILES['fupload']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
		$copy = copy($_FILES['fupload']['tmp_name'], "$idir" . $_FILES['fupload']['name']);   // Move Image From Temporary Location To Permanent Location 
			}
			}

	if (isset($_POST['submit'])){

//$fleetref=$_POST["fleetref"];
$fleetimage1 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$fleetimage2 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$fleetimage3 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$fleetimage4 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);

# setup SQL statement
if ($image == "1") {
$query = sprintf("UPDATE `fleet` SET `fleetimage1` = '$fleetimage1' WHERE `fleetref` = '$ref' LIMIT 1");
} else if ($image == "2"){
$query = sprintf("UPDATE `fleet` SET `fleetimage2` = '$fleetimage2' WHERE `fleetref` = '$ref' LIMIT 1");
} else if ($image == "3"){
$query = sprintf("UPDATE `fleet` SET `fleetimage3` = '$fleetimage3' WHERE `fleetref` = '$ref' LIMIT 1");
} else if ($image == "4"){
$query = sprintf("UPDATE `fleet` SET `fleetimage4` = '$fleetimage4' WHERE `fleetref` = '$ref' LIMIT 1");
}

#execute SQL statement
echo "before exe";
$result = mysql_db_query($db,$query,$cid) or die($query."<br>".mysql_error());
echo "after exe";

# check for error
if (!$result){
	echo("ERROR: " . mysql_error() . "\n$SQL\n");
}}		

	?>
        <div class="controls_left">
        <FORM NAME="fa" ACTION="<?php echo "fleet_edit_image.php?ref=$ref&image=$image"; ?>" METHOD="POST" enctype="multipart/form-data">
          <input type = "hidden" name="MAX_FILE_SIZE" value = "1000000">
	Select image:
	<input type = "file" name = "fupload">
	<p>Copyright note: Please only use photographs either that you have taken yourself or that you have permission to use as we will not be held responsible for any Copyright infringement.</p>
     <input name="submit" type="submit" value="Edit image" />
   </FORM>
        </div>
        <div class="image_right"><?php
        
	if ($image == "1") {
	$imageprint = "$fleetimage1";
	} else if ($image == "2") {
	$imageprint = "$fleetimage2";
	} else if ($image == "3") {
	$imageprint = "$fleetimage3";
	} else if ($image == "4") {
	$imageprint = "$fleetimage4";
	}

	echo "<div class=\"image_right\"><img src=\"../$imageprint\" alt=\"Edit image\" width=\"293\" height=\"218\"></div>";

	?>

Link to comment
https://forums.phpfreaks.com/topic/219298-image-update-url-update/
Share on other sites

mysql_db_query()

is very out of date

PHP ver 4.0.6:  mysql_db_query() is deprecated' date=' do not use this function[/quote']

update

$result = mysql_db_query($db,$query,$cid) or die($query."<br>".mysql_error());

to

mysql_select_db($db, $cid);
$result = mysql_query($query, $cid) or die($query."<br>".mysql_error());

 

Hi, thanks for your reply but I get two warnings now and still the same result, the field in the table still doesn't update?

 

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home//login/fleet_edit_image.php on line 116

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home//login/fleet_edit_image.php on line 116

 

Thanks

It  would seam you haven't connected to the database, add the following code to the start (with the correct database details)

$cid = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

Hi, I was connecting further up, here is my code:

 

<?php

	$con = mysql_connect("host","user","pass");
		if (!$con)
			{
			  die('Could not connect: ' . mysql_error());
			 }

			mysql_select_db("quest", $con);
        
	 $idir = "../fleet/";   // Path To Images Directory

if (isset ($_FILES['fupload'])){

//upload the image to tmp directory
$url = $_FILES['fupload']['name'];   // Set $url To Equal The Filename For Later Use 
	if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg") { 
		$file_ext = strrchr($_FILES['fupload']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
		$copy = copy($_FILES['fupload']['tmp_name'], "$idir" . $_FILES['fupload']['name']);   // Move Image From Temporary Location To Permanent Location 
			}
			}

	if (isset($_POST['submit'])){

//$fleetref=$_POST["fleetref"];
$fleetimage1 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$fleetimage2 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$fleetimage3 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);
$fleetimage4 = mysql_real_escape_string("$idir" . $_FILES['fupload']['name']);

# setup SQL statement
if ($image == "1") {
$query = sprintf("UPDATE `fleet` SET `fleetimage1` = '$fleetimage1' WHERE `fleetref` = '$ref' LIMIT 1");
} else if ($image == "2"){
$query = sprintf("UPDATE `fleet` SET `fleetimage2` = '$fleetimage2' WHERE `fleetref` = '$ref' LIMIT 1");
} else if ($image == "3"){
$query = sprintf("UPDATE `fleet` SET `fleetimage3` = '$fleetimage3' WHERE `fleetref` = '$ref' LIMIT 1");
} else if ($image == "4"){
$query = sprintf("UPDATE `fleet` SET `fleetimage4` = '$fleetimage4' WHERE `fleetref` = '$ref' LIMIT 1");
}

#execute SQL statement
echo "before exe";
mysql_select_db($db, $cid);
$result = mysql_query($query, $cid) or die($query."<br>".mysql_error());
echo "after exe";

# check for error
if (!$result){
	echo("ERROR: " . mysql_error() . "\n$SQL\n");
}} else {

//header("location:fleet_edit.php?ref=$ref");
	//exit();


}

	?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.