MySQL Forums
Forum List  »  PHP

Re: PHP UPDATE passes a blank string instead of contents of variable
Posted by: Cane Kostovski
Date: July 27, 2015 04:25PM

Pete asked me to do a debug: I have modified it a little to make it easier to read. Here is the new code and the results afterward:

  //Images
if (isset($_FILES['imgs']['tmp_name'])){
	$name = $_POST['name'];
	echo "<pre>";
	print_r( $_FILES['imgs']['tmp_name'] );
	echo "</pre>"; 
	foreach($_FILES['imgs']['tmp_name'] as $key => $tmp_name ){
		$file_name = $_FILES['imgs']['name'][$key];
		$file_size = $_FILES['imgs']['size'][$key];
		$file_tmp = $_FILES['imgs']['tmp_name'][$key];
		$file_type = $_FILES['imgs']['type'][$key];
	$desired_dir = "../animal_images/{$name}/";
	if (!is_dir($desired_dir)) {
		if (mkdir($desired_dir)) {
			echo "The directory: " . $desired_dir . " has been created.<br />";
		}else{
			echo "The directory: " . $desired_dir . " has NOT been created.<br />";
		}
	}
        move_uploaded_file($file_tmp,$desired_dir.$file_name);
	$num = $key + 1;
	$imgnum = "img{$num}";
	if ($file_name == "") {
	    echo "File number {$num} not changed.<br />";
	}else{
	echo "File_Name Contents are: {$file_name} <br />";
	$sqlfile = "UPDATE `animals` SET `{$imgnum}`  = '{$file_name}'  WHERE `id` = {$id}";
	echo "SQLFILE Contents before the query are: {$sqlfile} <br />"; 
	$result = mysqli_query($con,$sqlfile) or trigger_error("Query Failed! SQL: $sqlfile - Error: ".mysqli_error(), E_USER_ERROR);
	if (!$result) {
	  echo "<h1>Could not successfully run query ($sqlfile) from DB: " . mysqli_error() . "</h1>";
	  exit;
	}else{
	      echo "Database updated with SQL: " . $sqlfile . " and MySQL Error: " . mysqli_error() . ".<br /><br />";
	    }
	}
	}
}

Results:
Array
(
[0] => /tmp/phpy9Ehzl
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)

File_Name Contents are: summer2.jpg
SQLFILE Contents before the query are: UPDATE `animals` SET `img1` = 'summer2.jpg' WHERE `id` = 44
Database updated with SQL: UPDATE `animals` SET `img1` = 'summer2.jpg' WHERE `id` = 44 and MySQL Error: .

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.