OK.
First of all, $value is never defined.
This code is a security risk because you need to sanitize your input before inserting into the database.
use $_GET or $_POST depending on how your form is set. $_REQUEST probably also includes information you wont need
Not sure what your database looks like. Should each form field be a separate row or a separate column? Your code seems to do the former, but it sounds like you'd want the latter? If it's the latter then you really would need to name your form inputs like Amir Noori noted.
Assuming you have a form like that:
<form method="POST" action="myphp.php`>
<input type="text" name="column_name_one" />
<input type="text" name="column_name_two" />
<input type="text" name="column_name_three" />
<input type="submit" name="submit" value="submit" />
then
<?php
if (isset $_POST['submit'] {
$con=mysqli_connect("","test","test","Flashcards");
$values = array();
$columns = array();
foreach($_POST[] as $key => $value) {
if (!empty($key) && $key != "submit") {
$values[] = $con->real_escape_string($value);
$columns[] = $con->real_escape_string($key);
}
}
$colStr = implode(",",$columns);
$valStr = implode("','",$values);
$myQuery = "INSERT INTO Cards($colStr) VALUES ('$valStr');
if (!$con->query($myQuery)) {
echo "Error Occured: $con->error";
}
}
?>
Now this only works when your column names are the same as your form input names. Also assumes they are all strings (varchar etc). If this is not the case then you need to handle that by simply accessing the form fields individually by name. One simple way:
<?
if (isset($_POST['name']) && !empty($_POST['name']) { //name field maps to cName column varchar
$colStr = "cName,";
$valStr = "'" . $_POST['age'] . "',"; //need quotes
}
if (isset($_POST['age']) && !empty($_POST['age']) { //age field maps to customerAge column numeric
$colStr .= "customerAge,";
$valStr .= $_POST['age'] . ","; //no quotes
}
?>
Or use array_map() to map an array of column names to form fields. Something like that might also help if you need to make sure all the post variable names are really valid column names and someone isn't trying to send you garbage. Obviously the insert will fail if the column names aren't correct but usually it's better not to let it even try to insert a bad query.