php data entry

doctorbill
doctorbill used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David S.Consultant & Challenge Subduer
Top Expert 2009

Commented:
What does "print_r($_POST['equip']);" show?

Author

Commented:
Where do I put this string

Author

Commented:
I get the error "Page isn't working"
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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
David S.Consultant & Challenge Subduer
Top Expert 2009

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

Author

Commented:
Thanks Chris - page now working. Can't believe I missed that

Author

Commented:
Thanks all - really appreciated
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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 !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial