I have looked at other posts and tried some of the suggestions but I still can't seem to populate a specific table in my database.
I will supply some blocks of my code to put things into perspective.
Here is my code for creating the table I want to populate with data:
CREATE TABLE `Devices` (
`dID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`deviceType` enum('BT','C1','AW') DEFAULT NULL COMMENT 'BT = Bluetooth, C1 = C1 Reader, AW = Acyclica Wifi',
`deviceName` varchar(255) NOT NULL DEFAULT '',
`deviceIP` varchar(15) NOT NULL DEFAULT '',
`devicePort1` int(4) NOT NULL,
`devicePort2` int(4) DEFAULT NULL,
`deviceRealLatitude` float(10,6) NOT NULL,
`deviceRealLongitude` float(10,6) NOT NULL,
`deviceVirtualLatitude` float(10,6) DEFAULT NULL,
`deviceVirtualLongitude` float(10,6) DEFAULT NULL,
`deviceChanged` tinyint(1) DEFAULT NULL,
`deviceStatus` tinyint(1) DEFAULT NULL,
`deviceLastSeen` timestamp NULL DEFAULT NULL,
`deviceBufferSize` int(4) DEFAULT NULL,
`deviceSoftwareVersion` varchar(20) DEFAULT NULL,
`deviceMacAddress` varchar(17) DEFAULT NULL,
`deviceTest` tinyint(1) DEFAULT NULL,
`deviceAPIKey` varchar(100) DEFAULT NULL COMMENT 'Only used with the Acyclia Scanners',
`deviceSerialID` int(6) DEFAULT NULL COMMENT 'Only used with the Acyclia Scanners',
PRIMARY KEY (`dID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
I have declared all the field variables i.e
$dID = 0;
.....
$deviceSerialID = 0;
I checked if each field is set i.e
isset($_POST['dID']) == 1 ? ($dID = ($_POST['dID'])) : null;
.....
isset($_POST['deviceSerialID']) == 1 ? ($deviceSerialID = ($_POST['deviceSerialID'])) : null;
Here is where I use INSERT INTO "table name" --> VALUES
$insert_query = "INSERT INTO Devices (`dID`, `deviceType`, `DeviceName`, `deviceIP`,
`devicePort1`, `devicePort2`, `deviceRealLatitude`, `deviceRealLongitude`,
`deviceVirtualLatitude`, `deviceVirtualLongitude`, `deviceChanged`, `deviceStatus`,
`deviceLastSeen`, `deviceBufferSize`, `deviceSoftwareVersion`, `deviceMacAddress`,
`deviceTest`, `deviceAPIKey`, `deviceSerialID`) VALUES ('".$dID."', '".$deviceType."',
'".$deviceName."', '".$deviceIP."', '".$devicePort1."', '".$devicePort2."',
'".$deviceRealLatitude."', '".$deviceRealLongitude."', '".$deviceVirtualLatitude."',
'".$deviceVirtualLongitude."', '".$deviceChanged."', '".$deviceStatus."',
'".$deviceLastSeen."', '".$deviceBufferSize."', '".$deviceSoftwareVersion."',
'".$deviceMacAddress."', '".$deviceTest."', '".$deviceAPIKey."',
'".$deviceSerialID."')";
mysqli_query($conn,$insert_query);
Now, in my html form, I only want to input data for some of the fields:
<fieldset>
<form name = "form1" id = "form1" action = "" method = "post">
Device ID: <input type="text" name="dID" > <br>
Device Type: <select name="deviceType">
<option value = "BT">BT</option>
<option value = "C1">C1</option>
<option value = "AW">AW</option>
</select> <br>
Device Name: <input type="text" name="deviceName" > <br>
Device IP: <input type="text" name="deviceIP" > <br>
Device Port 1: <input type="text" name="devicePort1"> <br>
Device Port 2: <input type="text" name="devicePort2"> <br>
<input type = "submit" name = "submit" value = "submit" />
</fieldset>
</form>
I get no errors when I enter my data and hit submit. I realize that some of the things I have done in the code are not necessary such as the tick marks. Its all been a matter of trying to see if it would fix my problem. Any suggestions as to what I can do please?
UPDATE 6/20/14
It seems that trouble lies in my CREATE TABLE syntax. When I replace type 'int' or 'float' with 'varchar', for the fields (dID is an exception), it updates the table. Anyone familiar with posting an 'int' or 'float' value?
UPDATE 6/20/14 A couple hours later...
Guys, it seems I have discovered the problem. It was a small technical issue and I complicated the code to drown in further error.
The idea follows this:
I do not need to worry about a field being set at this point, as many fields will remain null. I only need to fill a few out using this method for each field:
i.e
$deviceType = $_POST['deviceType'];
Also, the using an 'int' or 'float' requires a difference in how to implement the INSERT INTO ... VALUES clause. If leaving it null (which I am for now), I cannot do this:
$insert_query1 = "INSERT INTO Devices (deviceChanged) VALUES ('$deviceChanged');
This iteration was preventing the table from being filled with its appropriate values.
Instead, fill it with null:
$insert_query1 = "INSERT INTO Devices (deviceChanged) VALUES (NULL);
PHP now inserts data into MySQL. Thanks for the help!
error_reporting(E_ALL); ini_set('display_errors', 1);
stands to yield more. – Fred -ii- Jun 19 '14 at 21:58