php data entry

Insert php page:
<?php require_once('incMultiDBConnection.php'); ?>
<?php 


$insert = $db->prepare("INSERT INTO inventasequipment (equip_companyname, equip_name, equip_type, equip_sn, equip_productno, equip_website, equip_support, equip_use, equip_purchasedate, equip_warrenty, equip_performance, equip_comments, equip_cost, equip_datevisited, equip_timefrom, equip_timeto) VALUES (:company, :name, :type, :sn, :pn, :web, :support, :use, :purchase, :warrenty, :perf, :comments, :cost, :visitdate, :timefrom, :timeto)");

foreach ($_POST['equip'] as $equipment):
    $insert->execute([
        'company' => $equipment['companyname'],
        'name' => $equipment['equipname'],
        'type' => $equipment['equiptype'],
        'sn' => $equipment['equipsn'],
        'pn' => $equipment['equippn'],
        'web' => $equipment['equipweb'],
        'support' => $equipment['equipsupport'],
        'use' => $equipment['equipuse'],
        'purchase' => $equipment['equippurchase'],
        'warrenty' => $equipment['equipwarrenty'],
        'perf' => $equipment['equipperf'],
        'comments' => $equipment['equipcomments'],
        'cost' => $equipment['equipcost'],
        'visitdate' => $equipment['equipvisit'],
        'timefrom' => $equipment['equiptimefrom'],
        'timeto' => $equipment['equiptimeto'],
    ]);
endforeach;



?>

Open in new window


The above code works in that the first set of data is passed to the database correctly but the second set is only passed to the database with the first character of the text string from each text input box from the input form:

Input form:
<form action="sitesMultiInsertEquipment.php" method="post" name="dataentry">
    <h2>Site Equipment Multi Insert:</h2><br /><br />
    <label>Data Set 1  :</label><br />
<input type="text" name="equip[1][companyname]" required="required" placeholder="companyname">
    <input type="text" name="equip[1][equipname]" required="required" placeholder="equipment name">
    <input type="text" name="equip[1][equiptype]" required="required" placeholder="equipment type">
<input type="text" name="equip[1][equipsn]" required="required" placeholder="equipment serialnumber">
<input type="text" name="equip[1][equippn]" required="required" placeholder="equipment productnumber">
    <input type="text" name="equip[1][equipweb]" placeholder="equipment website">
<input type="text" name="equip[1][equipsupport]" placeholder="equipment support">
<input type="text" name="equip[1][equipuse]" placeholder="equipment use">
    <input type="text" name="equip[1][equippurchase]" placeholder="equipment purchase">
    <input type="text" name="equip[1][equipwarrenty]" placeholder="equipment warrenty">
<input type="text" name="equip[1][equipperf]" placeholder="equipment performance">
    <input type="text" name="equip[1][equipcomments]" required="required" placeholder="equipment comments">
    <input type="text" name="equip[1][equipcost]" placeholder="equipment cost">
    <input type="text" name="equip[1][equipvisit]" placeholder="equipment visitdate">
    <input type="text" name="equip[1][equiptimefrom]" id="timefrom" placeholder="equipment visit time from"><br />
    
    <input type="text" name="equip[1][equiptimeto]" id="timeto" placeholder="equipment visit time to"><br />
    
    
    <label>Data Set 2  :</label><br />
   <input type="text" name="equip[2[companyname]" required="required" placeholder="companyname">
    <input type="text" name="equip[2][equipname]" required="required" placeholder="equipment name">
    <input type="text" name="equip[2][equiptype]" required="required" placeholder="equipment type">
<input type="text" name="equip[2][equipsn]" required="required" placeholder="equipment serialnumber">
<input type="text" name="equip[2][equippn]" required="required" placeholder="equipment productnumber">
    <input type="text" name="equip[2][equipweb]" placeholder="equipment website">
<input type="text" name="equip[2][equipsupport]" placeholder="equipment support">
<input type="text" name="equip[2][equipuse]" placeholder="equipment use">
    <input type="text" name="equip[2][equippurchase]" placeholder="equipment purchase">
    <input type="text" name="equip[2][equipwarrenty]" placeholder="equipment warrenty">
<input type="text" name="equip[2][equipperf]" placeholder="equipment performance">
    <input type="text" name="equip[2][equipcomments]" required="required" placeholder="equipment comments">
    <input type="text" name="equip[2][equipcost]" placeholder="equipment cost">
    <input type="text" name="equip[2][equipvisit]" placeholder="equipment visitdate">
    <input type="text" name="equip[2][equiptimefrom]" id="timefrom2" placeholder="equipment visit time from">

    <input type="text" name="equip[2][equiptimeto]" id="timeto2" placeholder="equipment visit time to"><br />
      
    
    <input type="submit" value=" Submit " name="submit"/><br />
    
</form>
 

Open in new window


The error after submission is as follows:

Warning: Illegal string offset 'companyname' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 9

Warning: Illegal string offset 'equipname' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 10

Warning: Illegal string offset 'equiptype' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 11

Warning: Illegal string offset 'equipsn' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 12

Warning: Illegal string offset 'equippn' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 13

Warning: Illegal string offset 'equipweb' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 14

Warning: Illegal string offset 'equipsupport' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 15

Warning: Illegal string offset 'equipuse' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 16

Warning: Illegal string offset 'equippurchase' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 17

Warning: Illegal string offset 'equipwarrenty' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 18

Warning: Illegal string offset 'equipperf' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 19

Warning: Illegal string offset 'equipcomments' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 20

Warning: Illegal string offset 'equipcost' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 21

Warning: Illegal string offset 'equipvisit' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 22

Warning: Illegal string offset 'equiptimefrom' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 23

Warning: Illegal string offset 'equiptimeto' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 24

Notice: Undefined index: companyname in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 9

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'equip_companyname' cannot be null' in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php:25 Stack trace: #0 C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php(25): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\Development\Inventas\Sites\sitesMultiInsertEquipment.php on line 25

Open in new window


This is the DB Connect page:
<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$username = 'root';
$password = 'xxxxxx';
$dsn      = 'mysql:host=localhost;dbname=inventas;charset=utf8mb4';

$options  = [
    PDO::ATTR_EMULATE_PREPARES   => false,
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
];

try {
    $db = new PDO($dsn, $username, $password, $options);
} catch(PDOException $e) {
    die( $e->getMessage() );
}

?>

Open in new window


I have checked and rechecked spelling and format but cannot locate where the error is
doctorbillTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
What does "print_r($_POST['equip']);" show?
0
doctorbillTechAuthor Commented:
Where do I put this string
0
doctorbillTechAuthor Commented:
I get the error "Page isn't working"
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

Chris StanyonWebDevCommented:
Line 24 of your input page - you have a typo:

<input type="text" name="equip[2[companyname]" required="required" placeholder="companyname">

You're missing the closing bracket after equip[2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David S.Commented:
It's not a string. I was just posting code without using a codeblock. If the error Chris spotted isn't the only one, put this at the very beginning of your file.
<?php
header('Content-Type: text/plain');
print_r($_POST['equip']);
exit(0);
?>

Open in new window

0
doctorbillTechAuthor Commented:
Thanks Chris - page now working. Can't believe I missed that
0
doctorbillTechAuthor Commented:
Thanks all - really appreciated
0
Chris StanyonWebDevCommented:
No worries ... easily done :)

I would heed David's advice though as a general approach to ALL development work. Whilst your writing your code, turn on error_reporting and var_dump (or print_r) the data ... A LOT !! It really pays to know what's going on !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.