problem in Entering data into database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

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

problem in Entering data into database

Post by deshetty »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello
I have problem in adding data into the database. and the form validation codes are also troubling. can anyone point what mistake i have done.
the scripts of adding contacts and related files are provided below
Thank you

[b](add_contact.php)[/b]

Code: Select all

<?
   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));
   // check icq number
   if(!empty($uin) && !ereg("^[[]]+$",$uin)){ alertMsg("Warning!","It seems you have inserted an invalid ICQ number",'false'); }
   // check zip
   if(!empty($zip) && !ereg("^[[]]{5}$",$zip)){ alertMsg("Warning!","It seems you have inserted an invalid ZIP number",'false');}
   // state
   if(!empty($state) && !eregi("^[[]]+$",$state)){   alertMsg("Warning!","It seems you have inserted an invalid state chars",'false'); }
   if($home_phone && !eregi("^(\+?)[[]]+$",$home_phone)) alertMsg("Insert valid home_phone number!",'false');
   if($work_phone && !eregi("^(\+?)[[]]+$",$work_phone)) alertMsg("Insert valid work_phone number!",'false');
   if($mobile_phone && !eregi("^(\+?)[[]]+$",$mobile_phone)) alertMsg("Insert valid mobile_phone number!",'false');
   if($fax_phone && !eregi("^(\+?)[[]]+$",$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>





config file is saved in a folder include (conf.inc)

Code: Select all

<?
$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 are as follows



Code: Select all

#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)
);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: problem in Entering data into database

Post by superdezign »

deshetty wrote:I have problem in adding data into the database. and the form validation codes are also troubling. can anyone point what mistake i have done.
You'll need to give us a hell of a lot more than that if you want some help.

What is it that your code is supposed to be doing? What is your code actually doing? Where is it going wrong?
User avatar
deshetty
Forum Newbie
Posts: 10
Joined: Mon Aug 06, 2007 11:00 pm

Post by deshetty »

Basically Its a address book where,we can enter contact details and search for contact details. entered data does not get saved in the database. and no error is shown!!!!!!...... can you tel me the method for creating tables(multiple tables)...... [s]coz[/s] because i created each tables indivisually on same database.
Thank you
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

deshetty wrote:can you tel me the method for creating tables(multiple tables)...... coz i created each tables indivisually on same database.
? The method for creating table...? "CREATE TABLE..."


Anyway, you'll need to do some debugging. Where is the failure? In PHP? In SQL? And why do you use JavaScript to display your errors?
User avatar
deshetty
Forum Newbie
Posts: 10
Joined: Mon Aug 06, 2007 11:00 pm

Post by deshetty »

k let me try
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

deshetty, can you please use php tags when you post your code, only post the relevant portion that your having problems with, and not use abbreviated words in sentences.
Post Reply