SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: Form to MSQL

  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form to MSQL

    Hi please someone help im trying to create a form to add the data to mysql
    I know it is connecting to the database but it isnt adding the info. and on top of that im not getting any errors.

    here is mysql dump
    Code:
    CREATE TABLE Customer Info (
      customer_number int(10) unsigned NOT NULL auto_increment,
      first_name varchar(15) NOT NULL default '',
      last_name varchar(15) NOT NULL default '',
      address1 varchar(30) NOT NULL default '',
      address2 varchar(30) NOT NULL default '',
      city varchar(15) NOT NULL default '',
      state char(2) NOT NULL default '',
      zip varchar(5) NOT NULL default '',
      home_phone varchar(12) NOT NULL default '',
      other_phone varchar(12) NOT NULL default '',
      email varchar(30) NOT NULL default '',
      source varchar(30) NOT NULL default '',
      UNIQUE KEY customer_number (customer_number)
    ) TYPE=MyISAM COMMENT='PC Diagnostics Customer Info';
    this is my config.php
    PHP Code:
    <?php
    // Database conection setings
    $dbhost "localhost";
    $dbuser "dbuser";
    $dbpass "dbpass";
    $dbname "dbname";

    // makes the conection to the db
    function dbopen($dbname "" ){
    global 
    $dbuser,$dbpass,$dbhost,$dbname;

    //Made the connection
    $startconnection = @mysql_connect($dbhost$dbuser$dbpass) or print "Unable to connect to the Database.";
    @
    mysql_select_db($dbname$startconnection) or print "Unable to select database";
    return 
    $startconnection;
    }
    ?>
    and this is my form.php
    PHP Code:
    require 'config.php';
    $date date("F d, Y" );
    $dbcnx dbopen();

    // Insert Form data into database
    mysql_query("INSERT INTO Customer Info SET first_name='{$_POST['Name']}',city='{$_POST['City']}',home_phone='{$_POST['Home_Phone']}',
    address1='
    {$_POST['Street_Address']}',address2='{$_POST['Address2']}',email='{$_POST['Email']}',
    ,zip='
    {$_POST['Zip_Code']}'");

    ?> 
    what am i doing wrong please help.

  2. #2
    SEO Addict eli03's Avatar
    Join Date
    Aug 2004
    Location
    Pasig City Cainta, Philippines
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    [PHP]

    and this is my form.php
    PHP Code:
    require 'config.php';
    $date date("F d, Y" );
    $dbcnx dbopen();

    // Insert Form data into database
    mysql_query("INSERT INTO Customer Info SET first_name='$_POST[Name]',city='$_POST[City]',home_phone='$_POST[Home_Phone]',
    address1='
    $_POST[Street_Address]',address2='$_POST[Address2]',email='$_POST[Email]',
    ,zip='
    $_POST[Zip_Code]'") or die(mysql_error());

    ?> 

  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope that didnt work either still no mysql entries and no errors.

  4. #4
    SEO Addict eli03's Avatar
    Join Date
    Aug 2004
    Location
    Pasig City Cainta, Philippines
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try to make variable before inserting it to the databases ex.

    $address1=$_POST['Street_Address'];

  5. #5
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do what eli03 said or put {} aroung your _POST vars in the query:
    PHP Code:
    mysql_query("INSERT INTO Customer Info SET first_name='{$_POST['Name']}', city='{$_POST['City']}',
    home_phone='
    {$_POST['Home_Phone']}', address1='{$_POST['Street_Address']}',
    address2='
    {$_POST['Address2']}', email='{$_POST['Email']}'
    ,zip='
    {$_POST['Zip_Code']}'") or die(mysql_error()); 
    also, remove the double commas before zip=

  6. #6
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i tried that and now i am getting an error

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/pdupuis/propchelp.com/forms/form.php on line 349

    this is what i have

    PHP Code:
    mysql_query("INSERT INTO Customer Info SET $Gender=$_POST['Gender'];,$First Name=$_POST['First_Name'];,
    $Last_Name=$_POST['Last_Names'];,$Street_Address=$_POST['Street_Address'];,$Address_Cont=$_POST['Address_Cont'];,
    $City=$_POST['City'];,$State=$_POST['State'];,$Zip_Code=$_POST['Zip_Code'];,$Home_Phone=$_POST['Home_Phone'];,
    $Cell_Phone=$_POST['Cell_Phone'];,$Email=$_POST['Email'];,$Description=$_POST['Description'];,
    $Referal=$_POST['Referal'];,$Technician=$_POST['Technician'];") or die(mysql_error()); 

    I modified it to have more fields so i had to recreate mysql and im getting a syntax erre this is what i have for that.

    Code:
    CREATE TABLE Customer Info (
      Gender varchar(15) NOT NULL default '',
      First_Name varchar(15) NOT NULL default '',
      Last_Name varchar(15) NOT NULL default '',
      Street_address varchar(30) NOT NULL default '',
      Address_Cont varchar(30) NOT NULL default '',
      City varchar(15) NOT NULL default '',
      State char(2) NOT NULL default '',
      Zip_Code varchar(5) NOT NULL default '',
      home_phone varchar(12) NOT NULL default '',
      Cell_Phone varchar(12) NOT NULL default '',
      Email varchar(30) NOT NULL default '',
      Description varchar(30) NOT NULL default '',
      Referal varchar(30) NOT NULL default '',
      Technician varchar(30) NOT NULL default '',
      source varchar(30) NOT NULL default '',
      UNIQUE KEY customer_number (customer_number)
    ) TYPE=MyISAM COMMENT='PC Diagnostics Customer Info';

  7. #7
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) remove the semi-colons
    2) either replace the _POST vars with other vars before the query -or- surround them with curly brackets

  8. #8
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok well aparently it had a problem with the $ in there so i took them out and now im not getting an error anymore the bad new is I have no tables in the database cause mysql syntax is still wrong. you would think that i would get table not found errors when submitting the form but i dont. so that must mean something else is wrong.

    so far i have my form.php
    PHP Code:
    require 'config.php';
    $date date("F d, Y" );
    $dbcnx dbopen();

    // Insert Form data into database
    mysql_query("INSERT INTO Customer Info SET Prefix=_POST['Gender'],First Name=_POST['First_Name'],
    Last_Name=_POST['Last_Names'],Street_Address=_POST['Street_Address'],Address_Cont=_POST['Address_Cont'],
    City=_POST['City'],State=_POST['State'],
    $Zip_Code=_POST['Zip_Code'],Home_Phone=_POST['Home_Phone'],
    Cell_Phone=_POST['Cell_Phone'],Email=_POST['Email'],Description=_POST['Description'],
    Referal=_POST['Referal'],Technician=_POST['Technician']"
    ) or die(mysql_error()); 
    and mysql dump which doesnt work either

    Code:
    CREATE TABLE Customer Info (
      Prefix {varchar}(15) NOT NULL default '',
      First_Name {varchar}(15) NOT NULL default '',
      Last_Name {varchar}(15) NOT NULL default '',
      Street_address {varchar}(30) NOT NULL default '',
      Address_Cont {varchar}(30) NOT NULL default '',
      City {varchar}(15) NOT NULL default '',
      State {varchar}(2) NOT NULL default '',
      Zip_Code {varchar}(5) NOT NULL default '',
      home_phone {varchar}(12) NOT NULL default '',
      Cell_Phone {varchar}(12) NOT NULL default '',
      Email {varchar}(30) NOT NULL default '',
      Description {varchar}(30) NOT NULL default '',
      Referal {varchar}(30) NOT NULL default '',
      Technician {varchar}(30) NOT NULL default '',
      source {varchar}(30) NOT NULL default '',
      UNIQUE KEY customer_number (customer_number)
    ) TYPE=MyISAM COMMENT='PC Diagnostics Customer Info';
    what is going on here i must be missing something.

  9. #9
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    break up large steps into lines and into multiple steps. you're accomplishing nothing - not even runtime - by jumpling it all together. make it easier on yourself and others to find the problem:
    PHP Code:
    $name       $_POST['Name'];
    $city       $_POST['City'];
    $home_phone $_POST['Home_Phone'];
    $address1   $_POST['Street_Address'];
    $address2   $_POST['Address2'];
    $email      $_POST['Email'];
    $zip        $_POST['Zip_Code'];

    $query "INSERT INTO `Customer Info` SET
              first_name = '
    $name',
              city       = '
    $city',
              home_phone = '
    $home_phone',
              address1   = '
    $address1',
              address2   = '
    $address2',
              email      = '
    $email',
              zip        = '
    $zip'";
    mysql_query($query) or die(mysql_error()); 

  10. #10
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok sorry about that i did it and still cant get it to work here is what i have

    PHP Code:
    require 'config.php';
    $date date("F d, Y" );
    $dbcnx dbopen();

    // Insert Form data into database


    $Prefix           $_POST['Prefix'];
    $First_Name       $_POST['First_Name'];
    $Last_Name        $_POST['Last_Name'];
    $Street_Address   $_POST['Street_Address'];
    $Address_Cont     $_POST['Address_Cont'];
    $City             $_POST['City'];
    $State            $_POST['State'];
    $Zip_Code         $_POST['Street_Address'];
    $Home_Phone       $_POST['Home_Phone'];
    $Cell_Phone       $_POST['Cell_Phone'];
    $Email            $_POST['Email'];
    $Description      $_POST['Description'];
    $Referal          $_POST['Referal'];
    $Technician       $_POST['Technician'];

    $query "INSERT INTO `Customer Info` SET
              Prefix           = '
    $Prefix',
              First_Name       = '
    $First_Name',
              Last_Name        = '
    $Last_Name',
              Street_Address   = '
    $Street_Address',
              Address_Cont     = '
    $Address_Cont',
              City             = '
    $City',
              State            = '
    $State'
              Zip_Code         = '
    $Zip_Code',
              Home_Phone       = '
    $Home_Phone',
              Cell_Phone       = '
    $Cell_Phone',
              Email            = '
    $Email',
              Description      = '
    $Description',
              Referal          = '
    $Referal',
              Technician       = '
    $Technician'";

    mysql_query($query) or die(mysql_error());


    ?> 

  11. #11
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you have phpmyadmin? i highly recommend it for all php+mysql use, simple and advanced. play around with that and see if you can get the right query to work in it

  12. #12
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, make sure of the simple things. like your $_POST reaching the script ok and being able to connect to the db with a simple query

  13. #13
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i do have phpmyadmin and i have been playing with getting the right quary to work for the past 7 hours nothing i try works

  14. #14
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i believe it is reaching the database cause the first time i tried it i made a mastake. i had a mis type in the database name and got an error unable to connect to database and i cheged it and i never got the error again.

  15. #15
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Afterburn25
    well i believe it is reaching the database cause the first time i tried it i made a mastake. i had a mis type in the database name and got an error unable to connect to database and i cheged it and i never got the error again.
    don't guess when you can test. a lack of error is not a test - a positive result is. have it read and display something simple from the db, just so you know it's working right. a simple SELECT will do. do the same with _POST. run this to make sure it's passing everything you think it's passing:
    PHP Code:
    echo('<pre>');
    print_r($_POST);
    echo(
    '</pre>'); 
    after the results prove positive, you can delete them but it's better to know for sure that it works

  16. #16
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darchangel
    don't guess when you can test. a lack of error is not a test - a positive result is. have it read and display something simple from the db, just so you know it's working right. a simple SELECT will do. do the same with _POST. run this to make sure it's passing everything you think it's passing:
    PHP Code:
    echo('<pre>');
    print_r($_POST);
    echo(
    '</pre>'); 
    after the results prove positive, you can delete them but it's better to know for sure that it works
    ok well it is working now and i know for sure it is passing the data but the problem is i want to move it from my form.php page to my success page that way it will only add to the database if it is error free and the success page will only load if it doesnt have errors. I am trying to work with sessions. but everytime i set it up it sends a blank row to the database so somehow the data isnt getting passed.

    here is an example of my form.php
    PHP Code:
     session_start();
    $_SESSION['var'] = 'Email'
    and my success.php
    PHP Code:
     session_start();
    echo 
    $_SESSION['var'];
    $_SESSION['Email'] = 'Email'
    any help would be greatly appretiated

  17. #17
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'd love to help you but i'm confused as to the problem. let me say how i see your issue and please correct any mis-assumptions:
    - you have data that needs to get to a certain page
    - on this page, an insert query will be run
    - the data exists in the form of variables
    - the variables need to be parsed into the insert query
    - the query needs to be run successfully on a db

    i'd go through and see which part is not happening, step by step. you said that the data was arriving at the correct page and that the script could connect to the db. here are some other tests. perform each test separately (once all of these pass, i see no reason that your script shouldn't work):
    1) outside of your script, create an sql query that does what you want it to do. run it in phpmyadmin to be sure it works perfectly. we'll call this: $hardcoded_query
    2) in your script, just prior to the insert query, echo all variables that need to be used by the query. if this can't happen, you know that not all of the data is reaching where it needs to be
    3) have your script parse the variables into your desired sql query. we'll call the resulting sql: $softcoded_query. echo $softcoded_query (don't bother actually running it on the db yet). make sure $softcoded_query is identical to $hardcoded_query. if it's not identical, look to see why
    4) hard-code $hardcoded_query into your script. this time, actually run it on the db. since you already know that the query is perfect, then if this doesn't work, you know there's a db connectivith issue
    5) since you proved in step 3 that $hardcoded_query and $softcoded_query are interchangable, you should be able to swap them out
    6) script should now work

  18. #18
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no you had everything i want to do correct but the problem is i know the data is not arriving to the page i want i know this cause it does insert into the database. however it inserts a blank row. i tried implamenting this insert on the actual form page and it worked like a charm so that tells me the data is not being passed to the next page

  19. #19
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps if you did your query string right... The SET is if you are updating a record. You are wanting to INSERT a new one... Try this:

    PHP Code:
    $sql "INSERT INTO 'Customer Info' (Prefix, First_Name, Last_name, Street_Address, Address_Cont, City, State, Zip_Code, Home_Phone, Cell_Phone, Email, Description, Referal, Technician) VALUES ('$Prefix', '$First_Name', '$Last_Name', '$Street_Address', '$Address_Cont', '$City', '$State', '$Zip_Code', '$Home_Phone', '$Cell_Phone', '$Email', '$Description', '$Referal', '$Technician')";

    $query mysql_query($sql) or die(mysql_error()); 
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •