Jump to content

Validation while inserting into database


varun_146100

Recommended Posts

Hi,

 

I have a form from which the user inputs some data.

Now i need to check from the database whether these are valid values or not since the values inputted can clash with the existing values.

Here is the query:

 

 

$sql="if exists(select * from EnvironmentBookingtest where EnvironmentId=$_POST[EnvName] and ((convert(varchar(10),convert( datetime,'$_POST[Date1]',103), 110 ) between StartDate and EndDate) OR (convert(varchar(10),convert( datetime,'$_POST[Date2]',103), 110 ) between StartDate and EndDate)))

begin

print 'wrong data'

end

else

begin

INSERT INTO EnvironmentBooking VALUES ($_POST[EnvName],'".$_POST[bookFor]."',convert(varchar(10),convert( datetime,'$_POST[Date1]',103), 110 ),convert(varchar(10),convert( datetime,'$_POST[Date2]',103), 110 ),'".$_POST[Purpose]."',getdate())

end";

sybase_query($sql) or die ('Error updating database');

 

Now what i want is that this message 'wrong data' should be printed on the form when the user inputs the data and hits the submit button.

 

Or is there a way where i can add it as a validation using javascript such that it can be sent as an alert ??

Please help.Its urgently required.

First of all that SQL statement is just wow I don't know if I would even want to touch it, I don't know if it would work, but it's one of those thing that you see and think if I touch this thing I'm going to break it.

 

Secondly I think JavaScript or better yet AJAX would be the way to go. AJAX can run the code in the background (the check), and once validated submit it.

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.