catching and insert to DB

Discussion in 'PHP' started by roice, Feb 22, 2011.

  1. #1
    Hello,
    I have form with lots of fields - The users fill them and when he submit the forum the data collected with $_POST , get check, and if it all ok - insert into the DB.
    Is there any fast way to collected all the user data and insert it into the DB instead of doing:
    $va1 = $_POST['var1'];
    $va2 = $_POST['var2'];
    $va3 = $_POST['var3'];
    ....
    ..
    .
    INSERT INTO table_name (var1, var2, var3,...)
    VALUES (var1, var2, var3,...) 
    PHP:

    ?
     
    roice, Feb 22, 2011 IP
  2. Daws0n

    Daws0n Peon

    Messages:
    384
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think the code above is perfect, if someone will point out any other way to speed up things, then it would be helpful for me too
     
    Daws0n, Feb 22, 2011 IP
  3. arengin

    arengin Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Check in the separate connected file (check.php), if all is carried out to add in a database

    
    include "check.php";
    if($check==true){
    mysql_query("INSERT INTO `table_name` (var1, var2, var3,...) VALUES ('".$_POST['var1']."', '".$_POST['var2']."', '".$_POST['var3']."',...)");
    }
    
    PHP:
     
    arengin, Feb 22, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Just an observation based on form filling... when were dealing with lots and lots of data for collection, we usually do it in steps, ie: 1, 2, and finish... from the users end filling in little bits of information rather then quite a lot in one go, makes the process mentally seem faster.
     
    MyVodaFone, Feb 22, 2011 IP
  5. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #5
    Actually as caching in database is a term that has absolutely different meanings, I would call your question as about minimizing code.
    If you have passed all security checks on the incoming data and you are secure to use $_POST (if you confirm), then:

    
    mysql_query( 'INSERT INTO `table_name` (`'. implode('`, `', array_keys($_POST)).'`) VALUES ("'.implode('","', $_POST).'");');
    
    Code (markup):
    or to get all variables created quickly, you can simply extract the array
    extract($_POST); // = $var1 $var2 .... automatically created, use them
    Code (markup):
     
    Vooler, Feb 23, 2011 IP
  6. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i always get my post data to a variable and then pass it through a mysql_real_escape_string($user) and then place the new filtered variable in your mysql connection string

    This is done to prevent injection into your database a very important step im my opinion
     
    srisen2, Feb 23, 2011 IP