Inserting data through a HTML Forms

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
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Inserting data through a HTML Forms

Post by franknu »

Ok, I am trying to insert data through a HTML forms but it is no grabing the select text, i am also getting an double entry message

Code: Select all

<?

$host = "localhost";
$username = "localhost";
$password = "abc123";
$database = "contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);



   // Business Owner

  $First_Name = addslashes($First_Name);
  $Last_Name= addslashes($Last_Name);
  $Owner_Address = addslashes($Owner_Address);
  $State= addslashes($State);
  $City= addslashes($City);
  $Zip= addslashes($Zip);
  $Tel= addslashes($Tel);
  $Email= addslashes($Email);
  $Position=addslashes($Position);
  $Comments=addslashes($Comments);
 // Business Info

 $BusinessName = Addslashes ($BusinessName);
 $Slogan = Addslashes($Slogan);
 $Business_Address = Addslashes($Business_Address);
 $Tel = Addslashes($Tel);
 $Email = Addslashes($Email);
 $Member_Status =Addslashes($Member_Status);
 $Fax =Addslashes($Fax);

 //State

 $State_search =Addslashes ($State_search);

 // Towns

 $Towns_search = Addslashes ($Towns_search);

 // Categories

   $Categories =Addslashes($Categories);




if(!$db)

{
echo " Error: could not connect to database.";

exit;
   }


                   //business owner
$sql="INSERT INTO `FirstName`(`First_Name`,`Last_Name`,`Owner_Address`,`State`,`City`, `Zip`,`Tel`, `Email`,`Comments`,`Bus_Position`,`Status`)
VALUES ('".$First_Name."','".$Last_Name."','".$Owner_Address."','".$State."','".$City."','".$Zip."','".$Tel."','".$Email."','".$Comments."','".$Bus_Position."','".$Status."')";

$result = mysql_query($sql);
echo mysql_error();



if($result)

         {
echo mysql_affected_rows()." .Business Owner information Inserted.";
          }


    //Business Info

 $sql="INSERT INTO `Business_Info`(`BusinessName`,`Slogan`,`Business_Address`,`Tel`,`Website`,`Email`,`Member_Status`,`Fax`)
Values('".$BusinessName."','".$Slogan."','".$Business_Address."','".$Tel."','".$Website."','".$Email."','".$Member_Status."','".$Fax."')";



$result = mysql_query($sql);
echo mysql_error();

if($result)

         {
echo mysql_affected_rows(). ".Business Information Inserted.";
          }

         //State
   $sql = " INSERT INTO `State_search`(`State_search`)
    VALUES('".$State_search."')";

     $result = mysql_query($sql);
echo mysql_error();

if($result)

         {
echo mysql_affected_rows(). ".State Information Inserted.";
          }

// Towns
     $sql = " INSERT INTO `Towns_search`(`Towns_search`)
    VALUES('".$Towns_search."')";

     $result = mysql_query($sql);
echo mysql_error();

if($result)

         {
echo mysql_affected_rows(). ".Towns Information Inserted.";
          }


   // Category

    $sql = " INSERT INTO `Categories`(`Categories`)
    VALUES('".$Categories."')";

     $result = mysql_query($sql);
echo mysql_error();

if($result)

         {
echo mysql_affected_rows(). ".Categories Information Inserted.";
          }



?>
this is the message that i am getting

1 .Business Owner information Inserted.1.Business Information Inserted.Duplicate entry '' for key 11.Towns Information Inserted.1.Categories Information Inserted.

the State_search and the Towns_search are the one i need to intruduce into the database because the others are going through fine
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

You need to first check your database to see what are set for unique keys, and make sure that each column can ONLY have unique values. If they sometimes will have the same set them to reg.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your 1State_search` field is either a primary key or unique key. It looks like you are just throwing the state at the table regardless of whether the entry is already in the database. I also notice that you are using register_globals, you are not validating your data and you are doing absolutely no error checking at the query level. You might want to clean up some of this before putting it into production use.

But the error you are getting is because you are trying to insert a key value into the table when the value is already in the table.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

First about State_search and Towns_search. Those tables probably need a primary key that autoincrements added to them. I am not sure what they are, but it seems like they need to be keyed to the business. To do that you would do call mysql_insert_id() after doing the business insert and then use that ID as a "businessID" field in the State_search and Towns_search.

The same is probably true of the person. After inserting the person you could get the ID and add it to the business table as "ownerID".

Also, if you are going to insert data into mysql you should use the database specific function to do that which is: mysql_real_escape_string().

I would also recommend filtering the request vars with preg_replace() and using the more current $_POST superglobal rather than depending on registered vars.
(#10850)
Post Reply