Jump to content

[SOLVED] A litte 'If/else' help please.


Craigus

Recommended Posts

Howdy all,

 

I have a form that inserts some data along with an image into mysql. What I'm wanting to do it if the user leaves the image upload field blank I want it to be skipped (image not required). I've spent some time on this and am not exactly sure on the best method to use?

 

Any pointers would be greatly appreciated.

 

My insert code:

<?php

//Get DB configuraion file
require('./config/config.php');


//Set variables
$species = $_POST['species'];
$location = $_POST['location'];
$state = $_POST['state'];
$date = $_POST['date'];
$comments = $_POST['comments'];


//Check file is a valid image and under 200kb
if (($_FILES["image"]["type"] == "image/jpeg")
&& ($_FILES["image"]["size"] < 200000))
{


//prepare the image for insertion
$image =addslashes (file_get_contents($_FILES['image']['tmp_name']));

//Insert into database
$sql="INSERT INTO bird_db (image, species, location, state, date, latlong, comments)
VALUES ('$image', '$species', '$location', '$state', '$date', '$latlong', '$comments')";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}


//Results
echo "Thank you, your record has been added to the database";
}

//Error if file invalid type/size
else
{echo "Sorry, your image is more then 200KB or you have entered an invalid file type (.jpg only)";}

?>

Link to comment
https://forums.phpfreaks.com/topic/160014-solved-a-litte-ifelse-help-please/
Share on other sites

<?php

//Get DB configuraion file
require('./config/config.php');


//Set variables
$species = $_POST['species'];
$location = $_POST['location'];
$state = $_POST['state'];
$date = $_POST['date'];
$comments = $_POST['comments'];

if (isset($_FILES['image']['tmp_name']))
{
//Check file is a valid image and under 200kb
if (($_FILES["image"]["type"] == "image/jpeg") && ($_FILES["image"]["size"] < 200000))
   {
		//prepare the image for insertion
		$image =addslashes (file_get_contents($_FILES['image']['tmp_name']));
	}
	else
		echo "Sorry, your image is more then 200KB or you have entered an invalid file type (.jpg only)";
}
else
$image = 'NULL';	

//Insert into database
$sql="INSERT INTO bird_db (image, species, location, state, date, latlong, comments)
VALUES ('$image', '$species', '$location', '$state', '$date', '$latlong', '$comments')";

if (!mysql_query($sql))
   {
die('Error: ' . mysql_error());
   }
else
echo "Thank you, your record has been added to the database"; 	

}
?>

Try this.

 

P.S Not tested for errors

<?php

//Get DB configuraion file
require('./config/config.php');

//Set variables
$species = mysql_real_escape_string($_POST['species']);
$location = mysql_real_escape_string($_POST['location']);
$state = mysql_real_escape_string($_POST['state']);
$date = mysql_real_escape_string($_POST['date']);
$comments = mysql_real_escape_string($_POST['comments']);

if (is_uploaded_file($_FILES['image']['tmp_name'])){

//Check file is a valid image and under 200kb
if (($_FILES["image"]["type"] == "image/jpeg") && ($_FILES["image"]["size"] < 200000)){
	//prepare the image for insertion
	$image =mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
}

else{
	echo "Sorry, your image is more then 200KB or you have entered an invalid file type (.jpg only)";
}
}

else{
$image = 'NULL';
}

//Insert into database
$sql="INSERT INTO bird_db (image, species, location, state, date, latlong, comments)
VALUES ('$image', '$species', '$location', '$state', '$date', '$latlong', '$comments')";

$insertion = mysql_query($sql) or trigger_error(mysql_error());
if($insertion){
echo "Thank you, your record has been added to the database"; 
}

?>

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.