If you want to use an array use it in a HTML form like the following:
<form id="formData" class="checkStyle" action="create_procedure.php" method="post">
<input type="hidden" name="cms[user_id]" value="2">
<input type="hidden" name="cms[author]" value="<?= Login::full_name() ?>">
<input type="hidden" name="action" value="upload">
<input type="hidden" name="cms[page]" value="blog">
<div class="heading-style">
<label class="heading_label_style" for="heading">Heading</label>
<input class="enter_input_style" id="heading" type="text" name="cms[heading]" value="" tabindex="1" required
autofocus>
</div>
<div class="content-style">
<label class="text_label_style" for="content">Content</label>
<textarea class="text_input_style" id="content" name="cms[content]" tabindex="2"></textarea>
</div>
<div class="submit-button">
<button class="form-button" type="submit" name="submit" value="enter">submit</button>
</div>
</form>
Here’s how I insert the data using php:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = $_POST['cms'];
try {
$today = $todayDate = new DateTime('today', new DateTimeZone("America/Detroit"));
} catch (Exception $e) {
}
$data['date_updated'] = $data['date_added'] = $today->format("Y-m-d H:i:s");
$cms = new CMS($data);
$result = $cms->create();
if ($result) {
header("Location: gallery.php");
exit();
}
}
Sorry that’s from a different PHP script, but the main thing is $data = $_POST['cms'];
which is the array that would contain the data and used in the function
Very easy to do.
That way you could do something like this with the PHP code:
<?php
/*
* As long as you have the correct field names as the key and
* the correct values in the corresponding keys the following
* procedural function should work with no problem.
*
*/
function insertData(array $data, $pdo, $table) {
try {
/* Initialize an array */
$attribute_pairs = [];
/*
* Set up the query using prepared states with the values of the array matching
* the corresponding keys in the array
* and the array keys being the prepared named placeholders.
*/
$sql = 'INSERT INTO ' . $table . ' (' . implode(", ", array_keys($data)) . ')';
$sql .= ' VALUES ( :' . implode(', :', array_keys($data)) . ')';
/*
* Prepare the Database Table:
*/
$stmt = $pdo->prepare($sql);
/*
* Grab the corresponding values in order to
* insert them into the table when the script
* is executed.
*/
foreach ($data as $key => $value)
{
if($key === 'id') { continue; } // Don't include the id:
$attribute_pairs[] = $value; // Assign it to an array:
}
return $stmt->execute($attribute_pairs); // Execute and send boolean true:
} catch (PDOException $e) {
/*
* echo "unique index" . $e->errorInfo[1] . "<br>";
*
* An error has occurred if the error number is for something that
* this code is designed to handle, i.e. a duplicate index, handle it
* by telling the user what was wrong with the data they submitted
* failure due to a specific error number that can be recovered
* from by the visitor submitting a different value
*
* return false;
*
* else the error is for something else, either due to a
* programming mistake or not validating input data properly,
* that the visitor cannot do anything about or needs to know about
*
* throw $e;
*
* re-throw the exception and let the next higher exception
* handler, php in this case, catch and handle it
*/
if ($e->errorInfo[1] === 1062) {
return false;
}
throw $e;
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n"; // Not for a production server:
}
return true;
}
function updateData(array $data, $pdo, $table): bool
{
/* Initialize an array */
$attribute_pairs = [];
/* Create the prepared statement string */
foreach ($data as $key => $value)
{
if($key === 'id') { continue; } // Don't include the id:
$attribute_pairs[] = "$key=:$key"; // Assign it to an array:
}
/*
* The sql implodes the prepared statement array in the proper format
* and updates the correct record by id.
*/
$sql = 'UPDATE ' . $table . ' SET ';
$sql .= implode(", ", $attribute_pairs) . ' WHERE id =:id';
/* Normally in two lines, but you can daisy-chain pdo method calls */
$pdo->prepare($sql)->execute($data);
return true;
}
I don’t use that PHP code any longer, but I think you should get the idea. Yes, you still have to setup all the columns in the database table and have the corresponding data index (HTML array), but doing it that way is some much easier when inserting and updating the database table.