I am a PHP newbie, and keep on getting "PHP Notice: Array to string conversion" when I attempt this code given to me to loop through INSERTing items into a database. I need to loop through, because I am using an ajax script to dynamically read form field data as you add/remove fields, and pushing it through the php as $idx allows $idx to be incremented, allowing my dynamic addition of INSERTS.
Does anyone know how to alter this to make it work, and PLEASE tell me how/why/what you did so I can learn for future uses. THANKS!!
My PHP :
$sql = "INSERT INTO items (WID, model, description, cost, retail, qty) values(?, ?, ?, ?, ?, ?);";
$q = $pdo->prepare($sql);
$idx = 0;
foreach ($Model as $model_idx) {
$q->execute(array($wid,$model_idx, $Description[$idx], $Cost[$idx], $Retail[$idx], $Quantity[$idx]));
$idx++;
}
My Php/Html :
<div class="control-group <?php echo !empty($ModelError)?'error':'';?>">
<div class="controls">
<input type="text" data-type="model" name="model[]" id="model_1" placeholder="Model" value="<?php echo !empty($Model)?$Model:'';?>" class="form-control autocomplete_txt" autocomplete="off">
<?php if (!empty($ModelError)): ?>
<span class="help-inline"><?php echo $ModelError;?></span>
<?php endif; ?>
</div>
</div>
Validation/String Output :
if ( !empty($_POST)) {
// keep track validation $
$ModelError = null;
$DescriptionError = null;
$CostError = null;
$RetailError = null;
$QuantityError = null;
// keep track post values
$Model = $_POST['model'];
$Description = $_POST['description'];
$Cost = $_POST['cost'];
$Retail = $_POST['retail'];
$Quantity = $_POST['quantity'];
// validate input
$valid = true;
if (empty($Model)) {
$ModelError = 'Please enter Model';
$valid = false;
}
$Model?$Modelcontains other arrays ?