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
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

Link to comment
Share on other sites

<?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"; 
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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