is it possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

is it possible?

Post by specialchild »

i am new to php and am really struggling with something that to me seems like it should be quite simple...

is it possible to submit empty form fields to a mysql db using php?

i have built a simple new cms system which works fine if users complete all the forms, but if a user leaves a field blank the php script fails to run and nothing gets sumitted to the db.

i can get round the majority of this problem by using javascript, but my real problem is that i have a file upload field that i can't use javascript on to add a hidden value if a user does not add anything. ideally i would like to submit nothing if the user does not wantto submit anything.

can anyone give me any advice? i can't believe it can be as difficult as i am finding it...

thanks in advance :lol:
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, this could be due to a few things, namely how you are checking the data before you send it to mysql.

anyway we can see some of the code? especially your query. if it's quite long, only post the portions that control the data that isn't getting put into the db.
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

ok here is my php code:

Code: Select all

if ($action == "submit" && $title && $client && $body && $image && $file1 && $file2 && $dept){
	$title= preg_replace("/\\n/", "<br />", $title);
	$client= preg_replace("/\\n/", "<br />", $client);
	$body= preg_replace("/\\n/", "<br />", $body);
	
	

$image = $HTTP_POST_FILES['image']['tmp_name'];
$image_name = $HTTP_POST_FILES['image']['name'];
$image_size = $HTTP_POST_FILES['image']['size'];
	
$file1 = $HTTP_POST_FILES['file1']['tmp_name'];
$file1_name = $HTTP_POST_FILES['file1']['name'];
$file1_size = $HTTP_POST_FILES['file1']['size'];

$file2 = $HTTP_POST_FILES['file2']['tmp_name'];
$file2_name = $HTTP_POST_FILES['file2']['name'];
$file2_size = $HTTP_POST_FILES['file2']['size'];


// ADD PATH TO IMAGE DIRECTORY
$newLoc = "i have taken the path out for security";
if ($image_size!=0)

	{
		
	//echo "<br>" . $image_name;		
	$newLoc1 = $newLoc . $image_name ;
	
		if (!copy($image, $newLoc1)) 
		
			{
			
				echo "couldn't move file...";
				
			}
		
	}

if ($file1_size!=0)

	{
		
	//echo "<br>" . $image_name;		
	$newLoc1 = $newLoc . $file1_name ;
	
		if (!copy($file1, $newLoc1)) 
		
			{
			
				echo "couldn't move file...";
				
			}
		
	}
	
	
	if ($file2_size!=0)

	{
		
	//echo "<br>" . $image_name;		
	$newLoc1 = $newLoc . $file2_name ;
	
		if (!copy($file2, $newLoc1)) 
		
			{
			
				echo "couldn't move file...";
				
			}
		
	}
	
	
	$dept= preg_replace("/\\n/", "<br />", $dept);
	addportfolio($title, $client, $body, $image_name, $file1_name, $file2_name, $dept);
	$complete="The new article has been successfully added.";
} elseif ($action == "delete" && $id){
	deleteportfolio($id);
	$complete="The article has been deleted.";
} elseif ($action == "submit" || $action == "update"){
	$error = "Error: The requested action has failed. Please go back and try again.";
}
Last edited by specialchild on Tue Nov 25, 2003 4:23 am, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

so where is the sql query?
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

depend on your table structure... I mean if you are trying to insert blank value to a field that is set as "NOT NULL" then you are ought to get an error. make sure that field can be NULL fo even if form is submitted with blank values it shouldn't throw errors...

secondly if you are using some checking while running SQL query...
well.. to say anything more... we should see smoe code.. like infolock said.. :
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

Code: Select all

function addportfolio($title, $client, $body, $image, $file1, $file2, $dept){
	$date_array=getdate(time());
	$date=$date_array["year"]."-".$date_array["mon"]."-".$date_array["mday"];
	$query = "INSERT INTO portfolio VALUES ('', '$date', '$title', '$client', '$body', '$image', '$file1', '$file2', '$dept')";
	get_data($query, "");
	return "done";
}
Last edited by specialchild on Tue Nov 25, 2003 4:23 am, edited 1 time in total.
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

guess.. i'm bit slow at typing.. heh
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

does it throw back an error? if so, what is it?

and can you please put [syntax=php][/syntax] tags around your code for easier viewing.

Mark
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

it doesn't throw back any error, the script just fails.
if all fields are completed the script runs fine.
if i leave one of the upload fields blank it fails - so i'm assuming it is a problem with empty fields.
i would have thought it possible to submit empty values. is this not the case.

apologies for not tagging my code - i'll go back and change them if i can.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

can you show the part of the code where the SQL statement gets executed.

Mark
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

i have the fields in the db set as null and the deafult as null. i am assuming this is correct?
i'm using phpmyadmin 2.2.0 hosted with mediatemple
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[big_search]mysql[/big_search]
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

this is the sql that adds the form data to the db, it works fien when all firelds contain a value, but if image, file1 or file2 are empty then it fails.

Code: Select all

function addportfolio($title, $client, $body, $image, $file1, $file2, $dept){ 
   $date_array=getdate(time()); 
   $date=$date_array["year"]."-".$date_array["mon"]."-".$date_array["mday"]; 
   $query = "INSERT INTO portfolio VALUES ('', '$date', '$title', '$client', '$body', '$image', '$file1', '$file2', '$dept')"; 
   get_data($query, ""); 
   return "done"; 
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yeah, but where does...

Code: Select all

$query = "INSERT INTO portfolio VALUES ('', '$date', '$title', '$client', '$body', '$image', '$file1', '$file2', '$dept')";
...get executed. I presume it is in the function called get_data.

Can you show me that function.

Mark
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

check my second post in this thread, it gets called there.
sorry if i'm not making much sense, but php is not my forte, this code is stuff i have managed to cobble together from searching the web and other peoples tutorials.
Post Reply