Automating process of forms and generating sql queries in PHP
This is simple script for automating the process of forms after submission, generally we have to assign every submitted value to a variable, and we have to add those values to the sql queries, By using this code you can generate any kind of sql queries, but i have included only for INSERT and SELECT queries only, while using this code you can add any number of form fields to the form, it will generate the sql queries automatically.
After submitting the form, it check for the form is submitted or not, if it’s submitted it will use array pop function to delete the last element of array that is submit. After that it will loop through post array using foreach loop, then we are using implode php function to get the INSERT and SELECT sql queries.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php if (isset($_POST['submit'])){ array_pop($_POST); // array pop is for removing the last value in post array that is submit foreach ($_POST as $field => $value){ // this is used for insert sql query $values[] = $value; $fields[] = $field; //this is for the select sql query $select[] = $field ."='".$value ."'"; } // this is for insert sql query $db = implode(', ', $fields); $value = "'" .implode("', '", $values) . "'"; echo "INSERT INTO `table` ({$db}) VALUES ({$value})"; echo "<br />"; // this is for select sql query $dbvalues = implode(' and ', $select); echo "SELECT * FROM `table` WHERE {$dbvalues}"; } ?> <html> <head> <title>Automating process of form submission and generating sql queries</title> </head> <body> <form action="" method="post"> <input type="text" name="user" value="user" /> <input type="password" name="pass" name="pass" value="pass"/> <input type="text" name="email" value="email" /> <input type="text" name="state" value="state" /> <input type="text" name="country" value="country" /> <input type="text" name="pin" value="pincode" /> <input type="submit" name="submit" value="submit" /> </form> </body> </html> |