SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question which way to loop or new technique

    Dear all
    i can not find anyway to solve my problem as below:
    i have a form post from other page:

    <?php
    $field1 = $_POST['field1'];
    $field2 = $_POST['field2'];
    $field3 = $_POST['field3'];
    $field4 = $_POST['field4'];

    $stmt = $db->prepare("INSERT INTO table(`field1`, `field2`, `field3`,`field4`) VALUES(?, ?, ?, ?)");
    $stmt->execute(array($field1, $field2, $field3, $field4));

    ?>

    what i wanted is when add fieldname the question mark (?) auto create and separate by comma but i can not find the way to loop it or create it anybody good knowledge or experience with it please help me

    thanks advance.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $s 'VALUES (' joinarray_fill04'?' ), ', ' ) . ')'
    maybe?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think your Q is about semi-automating the process of creating the query string, if so it would mean taking the incoming POST values and storing them in an array. OR, conversely, leaving the POST array alone, but building all your expectations on the POST array arriving in the correct order.
    PHP Code:
    // imagine an incoming POST load like this:

    $_POST['submit'] = "submit";
    $_POST['fname'] = "Joe";
    $_POST['lname'] = "Bloggs";

    //first, KO the submit...

    unset($_POST['submit']);

    // now what remains can be counted:

    $array_cnt count($_POST);  // 2

    // and then that number used in logic_earths solution

    $s 'VALUES (' joinarray_fill0$array_cnt'?' ), ', ' ) . ')';  

    // In conjunction with your own:

    $stmt->execute(array($_POST)); 
    All you have to do then is have the POST KEYS generate the column names for you.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks Cups and logic_earth
    read you code it actually work
    but it not working,

    $con = new PDO("mysql:host=localhost;dbname=dbfortest;charset=UTF-8","test","1234");
    $_POST['f1'];
    $_POST['f2'];
    $_POST['f3'];
    $countarr = count($_POST);
    echo $countarr;
    var_dump($countarr);
    $value = join(array_fill(0,$countarr,'?'),',');
    $table = 'test';
    $field = "field1,field2,field3";
    $sql = "INSERT INTO".$table."(".$field.") VALUE(".$countarr.")";
    $stmt = $con->prepare($sql);
    $stmt->execute(array($_POST));
    echo $con->lastInsertId();

    Notice: Array to string conversion in C:\wamp\www\sitetest.com\test\looptest.php on line 14


    all this code i'll build the class in the future so hope you guy help me
    thanks advance,

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You did not set the spoof POST variables. $_POST['fname'] = "Joe" these spoof POST values are only shown for illustrative purposes only, you would normally get these values directly from a form submission.

    This works for me (up to the creation of a valid query string anyway)
    PHP Code:
    // imagine an incoming POST load like this:
    $_POST['submit'] = "submit";
    $_POST['fname'] = "Joe";
    $_POST['lname'] = "Bloggs";

    //first, KO the submit...
    unset($_POST['submit']);

    // now what remains can be counted:
    $array_cnt count($_POST);  // 2

    // generate the table column names
    $keys array_keys($_POST);

    // start the query string 
    $sql 'INSERT INTO mytable ('.join(','$keys).') ';

    // and then that the number of ? placeholders as per logic_earths solution
    $sql .= 'VALUES (' joinarray_fill0$array_cnt'?' ), ', ' ) . ')'

    // echo it out to inspect it carefully
    echo $sql;

    // Output:
    // INSERT INTO mytable (fname,lname) VALUES (?, ?) 
    Bear in mind this will only work if:

    Your form element names exactly match those of your database table column names
    You use unset() to remove any other form elements in that form, maybe after processing
    You use the POST method for your form (or change all the POSTs to GET)
    All the necessary form elements are set (you could do this with JS)
    You build in something to handle failure caused by someone changing the form values

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    49
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Dear cup and earth_logic
    here my code now it work thank you guy so much, as your idea have any more better than this
    i just want i search some technique that more better
    <?php
    $con = new PDO("mysql:host=localhost;dbname=dbfortest;charset=UTF-8","test","1234");
    $_POST['f1'] = "a";
    $_POST['f2'] = "b";
    $_POST['f3'] = "c";
    $arr_post = count($_POST);
    $table = 'test';
    $field = array('field1','field2','field3');
    $testing = join(', ',array_values($field));
    $sql = "INSERT INTO ".$table." ( ".$testing." )"."VALUES ( ". join( array_fill(0, $arr_post,'?'),',')." )";
    $stmt = $con->prepare($sql);
    $stmt->execute($value);
    echo $con->lastInsertId();
    ?>

    thanks so much


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
  •