HELP? im new please look! php mysql question..

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
caleb1
Forum Newbie
Posts: 1
Joined: Wed Nov 19, 2008 11:00 pm

HELP? im new please look! php mysql question..

Post by caleb1 »

hey i am pretty new at this soooo, here goes i got this form validation code from awebsite..

Code: Select all

<html>
   <head>
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
      <title>
Form Page: test
      </title>
      <Style>
        BODY, P,TD{ font-family: Arial,Verdana,Helvetica, sans-serif; font-size: 10pt }
        A{font-family: Arial,Verdana,Helvetica, sans-serif;}
        B { font-family : Arial, Helvetica, sans-serif; font-size : 12px;   font-weight : bold;}
        </Style>
      </script>
   </head>
<body>
<h2>Registratin Form</h2>
 
<?PHP
require_once "formvalidator.php";
$show_form=true;
if(isset($_POST['Submit']))
{
    $validator = new FormValidator();
    $validator->addValidation("Name","req","Please fill in Name");
    $validator->addValidation("Email","email","The input for Email should be a valid email value");
    $validator->addValidation("Email","req","Please fill in Email");
    if($validator->ValidateForm())
    {
        echo "<h2>Validation Success!</h2>";
        $show_form=false;
    }
    else
    {
        echo "<B>Validation Errors:</B>";
 
        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
            echo "<p>$inpname : $inp_err</p>\n";
        }        
    }
}
 
if(true == $show_form)
{
?>
 
<form name='test' method='POST' action='' accept-charset='UTF-8'>
<table cellspacing='0' cellpadding='10' border='0' bordercolor='#000000'>
   <tr>
      <td>
         <table cellspacing='2' cellpadding='2' border='0'>
            <tr>
               <td align='right' class='normal_field'>Name</td>
               <td class='element_label'>
                  <input type='text' name='Name' size='20'>
               </td>
            </tr>
            <tr>
               <td align='right' class='normal_field'>Email</td>
               <td class='element_label'>
                  <input type='text' name='Email' size='20'>
               </td>
            </tr>
            <tr>
               <td align='right' class='normal_field'>Address</td>
               <td class='element_label'>
                  <input type='text' name='Address' size='20'>
               </td>
            </tr>
            <tr>
               <td align='right' class='normal_field'>City</td>
               <td class='element_label'>
                  <input type='text' name='City' size='20'>
               </td>
            </tr>
            <tr>
               <td colspan='2' align='left' valign='bottom' class='normal_field'>Comments</td>
            </tr>
            <tr>
               <td>
               </td>
               <td class='element_label'>
<textarea name='Comments' cols='50' rows='8'></textarea>
               </td>
            </tr>
            <tr>
               <td colspan='2' align='center'>
                  <input type='submit' name='Submit' value='Submit'>
               </td>
            </tr>
         </table>
      </td>
   </tr>
</table>
</form>
<?PHP
}//true == $show_form
?>
</body>
<html>

my question is how do i save it to the database using mysql?? Before i get plastered with why didn't you search. the reason is i really don't know what to search i am lost and really need your help! thx in advance.
nnnpuri
Forum Newbie
Posts: 14
Joined: Tue Nov 25, 2008 6:50 am

Re: HELP? im new please look! php mysql question..

Post by nnnpuri »

hi

just opy paste this code and check in mozilla,delete field you do not wish

<!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: HELP? im new please look! php mysql question..

Post by nnnpuri »

This is php validation


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

</head>


<body>

<h1>Regestration Form</h1>
<h4> * indicates must select </h4>
<?php

error_reporting(E_ALL ^ E_NOTICE);


$nameerror="";
$emailerror="";
$addresserror="";
$gendererror="";
$fileerror="";
$uploadfileerror="";
$hobbyerror="";
$stateerror="";
$cityerror="";
$usernameerror="";
$passworderror="";
$confirmpassworderror="";
$pc="";
$pwd = strip_tags(trim($_POST['password'])); //Value of Password Store
$conf_pwd = strip_tags(trim($_POST['confirm_password'])); //Value of Confirm Password Store





if (!empty($_POST['Submit']))
{


$namearray=array('name' =>$_POST['name']);
$fname=(trim($_POST['name']));

if ($fname=="")
{
$nameerror="please enter name";
}


$emailarray=array('email' =>$_POST['email']);
$femail=(trim($_POST['email']));

if ($femail=="")
{
$emailerror="please enter email";
}

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $femail))
{
$invalidemail="please enter valid email id"."<br>";
}


$addressarray=array('address' =>$_POST['address']);
$faddress=(trim($_POST['address']));

if ($faddress=="")
{
$addresserror="please enter address";
}

$genderarray=array('m2'=>$_POST['m2']);
$fgender=(isset($_POST['m2']));

if($fgender=="")
{
$gendererror="please select gender";
}


$filearray=array('file'=>$_FILES['file']);
$ffile=(trim($_FILES['file']));

if ($ffile=="")
{
$fileerror="please upload file";
}


