Confused in POST method

Discussion in 'PHP' started by yoursanjay, Jul 11, 2008.

  1. #1
    I am a fresher in PHP & I have written a script to enter user data to database but it is not working rather than no error msg has been showing. It may be for using POST method wrongly but I am confused to find out the error. Plz assist me to solve it out..
     

    Attached Files:

    yoursanjay, Jul 11, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    The attachement you have attached is not working

    please paste the code

    Regards

    Alex
     
    kmap, Jul 11, 2008 IP
  3. yoursanjay

    yoursanjay Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    <!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>Adding data to the database</title>
    </head>

    <body>
    <?php



    if (isset($_POST[$user_name]) && isset($_POST[$password]) && isset($_POST[$prefix]) && isset($_POST[$first_name]) && isset($_POST[$last_name]) && isset($_POST[$email]) && isset($_POST[$address1]) && isset($_POST[$address2]) && isset($_POST[$city]) && isset($_POST[$state]) && isset($_POST[$code]) && isset($_POST[$country]) && isset($_POST[$phone]))
    {
    $dberror="";
    $ret=add_to_database($_POST[$user_name],$_POST[$password],$_POST[$prefix],$_POST[$first_name],$_POST[$last_name],$_POST[$email],$_POST[$address1],$_POST[$address2],$_POST[$city],$_POST[$state],$_POST[$code],$_POST[$country],$_POST[$phone],$_POST[$dberror]);

    if(!$ret)
    {
    print "Error:$dberror<br/>\n";
    }
    else
    {
    print "Thank you very much!!";
    }
    }
    else
    {
    write_form();
    }

    function add_to_database($user_name,$password,$prefix,$first_name,$last_name,$email,$address1,$address2,$city,$state,$code,$country,$phone,&$dberror)
    {
    /* $user_name=mysql_real_escape_string($user_name);
    $password=mysql_real_escape_string($password);
    $prefix=mysql_real_escape_string($prefix) ;
    $first_name=mysql_real_escape_string($first_name);
    $last_name=mysql_real_escape_string($last_name);
    $email=mysql_real_escape_string($email);
    $address1=mysql_real_escape_string($address1);
    $address2=mysql_real_escape_string($address2);
    $city=mysql_real_escape_string($city);
    $state=mysql_real_escape_string($state);
    $code=mysql_real_escape_string($code);
    $country=mysql_real_escape_string($country);
    $phone=mysql_real_escape_string($phone);
    */

    $database="bluebell";
    $username="root";
    $password="";

    $connect=mysql_pconnect("localhost",$username,$password);
    if(!$connect)
    {
    $dberror="Couldn't connect to the MySql Server..";
    return false;
    }

    if(!mysql_select_db($database,$connect))
    {
    $dberror=mysql_error();
    return false;
    }

    $query="INSERT INTO user (user_name,password,prefix,first_name,last_name,email,address1,address2,city,state,code,country,phone) values ('$_POST[$user_name]','$_POST[$password]','$_POST[$prefix]','$_POST[$first_name]','$_POST[$last_name]','$_POST[$email]','$_POST[$address1]','$_POST[$address2]','$_POST[$city]','$_POST[$state]','$_POST[$code]','$_POST[$country]','$_POST[$phone]')";

    if(!mysql_query($query,$connect))
    {
    $dberror=mysql_error();
    return false;
    }
    return true;
    }

    function write_form()
    {
    global $PHP_SELF;
    print "<form action=\"$PHP_SELF\" method=\"post\"> \n";

    print "<input type='text' name='user_name'>Your User Name:\n";
    print "<p><input type='password' name='password'>Your Password:</p>";
    print "<p><select name='prefix'>";
    print "<option value='Mr.'>Mr</option>";
    print "<option value='Mrs.'>mrs</option>";
    print "<option value='Dr.'>Dr</option>";
    print "</select></p>";
    print "<p><input type='text' name='first_name'>Your First Name:</p>";
    print "<p><input type='text' name='last_name'>Your Last Name:</p>";
    print "<p><input type='text' name='email'>Your Email Id:</p>";
    print "<p><input type='text' name='address1'>Your Address Line1:</p>";
    print "<p><input type='text' name='address2'>Your Address Line2:</p>";
    print "<p><input type='text' name='city'>Your City:</p>";
    print "<p><input type='text' name='state'>Your State:</p>";
    print "<p><input type='text' name='code'>Your Postal Code:</p>";
    print "<p><input type='text' name='country'>Your Country:</p>";
    print "<p><input type='text' name='phone'>Your Phone Number:</p>";

    print "<p><input type='submit' value='Submit'></p></form>";


    }
    ?>
    </body>
    </html>
     
    yoursanjay, Jul 11, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    I don't have time to look through the code line for line but try:

    error_reporting(E_ALL);

    Get anything?

    Dan
     
    Danltn, Jul 11, 2008 IP
  5. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #5
    try to debug script by printing variables

    $query="INSERT INTO user (user_name,password,prefix,first_name,last_name,email,address1,address2,city,state,code,country,phone) values ('$_POST[$user_name]','$_POST[$password]','$_POST[$prefix]','$_POST[$first_name]','$_POST[$last_name]','$_POST[$email]','$_POST[$address1]','$_POST[$address2]','$_POST[$city]','$_POST[$state]','$_POST[$code]','$_POST[$country]','$_POST[$phone]')";

    Remove single quotes

    $query="INSERT INTO user (user_name,password,prefix,first_name,last_name,email,address1,address2,city,state,code,country,phone) values ($_POST[$user_name],$_POST[$password],$_POST[$prefix],$_POST[$first_name],$_POST[$last_name],$_POST[$email],$_POST[$address1],$_POST[$address2],$_POST[$city],$_POST[$state],$_POST[$code],$_POST[$country],$_POST[$phone])";
    Regards

    Alex
     
    kmap, Jul 11, 2008 IP