Validation while inserting into database

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

Post Reply
varun_146100
Forum Newbie
Posts: 3
Joined: Wed Oct 22, 2008 3:48 am

Validation while inserting into database

Post by varun_146100 »

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:

Code: Select all

$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.
nnnpuri
Forum Newbie
Posts: 14
Joined: Tue Nov 25, 2008 6:50 am

Re: Validation while inserting into database

Post by nnnpuri »

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>




<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{

if (value==null||value=="")
{alert(alerttxt);return false;}
else {return true}
}
}


function validate_dropdown(thisform)
{
with(thisform) {
if(thisform.state.options.length == 0 || thisform.state.value == "") {
alert("please select state");
thisform.state.focus();
return false;
}
}

return true;
}

function validate_listbox(thisform)
{
var success = false;

with(thisform) {
for(i=0; i<thisform.city.options.length; i++) {
if(thisform.city.options.selected == true) {
success = true;
break;
}
}
}

if(!success) {
alert("Please select city");
thisform.city.focus();
}

return success;
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(name,"Name must be filled out!")==false)
{name.focus();return false;}
}

with (thisform)
{
if (validate_required(address,"Address must be filled out!")==false)
{
address.focus();return false;
}
}

with (thisform)
{
if (validate_required(email,"Email must need")==false)
{
email.focus();return false;
}
}

if(!document.getElementById('rdmale').checked && !document.getElementById('rdfemale').checked) {
alert("please select gender");
document.getElementById('rdmale').focus();
return false;
}

if(!document.getElementById('chk1').checked && !document.getElementById('chk2').checked &&
!document.getElementById('chk3').checked && !document.getElementById('chk4').checked) {
alert("please select atleast one hobby");
document.getElementById('chk1').focus();
return false;
}

if(!validate_dropdown(thisform)) {
return false;
}

if(!validate_listbox(thisform)) {
return false;
}

with (thisform)
{
if (validate_required(user_name,"User Name must be filled out!")==false)
{
user_name.focus();return false;
}
}


with (thisform)
{
if (validate_required(password,"password must be filled out!")==false)
{
password.focus();return false;
}

}


with (thisform)
{
if (validate_required(confirm_password,"Confirm paaaword must be filled out!")==false)
{
confirm_password.focus();return false;
}
}

if(thisform.password.value != thisform.confirm_password.value) {
alert("Password and Confirm Password did not match.");
thisform.confirm_password.focus();
return false;
}

/*var radio_choice = false;
for (counter = 0; counter <form1.m2.length; counter++)
{
if (form1.m2[counter].checked)
radio_choice = true;
}

if (!radio_choice)
{
alert("please select option button.")
return (false);
}*/
return (true);
}
</script>



<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<body>
<h1>Regesrtation Form</h1>
<form action="" method="post" name="form1" id="myForm" onsubmit="return validate_form(this)">
<table border="0" width="100%" align="center">
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" />
*(Maxlength 50)</td>
</tr>
<tr>
<td>Address</td>
<td><textarea name="address" id="address"></textarea>
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" id="email" />
*(Maxlength 100)</td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="m2" value="male" id="rdmale"/>
Male
<input type="radio" name="m2" value="female" id="rdfemale">
Female</td>
</tr>
<tr>
<td>Upload Photo</td>
<td><input type="file" name="upload" id="upload" /></td>
</tr>
<tr>
<td>Hobby</td>
<td><input type="checkbox" name="hobby[]" value="cricket" id="chk1" />cricket&nbsp;
<input type="checkbox" name="hobby[]" value="football" id="chk2" />football&nbsp;
<input type="checkbox" name="hobby[]" value="rugby" id="chk3" />rugby&nbsp;
<input type="checkbox" name="hobby[]" value="other" id="chk4" />other
</td>
</tr>
<tr>
<td>State</td>
<td><select name="state" id="state">
<option></option>
<option value="Gujarat">Gujarat</option>
<option value="Maharashtra">Maharashtra</option>
<option value="MP">MP</option>
</select></td>
</tr>
<tr>
<td>City</td>
<td><select size="5" name="city" id="city">
<option value="ahmedabad">ahmedabad</option>
<option>patan</option>
<option>Shihori(B.K)</option>
<option>surendranagar</option>
<option>ahmedabad</option>
<option>patan</option>
<option>surendranagar</option>
<option>ahmedabad</option>
<option>patan</option>
<option>surendranagar</option>
<option>ahmedabad</option>
<option>patan</option>
<option>surendranagar</option>
<option>ahmedabad</option>
<option>patan</option>
<option>surendranagar</option>
</select>
</td>
</tr>

<tr>
<td>User name</td>
<td><input type="text" name="user_name" id="user_name" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td>confirm password</td>
<td><input type="password" name="confirm_password" id="confirm_password" /></td>
</tr>
<td><input type="submit" value="Submit" id="submit1"/>
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>



<?php echo $_POST["name"]; ?>.<br />
<?php echo $_POST["address"]; ?>.<br />
<?php echo $_POST["email"]; ?>.<br />
<?php echo $_POST["m2"]; ?>.<br />
<?php echo $_POST["upload"]; ?>.<br />

<?php echo $_POST["chk1"]; ?>.<br />
<?php echo $_POST["state"]; ?>.<br />
<?php echo $_POST["city"]; ?>.<br />
<?php echo $_POST["user_name"]; ?>.<br />
<?php echo $_POST["password"]; ?>.<br />
<?php echo $_POST["confirm_password"]; ?>.<br />

<?php

$con = mysql_connect("aditya5","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("training", $con);

$sql="INSERT INTO test1 (name, address, email, m2, upload, chk1, state, city, user_name, password, confirm_password)
VALUES
('$_POST[name]','$_POST[address]','$_POST[email]','$_POST[m2]','$_POST[upload]','$_POST[chk1]','$_POST[state]','$_POST[city]',
'$_POST[user_name]','$_POST[password]','$_POST[confirm_password]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

</body>
</html>
nnnpuri
Forum Newbie
Posts: 14
Joined: Tue Nov 25, 2008 6:50 am

Re: Validation while inserting into database

Post by nnnpuri »

if u need php server side validation ,mail me ----- Naresh
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Validation while inserting into database

Post by Kieran Huggins »

WOW - two things to consider:

1: please use code tags - left-aligned code without syntax highlighting is the number one cause of Panda extinction.

2: Let's try to keep answers in-thread, so other users are able to find them with a search. We like yelling at people to search; lets not hand them a citation with which they can yell back.

ANSWER TIME:

There's really no way around this - you have to check for duplicate values in the DB in some way. Sorry 'bout your luck :-/
nnnpuri
Forum Newbie
Posts: 14
Joined: Tue Nov 25, 2008 6:50 am

Re: Validation while inserting into database

Post by nnnpuri »

i am indian and my english is little poor so i do not understand u batter,can u please use simle english
Post Reply