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.

$s = 'VALUES (' . join( array_fill( 0, 4, '?' ), ', ' ) . ')';

maybe?

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.


// 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 (' . join( array_fill( 0, $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. :wink:

thanks Cups and logic_earth
read you code it actually work :slight_smile:
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\ est\looptest.php on line 14

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

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)


// 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 (' . join( array_fill( 0, $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

Dear cup and earth_logic
here my code now it work :slight_smile: thank you guy so much, as your idea have any more better than this :slight_smile:
i just want i search some technique that more better :slight_smile:
<?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