Problem in entering data into the 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

Locked
User avatar
deshetty
Forum Newbie
Posts: 10
Joined: Mon Aug 06, 2007 11:00 pm

Problem in entering data into the database

Post by deshetty »

Hello
The problem is error while entering the data to the database. validation error is also not shown.... can anybody point what mistake i have done... Here is the code


<?
include 'include/conf.inc.php';
$db = mysql_connect($host,$dbuser,$dbpwd);
mysql_select_db($dbname);
function alertMsg($msgH,$msgBody='',$goto='')
{
print "<script>\n";
print "alert('$msgH $msgBody');\n";
if($goto == 'false')
{
print "history.back();\n";
} else {
print "self.location.href='add_contact.php';\n";
}
print "</script>\n";
die();
}
?>
<html>
<head>
<title>Add contact</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
td { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px}
.alertTitle { font-family: Arial; font-size: 16px; font-weight: bold; color: #FFFFFF}
.alertbody { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; color: #CCCCCC}
a:visited { color: #0099FF; text-decoration: underline}
a:link { color: #0099FF; text-decoration: underline }
a:hover { color: #0099FF; text-decoration: underline }
-->
</style>
<link rel="stylesheet" href="include/style.inc.php" type="text/css">
</head>
<body bgcolor="#000066" text="#000000">
<?
switch($action)
{
case 'insert':
insert($first,$last,$nickname,$web,$email,$address,$home_phone,$work_phone,$mobile_phone,$fax_phone,$day,$month,$year,$comment,$company,$uin,$zip,$city,$state,$country);
break;
default:
show_form();
break;
}
?>
<?
function insert($first,$last,$nickname,$web,$email,$address,$home_phone,$work_phone,$mobile_phone,$fax_phone,$day,$month,$year,$comment,$company,$uin,$zip,$city,$state,$country)
{
GLOBAL $GLOBALS, $PHP_SELF, $address_table, $email_table, $location_table, $phone_table,$db;
if($first || $last || $nickname)
{
} else {
alertMsg("Warning!","you must fill out obligatory fields",'false');
}
$first = trim(ucfirst($first));
$last = trim(ucfirst($last));
$nickname = trim(ucfirst($nickname));
$state = strtoupper($state);
$country = trim(ucfirst($country));
$city = trim(ucfirst($city));

if(!empty($uin) && !ereg("^[[:digit:]]+$",$uin)){ alertMsg("Warning!","It seems you have inserted an invalid ICQ number",'false'); }

if(!empty($zip) && !ereg("^[[:digit:]]{5}$",$zip)){ alertMsg("Warning!","It seems you have inserted an invalid ZIP number",'false');}

if(!empty($state) && !eregi("^[[:alpha:]]+$",$state)){ alertMsg("Warning!","It seems you have inserted an invalid state chars",'false'); }
if($home_phone && !eregi("^(\+?)[[:digit:]]+$",$home_phone)) alertMsg("Insert valid home_phone number!",'false');
if($work_phone && !eregi("^(\+?)[[:digit:]]+$",$work_phone)) alertMsg("Insert valid work_phone number!",'false');
if($mobile_phone && !eregi("^(\+?)[[:digit:]]+$",$mobile_phone)) alertMsg("Insert valid mobile_phone number!",'false');
if($fax_phone && !eregi("^(\+?)[[:digit:]]+$",$fax_phone)) alertMsg("Insert valid fax number!",'false');
if($day != "NULL")
{
if(strlen($day) == 1) $day = "0".$day;
if(strlen($month) == 1) $month = "0".$month;
$birth = $year."-".$month."-".$day;
} else {
$birth = "0000-00-00";
}

$check = mysql_query("SELECT * FROM $address_table WHERE first = '$first' AND last = '$last' AND nickname = '$nickname'",$db);
$num_result = mysql_num_rows($check);
if($num_result)
{
alertMsg("Warning!","This person already exist in database",'false');
} else {
// name
$_query = "INSERT INTO $address_table (first,last,nickname,web,birth,comment,company,uin) VALUES('$first','$last','$nickname','$web','$birth','$comment','$company','$uin')";
$result = mysql_query($_query);
// get insered id
$my_id = mysql_insert_id();
// email
if($email)
{
$_email = "INSERT INTO $email_table (id_user,email) VALUES ('$my_id','$email')";
$result = mysql_query($_email);
}
// address
if($address || $zip || $city || $state || $country)
{
$_location = "INSERT INTO $location_table (id_user,address,zip,city,state,country) VALUES ('$my_id','$address','$zip','$city','$state','$country')";
$result = mysql_query($_location);
}
// phone
$_phone = "INSERT INTO $phone_table (id_user,home,work,mobile,fax) VALUES ('$my_id','$home_phone','$work_phone','$mobile_phone','$fax_phone')";
$result = mysql_query($_phone);
alertMsg("Done!","$first, $last added succesfully",'true');

}


}
// end insert function
?>
<?

function show_form()
{
GLOBAL $PHP_self, $db;
?>
<form name="form1" method="post" action="<?=$PHP_SELF?>">
<table width="600" border="0" cellspacing="1" cellpadding="1" height="30">
<tr bgcolor="#003399">
<td class="tdElenco"><b><font color="#FFFFFF">Add contact</font></b></td>
<td align="right" class="tdLittle"><a href="javascript:window.close();" class="sort">&raquo;
close</a></td>
</tr>
</table>
<table width="600" border="0" cellspacing="2" cellpadding="2">
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: company name</font></td>
<td width="429">
<input type="text" name="company">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><b><font color="#FFFFFF">:: nickname</font></b></td>
<td width="429">
<input type="text" name="nickname">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><b><font color="#FFFFFF">:: first name</font></b></td>
<td width="429">
<input type="text" name="first">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><b><font color="#FFFFFF">:: last name</font></b></td>
<td width="429">
<input type="text" name="last">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: web page</font></td>
<td width="429">
<input type="text" name="web">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: icq</font></td>
<td width="429">
<input type="text" name="uin">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: email</font></td>
<td width="429">
<input type="text" name="email">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: address</font></td>
<td width="429">
<textarea name="address" rows="3" wrap="VIRTUAL"></textarea>
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: zip (postal code)</font></td>
<td width="429">
<input type="text" name="zip" maxlength="5" size="10">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: city</font></td>
<td width="429">
<input type="text" name="city">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: state</font></td>
<td width="429">
<input type="text" name="state">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: country</font></td>
<td width="429">
<input type="text" name="country">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: home phone</font></td>
<td width="429">
<input type="text" name="home_phone">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: work phone</font></td>
<td width="429">
<input type="text" name="work_phone">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: mobile phone</font></td>
<td width="429">
<input type="text" name="mobile_phone">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: fax number</font></td>
<td width="429">
<input type="text" name="fax_phone">
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: birth date</font></td>
<td width="429">
<select name="day">
<option value="NULL" selected>Day</option>
<?
for($a =1; $a <= 31; $a++)
{
print "<option value=\"$a\">$a</option>\n";
}
?>
</select>
-
<select name="month">
<option value="NULL" selected>Month</option>
<?
for($a =1; $a <= 12; $a++)
{
print "<option value=\"$a\">$a</option>\n";
}
?>
</select>
-
<select name="year">
<option value="NULL" selected>Year</option>
<?
for($a=1960; $a <= 2010; $a++)
{
print "<option value=\"$a\">$a</option>\n";
}
?>
</select>
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171"><font color="#FFFFFF">:: comment</font></td>
<td width="429">
<textarea name="comment" rows="3" wrap="VIRTUAL"></textarea>
</td>
</tr>
<tr valign="top" bgcolor="#006699">
<td width="171">&nbsp;</td>
<td width="429">
<input type="hidden" name="action" value="insert">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</td>
</tr>
</table>
</form>
<?
}
// end form
?>
</body>
</html>





hrer is config file (conf.inc)


$host = 'localhost';
$dbuser = 'root';
$dbpwd = 'root';
$dbname = 'address_book';
$address_table = 'name_list';
$email_table = 'email';
$location_table ='location';
$phone_table = 'phone';
$colors = array('#0066BB','#006699'); // alternate colors
$linkPerPage = 20; // max results per page
?>





Database tables


# Table structure for table 'email'


CREATE TABLE email (
id int(10) NOT NULL auto_increment,
id_user int(10) DEFAULT '0' NOT NULL,
email char(255) NOT NULL,
PRIMARY KEY (id)
);


# Table structure for table 'location'

CREATE TABLE location (
id int(10) NOT NULL auto_increment,
id_user int(10) DEFAULT '0' NOT NULL,
address text NOT NULL,
zip int(5) DEFAULT '0' NOT NULL,
city varchar(255) NOT NULL,
state char(2) NOT NULL,
country varchar(255) NOT NULL,
PRIMARY KEY (id)
);


# Table structure for table 'name_list'

CREATE TABLE name_list (
id int(10) NOT NULL auto_increment,
first varchar(255) NOT NULL,
last varchar(255) NOT NULL,
nickname varchar(255) NOT NULL,
web varchar(255) NOT NULL,
birth date DEFAULT '0000-00-00' NOT NULL,
comment text NOT NULL,
company varchar(255) NOT NULL,
uin varchar(100) NOT NULL,
PRIMARY KEY (id),
KEY id (id, nickname)
);



# Table structure for table 'phone'


CREATE TABLE phone (
id int(10) NOT NULL auto_increment,
id_user int(10) DEFAULT '0' NOT NULL,
home char(255) NOT NULL,
work char(255) NOT NULL,
mobile char(255) NOT NULL,
fax char(255) NOT NULL,
PRIMARY KEY (id)
);


Thank you
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

deshetty, this is identical to your other post..

viewtopic.php?p=408655&highlight=#408655
Forum Rules wrote: 3. Do not make multiple, identical posts. This is viewed as spam and will be deleted.
Forum Rules wrote: 2.1 When asking a question, be as specific as you can be, provide the relevant code and any other information that is helpful in a concise and coherent manner. If you are not sure how to do that, read these guidelines.
You didn't use any leet speak on this post, but your other post contained some. Here is the rule for that..
Forum Rules wrote: 11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
Locked