When I fill out my form to upload pictures, it moves the files into a directory and updates my MySQL database with the string containing the file name with extension (ex: picture,jpg). The INSERT command works fine, but the UPDATE command does not fill the first field with the string that is passed, but changes the NULL into a blank field. I have spent three days on this trying all sorts of things like making the field I want first to be second, I have altered the column to be text character set utf8; and a bunch of stuff, but it always is blank. Please help me. The current code is below:
if (isset($_FILES['imgs']['tmp_name'])){
$name = $_POST['name'];
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 = "../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{
$sqlfile = "UPDATE animals SET {$imgnum} = 'NULL' WHERE id = {$id}";
$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 />";
}
$sqlfile = "UPDATE animals SET {$imgnum} = '{$file_name}' WHERE id = {$id}";
$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 />";
}
}
}
}