Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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!

share|improve this question
2  
You should use a prepared statement with placeholders and bind your input values to these placeholders. That will take care of sql injection and you don't have to escape your values too. – VMai Jun 19 '14 at 21:53
    
Any errors, if checking for them? – Fred -ii- Jun 19 '14 at 21:56
    
Can you present an example of this prepared statement please? – KS7X Jun 19 '14 at 21:56
    
I tried using mysqli_error($link) but nothing came up – KS7X Jun 19 '14 at 21:57
    
That's ok, but error_reporting(E_ALL); ini_set('display_errors', 1); stands to yield more. – Fred -ii- Jun 19 '14 at 21:58
up vote 0 down vote accepted

isset return boolean value (true/false) not integer or something else (http://www.php.net//manual/en/function.isset.php)

And another mistake is about how you use ternary comparision. See more about that here: http://davidwalsh.name/php-shorthand-if-else-ternary-operators

So instead of:

isset($_POST['deviceName']) == 1 ? ($deviceName = ($_POST['deviceName'])) : null;

you should write

$deviceName = isset($_POST['deviceName']) ? $_POST['deviceName'] : null;

Also please consider using some sort of loop to assign these variables (if they are at all needed) and not duplicate your code in huge amounts.

share|improve this answer
    
Ok thank you for that. I originally set isset($_POST['submit']) but that was not working at the time. Here's what var_dump returned: string(436) "INSERT INTO Devices (deviceType, DeviceName, deviceIP, devicePort1, devicePort2, deviceRealLatitude, deviceRealLongitude, deviceVirtualLatitude, deviceVirtualLongitude, deviceChanged, deviceStatus, deviceLastSeen, deviceBufferSize, deviceSoftwareVersion, deviceMacAddress, deviceTest, deviceAPIKey, deviceSerialID) VALUES ('BT', random, 45.45.45, 1200, 1300, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)" – KS7X Jun 19 '14 at 22:34
    
see my edit above - I found your problem I guess :) You never assign null to $deviceName. – DarkSide Jun 19 '14 at 22:36
    
see value random in your INSERT, what's that? – DarkSide Jun 19 '14 at 22:36
    
Thats what I inputted for the device name field. I am not sure what you referring to as your edit – KS7X Jun 19 '14 at 22:38
    
Sorry, edit just made. random should have ' around it. Also field name DeviceName is defined with first lowercase letter in table, so if you mix upper/lower case letters in your mysql fieldnames, then be consistent. – DarkSide Jun 19 '14 at 22:40

You are trying to insert a string in a place of a float.

You should remove the ticks from the non varchar columents. example devicePort1 is declared and int but the insert statement is sending a value with a tick like this , '".$devicePort1."', instead of this, because MySQL expects an int u should send the , ".$devicePort1.", , i.e with out the ticks.

Also because ID is AUTO_INCREMENT You should not to try to set value to it. To check if your statement is correct echo $query , then try to execute that from MySQL console or PHPMyAdmin.

A good practice is to use prepared statements or PDO. check this PHP: PDO - Manual or Prepared Statements Manual. Those links are from Php.net and they contain a very good explanation with examples;

share|improve this answer
    
MySQL does convert '123' to integer 123 automatically if needed, so applying '123' as value to integer column should work too. – DarkSide Jun 19 '14 at 22:20
    
Amanuel, you pushed me a little further. Although, when I check the database now (after inputting values on form), the field values are all 0 except for dID since that is AI. – KS7X Jun 19 '14 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.