$hobbyarray=array('hobby'=>$_POST['hobby']);
$fhobby=(isset($_POST['hobby']));

if($fhobby=="")
{
$hobbyerror="please select hobby";
}


$statearray=array('state'=>$_POST['state']);
$fstate=(trim($_POST['state']));

if($fstate=="")
{
$stateerror="please select state";
}


$cityarray=array('city'=>$_POST['city']);
$fcity=(trim($_POST['city']));

if($fcity=="")
{
$cityerror="please select city";
}


$usernamearray=array('user_name'=>$_POST['user_name']);
$fusername=(trim($_POST['user_name']));

if($fusername=="")
{
$usernameerror="please enter user name";
}


$passwordarray=array('password'=>$_POST['password']);
$fpassword=(trim($_POST['password']));

if($fpassword=="")
{
$passworderror="please enter password";
}


$confirmpasswordarray=array('confirm_password'=>$_POST['confirm_password']);
$fconfirmpassword=(trim($_POST['confirm_password']));

if($fconfirmpassword=="")
{
$confirmpassworderror="please enter Confirm password";
}


if($pwd != $conf_pwd)
{
$pc="your password and confirm password do not match"."<br>";
}


// File Upload Valditation...

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] >20000)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{


if (file_exists("D:/Training/12.11.08/images/" . $_FILES["file"]["name"]))
{


$now = time();

$path_parts = pathinfo($_FILES["file"]["name"]);
$string="naresh";
$newtry=$path_parts['basename'].$string;

$imagestring=$_FILES["file"]["name"];
$shuffled = str_shuffle($imagestring);

move_uploaded_file($_FILES["file"]["tmp_name"],
"D:/Training/12.11.08/images/" .$newtry.$now.$shuffled.$_FILES["file"]["name"]);
echo "Image Stored in: " ."D:/Training/12.11.08/images/"."<br>".$newtry.$now.$shuffled. $_FILES["file"]["name"];
}

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"D:/Training/12.11.08/images/" . $_FILES["file"]["name"]);
echo "Image Stored in: " ."D:/Training/12.11.08/images/"."<br>". $_FILES["file"]["name"];
}
}
}

else
{
//echo "Invalid file"."<br>";
$infileerror="invalid file";
}

//file upload synapse code


}
?>


<?php
error_reporting(E_ALL ^ E_NOTICE);


$mainerror="";
if(
($nameerror=="") &&
($emailerror=="")&&
($addresserror=="")&&
($gendererror=="")&&
($hobbyerror=="")&&
($stateerror=="")&&
($cityerror=="")&&
($usernameerror=="")&&
($passworderror=="")&&
($confirmpassworderror=="")&&
($pc=="")&&
($fileerror=="")&&
($infileerror=="")&&
($invalidemail=="")
)

{


echo "<br>"."Name :{$namearray['name']}"."<br>";
echo "Email :{$emailarray['email']}"."<br>";
echo "Address :{$addressarray['address']}"."<br>";
echo "Gender :{$genderarray['m2']}"."<br>";
echo "File :{$filearray['file']}"."<br>";
echo "Hobby :{$hobbyarray['hobby']}"."<br>";
echo "State :{$statearray['state']}"."<br>";
echo "City :{$cityarray['city']}"."<br>";
echo "Username :{$usernamearray['user_name']}"."<br>";
echo "Password :{$passwordarray['password']}"."<br>";
echo "Confirm Password :{$confirmpasswordarray['confirm_password']}"."<br>";

}
else
{
$mainerror="Please correct bellow mention error and try again ! Thanks";
}
?>
<table>



<tr><td><font color="#CC0066" size="+2"><?php echo $mainerror; ?></font></td></tr><br />
<tr><td><font color="#FF0000" size="+1"><?php echo $pc; ?></font></td></tr><br />


<form method="POST" ENCTYPE="multipart/form-data" action="">


<tr>
<td> <b> Name : </b> </td>
<td><input name="name" type="text" id="name" maxlength="20" value="<?php echo $_POST['name'] ?>"/>*(Maxlength 20)</td>
<td> <font color="#FF0000" size="+1"><?php echo $nameerror; ;?></font></td>
</tr>

<tr>
<td><b> Email : </b> </td>
<td><input name="email" type="text" id="email" maxlength="20" value="<?php echo $_POST['email'] ?>" />*(Maxlength 20)</td>
<td> <font color="#FF0000" size="+1"><?php echo $emailerror; ;?></font></td>
<td> <font color="#FF0000" size="+1"><?php echo $invalidemail; ;?></font></td>
</tr>

<tr>
<td><b> address : </b> </td>
<td><input name="address" id="address" value="<?php echo $_POST['address'] ?>"/>*(Maxlength 40)</td>
<td> <font color="#FF0000" size="+1"><?php echo $addresserror; ;?></font></td>
</tr>

<tr>
<td><b> Gender : </b> </td>
<td><input type="radio" name="m2" value="Male" id="rdmale" <?php echo $_POST['m2'] ?>/>
Male
<input type="radio" name="m2" value="Female" id="rdfemale">
Female &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;( * )</td>
<td> <font color="#FF0000" size="+1"><?php echo $gendererror; ;?></font></td>
</tr>

<tr>
<td> <b>Upload Photo : </b> </td>
<td><input type="file" name="file" id="file" value=""/> *(Upload Only .jpg,.jpeg,.gif,.png,.bmp files)</td>
<td><font color="#FF0000" size="+1"><?php echo $fileerror; ;?></font></td>
<td><font color="#FF0000" size="+1"><?php echo $infileerror; ?></font></td>
</tr>

<tr>
<td><b> Hobby : </b> </td>
<td><input type="checkbox" name="hobby" value="cricket" id="chk1" <?php echo $_POST['hobby'] ?> />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 &nbsp; &nbsp; &nbsp;*(Select Minimum 1) </td>
<td> <font color="#FF0000" size="+1"><?php echo $hobbyerror; ;?></font></td>
</tr>

<tr>
<td> <b> State : </b> </td>
<td><select name="state" id="state" >
<option value="<?php echo $_POST['state'] ?>" id="s0"> Select One </option>
<option id="s1">Gujarat</option>
<option id="s2">Maharashtra</option>
<option id="s3">MP</option>
</select> </td>
<td> <font color="#FF0000" size="+1"><?php echo $stateerror; ;?></font></td>
</tr>

<tr>
<td> <b> City : </b> </td>
<td><select size="5" name="city" id="city">
<option value="<?php echo $_POST['city'] ?>">ahmedabad</option>
<option id="c1">patan</option>
<option id="c2">surendranagar</option>
<option id="c3">ahmedabad</option>
<option id="c4">Mumbai</option>
<option id="c5">Rajkot</option>
<option id="c6">Wadhawan</option>
<option id="c7">Ratanpar</option>
</select>
*(Select Atleast 1)</td>
<td><font color="#FF0000" size="+1"><?php echo $cityerror; ;?></font></td>
</tr>

<tr>
<td> <b> User name : </b> </td>
<td><input type="text" name="user_name" value="<?php echo $_POST['user_name'] ?>" id="user_name" maxlength="15"/>*(Maxlength 15) </td>
<td> <font color="#FF0000" size="+1"><?php echo $usernameerror; ;?></font></td>
</tr>

<tr>
<td><b> Password : </b> </td>
<td><input type="password" name="password" id="password" maxlength="12"
value="<?php echo $_POST['password'] ?>"/>*(Maxlength 12)</td>
<td> <font color="#FF0000" size="+1"><?php echo $passworderror; ;?></font></td>
</tr>

<tr>
<td> <b> confirm password : </b> </td>
<td><input type="password" name="confirm_password" id="confirm_password" maxlength="12"
value="<?php echo $_POST['confirm_password'] ?>"/>*(Maxlength 12)</td>
<td> <font color="#FF0000" size="+1"><?php echo $confirmpassworderror; ;?></font></td>
</tr>

<tr>
<td><input type="submit" name="Submit" value="submit"/>
<input type="reset" value="Reset"/> </td>
</tr>
</form>
</table>

</body>
</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: HELP? im new please look! php mysql question..

Post by RobertGonzalez »

Wow, dude, that was a lot of code posted NOT using the code bbCode tag or php bbCode tag. Please, when posting code in the forums, wrap your code in either a

Code: Select all

tag or a

Code: Select all

tag. It will make it way easier for everyone to read.

To the original poster, what have you tried so far? Generally if the form is validated you would want to take the filtered and sanitized inputs and write them to the database using an insert query. It would look something like:

[sql]INSERT INTO
    `table_name`
VALUES 
    ('string_field_value_wrapped_in_quotes', int_value_not_in_quotes);
[/sql]

Now be aware that this is specific to MySQL (note the backticks around the table name and the quoting for string values as opposed to non-quoting of integer values).
nnnpuri
Forum Newbie
Posts: 14
Joined: Tue Nov 25, 2008 6:50 am

Re: HELP? im new please look! php mysql question..

Post by nnnpuri »

hi

i am from india and working as a php programmer
i do not understand what actually you wants to tell me,
if you wants to tell me some thing you can call me on my phone ,it is +91 9723062128

or write me

Thanks
Naresh
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: HELP? im new please look! php mysql question..

Post by RobertGonzalez »

nnnpuri wrote:hi

i am from india and working as a php programmer
i do not understand what actually you wants to tell me,
if you wants to tell me some thing you can call me on my phone ,it is +91 9723062128

or write me

Thanks
Naresh

What I am telling you is to use the code tag when posting code in the forums. Highlight the code when you post it, the click the "Code" button just above the text area. It will be posted as code (monospace letters, tabs/spaces preserved, easy to highlight and copy/paste from).
nnnpuri
Forum Newbie
Posts: 14
Joined: Tue Nov 25, 2008 6:50 am

Re: HELP? im new please look! php mysql question..

Post by nnnpuri »

ohh i see,thanks
Post Reply