Webocreation

Sunday, February 14, 2010

Code to insert check box elements in the database

Code to insert check box elements in the database
<?php

if(isset($_POST['button']))
{


//count-counts varibale in array
$num=count($_POST['hobbies']);

$errors=array();


$total_hobbies="";


for($i=0;$i<$num;$i++)
{
$total_hobbies=$total_hobbies.$_POST['hobbies'][$i].",";
}

$total_hobbies = substr($total_hobbies, 0,-1); // returns "ef"
//echo $total_hobbies;

if(empty($_POST['name']))
{
$errors[]="Your Name field is empty.Please fill it";
}
else
{
$name=$_POST['name'];
}

if(empty($_POST['address']))
{
$errors[]="Your address field is empty.Please fill it";
}
else
{
$address=$_POST['address'];
}


if(empty($_POST['user_name']))
{
$errors[]="Your user_name field is empty.Please fill it";
$user_name=FALSE;
}
else
{
$user_name=$_POST['user_name'];
}


$education=$_POST['education'];

$gender=$_POST['gender'];


if(strlen($_POST['pass'])<4)
{
$errors[]="Password field cannot be less than 4 character.Please fill it";
}
else
{
$pass=$_POST['pass'];
}



if (empty($errors))
{
require_once('connect.php');
//echo "$name $address $user_name $pass";


$query1="select user_id from profile where user_name=\"$user_name\"";

$result1=mysql_query($query1);
if(mysql_num_rows($result1)>0)
{
echo "This username is not available";
}
else
{


$query="insert into profile(name,address,user_name,pass,added_on) values ('$name','$address','$user_name',MD5('$pass'),NOW())";
//echo $query;

$result=mysql_query($query);

if (mysql_affected_rows()==1)
{
echo "You Are registered";

$id=mysql_insert_id();//gives last inserted id which is useful auto increment

if($_FILES['photo']['type']=='image/jpeg' or $_FILES['photo']['type']=='image/pjpeg')
{
$ext=".jpg";
}
else if ($_FILES['photo']['type']=='image/gif')
{
$ext=".gif";
}
else
{
echo "This image format is not allowed";
}

$file_name=$id.$ext;//7.jpg
$file_name1="tb".$id.$ext;//7.jpg
$query12="update profile set image='$file_name' where user_id=$id";
mysql_query($query12);//for image as image id is not initially known which is required to make file name



if (copy($_FILES['photo']['tmp_name'],"images/$file_name"))
{
echo "File Uploaded";


$sizes = getimagesize("images/$file_name");
//give image width and height//0-width 1 heigh

$thumb = imagecreatetruecolor(100, 100);//creates empty canvas
$source = imagecreatefromjpeg("images/$file_name");//this is source

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, 100, 100, $sizes[0], $sizes[1]);

// Output
//imagejpeg($thumb);
imagejpeg($thumb,"images/$file_name1");



/*$thumb1 = imagecreatetruecolor(400, 400);//creates empty canvas
$source1 = imagecreatefromjpeg("images/$file_name");//this is source

// Resize
imagecopyresized($thumb1, $source1, 0, 0, 0, 0, 400, 400, $sizes[0], $sizes[1]);

// Output
//imagejpeg($thumb);
imagejpeg($thumb1,"images/$file_name");*/


}
else
{
echo "File could not be uploaded";
}



}
else
{
echo "You Are NOT registered";
}
}




}

else
{

$errors_num=count($errors);
for($i=0;$i<$errors_num;$i++)
{
echo "{$errors[$i]}<br/>";
}
}


}//end of submit






?>





<form action="register.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>Name
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>Address
<input type="text" name="address" id="address" />
</p>

<p>Education
<label>
<select name="education">
<option value="1">Under Slc</option>
<option value="2">Intermediate</option>
<option value="3">Above Intermediate</option>
</select>
</label>
</p>

<p>Gender:
<label>
<input type="radio" name="gender" value="1" checked="checked"/>
Male</label>
<label>
<input type="radio" name="gender" value="2"/>
Female</label>
<br />
</p>
<p>Hobbies
<label>
<input type="checkbox" name="hobbies[]" value="playing" />
Playing</label>
<label>
<input type="checkbox" name="hobbies[]" value="Singing" />
Singing</label>
<br />
</p>
<p>Image
<label>
<input type="file" name="photo" />
</label>
</p>
<p>Username
<input type="text" name="user_name" id="user_name" />
</p>
<p>Password
<input type="password" name="pass" id="pass" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>


<?php
/*
mysql_connect()//connects
mysql_select_db()//
mysql_query()//passes query to mysql from php/
//CUD-Create-Unpdate-Delete---
//R-Read
mysql_affected_rows
mysql_num_rows
mysql_fetch_array
*/


?>

No comments:

Post a Comment