Hi guys,
Here is once again i need your help, when i checked all the the things & it doesnt work then i logon PHPhelp for asking rescue…
So guys here i need your another help,
i want to connect the data base with my form so the user can add his name, as you know i am a began-er in PHP i want to connect in this manner for practice…
I successfully connect this with dp but i dont understand why it is not adding the things,
i include the connect.php in my another php file where i make forum & from where i want that let the user add data.
following are the both php files…Please guide me…
[center]connect.php[/center]
<?php/**
- @author Mohib
- @copyright 2011
*/if(preg_match("/connect.php/",$_SERVER[‘SCRIPT_FILENAME’])){
die(“ACCESS DENIED”);
}define(“HOSTNAME”,“localhost”);
define(“USERNAME”,“root”);
define(“PWD”,"");
define(“DBNAME”,“schools”);if(!$link = mysql_connect(HOSTNAME,USERNAME,PWD)){
die(“Can’t Connect to My SQL\n”.mysql_error());
}if(!mysql_select_db(DBNAME,$link)){
die(“Can’t Select Data Base\n”.mysql_error());
}?>
[center]result.php[/center]
<?php/**
- @author Mohib
- @copyright 2011
*/include “connect.php”;
function cleanstr($string){
$string = htmlspecialchars(trim($string));
return $string;
}function print_form(){
$NewForm = “<form name=“new_record” method=“post” action=”{$_SERVER[‘PHP_SELF’]}">
";
School Name
<input type=“text” name=“Name” />
School Address
<input type=“text” name=“Address” />
School Phone No
<input type=“text” name=“Phone” />
<input type=“submit” value=“Add Record” />
<input type=“hidden” value=“true” name=“post_form” />
return $NewForm;
}function save_Rec(){
$name = cleanstr($_POST[‘Name’]);
$address= cleanstr($_POST[‘Address’]);
$phone = (int) cleanstr($_POST[‘Phone’]);if(empty($Name) || empty($address) || empty($phone)){ error_Message("Can't Insert Empty Record"); } if(strlen($Name)>49){ error_Message("Name is too long"); } if(strlen($address)>99){ error_Message("Address is too long"); } if($phone>11){ error_Message("Phone Number is too long"); }
$query = “INSERT INTO school_info(Name,Address,Phone)
VALUES (’$name’,’$address’,’$phone’)”;if(mysql_query($query)){
print “Record Added Successfully”;
print print_form();
}
else{
error_Message(mysql_error());
}}
function error_Message($error){
$msg = “$error Back”;
die($msg);
}if($_POST[‘post_form’]){
save_Rec();
}else{
print print_form();}
?>