简体   繁体   中英

PHP not inserting data into MySQL

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 ie

 $dID = 0;
 .....
 $deviceSerialID = 0;

I checked if each field is set ie

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:

ie

$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!

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.", , ie 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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM