lundi 9 février 2015

PHP repeating code bad practice?

Okay i'm new into this area and have a question on good and bad practices. Lets say i have 1 form foreach of my three different pages so i have three forms in total that have ability to insert, delete, and update records in a database which would require 9 individual unique SQL statements in total. Is it bad practice to put all of these statements(9) into one 'post.php' file. The PHP and SQL statements are exactly the same for each form the only thing that changes is column names and data types. For example lets say i have a condition that inserts data from a form into a database such as:



if (isset($_POST['input'])) {
$stmt = $dbh->prepare("INSERT INTO test (firstName, lastName, Address) VALUES (:firstName, :lastName, :Address)");
$stmt->execute(array(':firstName' => $_POST['firstName'],
':lastName' => $_POST['lastName'],
':Address' => $_POST['Address']));
}


Is it bad practice to write this over and over again or can i input this into a function and just change the array values somehow??


Aucun commentaire:

Enregistrer un commentaire