0

I have a dynamic form that has lets say the following fields:

 <input type="text" name="firstname">
 <input type="text" name="surname">
 <textarea name="description">

Lets say I have a table on Mysql that has the same column names.

When I post I want the array to split into column names and values and then insert it into a prepared statement:

I've tried the following:

foreach($_POST as $key => $value) {
$keys .= $key.',';
$values .= $value.',';
$q.= '?,';
$type .= 's';

}
rtrim($keys,',');
rtrim($values,',');
rtrim($q,',');

 $insert_stmt = $mysqli->prepare("INSERT INTO ".$form_table." (".$keys.") VALUES (".$q.")");
 $insert_stmt->bind_param($type, $values);  
 $insert_stmt->execute();

It seems to fail on bind_param() failed

I'm guessing it's seeing $values as one long string..am I getting close?

2
  • 2
    idownvotedbecau.se/noresearch. There are many tutorials and threads in here with examples you can use for that. Commented Nov 21, 2017 at 14:23
  • Would you be able to direct to the relevant post that relates to my specific problem? Commented Nov 21, 2017 at 16:57

1 Answer 1

1
//i - integer
//d - double
//s - string
//b - BLOB

$type = '';
$keys = '';
$values = '';
foreach($_POST as $key => $value) {
    $keys .= $key.',';
    $values .= $value.',';

    //change type according to your need or based on your data type.
    $type .= 's';
}
rtrim($keys,',');
rtrim($values,',');


//use keys, type and values
Sign up to request clarification or add additional context in comments.

1 Comment

seems to fail on the build_param - $values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.