[英]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 INTO“表名”-> 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: 现在,以我的html形式,我只想输入某些字段的数据:
<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 更新6/20/14
It seems that trouble lies in my CREATE TABLE syntax. 看来麻烦在于我的CREATE TABLE语法。 When I replace type 'int' or 'float' with 'varchar', for the fields (dID is an exception), it updates the table. 当我将类型'int'或'float'替换为'varchar'时,对于字段(dID是一个例外),它将更新表。 Anyone familiar with posting an 'int' or 'float' value? 熟悉发布“ int”或“ float”值的人吗?
UPDATE 6/20/14 A couple hours later... 更新6/20/14几个小时后...
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. 同样,使用'int'或'float'要求在实现INSERT INTO ... VALUES子句的方式上有所不同。 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: 而是用null填充它:
$insert_query1 = "INSERT INTO Devices (deviceChanged) VALUES (NULL);
PHP now inserts data into MySQL. PHP现在将数据插入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. 您应该删除非varchar拼写中的刻度。 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. example devicePort1被声明为int,但是insert语句发送带有滴答声的值,例如, '".$devicePort1."',
而不是此,因为MySQL期望int您应该发送, ".$devicePort1.",
,即没有滴答声。
Also because ID is AUTO_INCREMENT You should not to try to set value to it. 同样,因为ID为AUTO_INCREMENT,所以您不应尝试为其设置值。 To check if your statement is correct echo $query
, then try to execute that from MySQL console or PHPMyAdmin. 要检查您的语句是否正确,请echo $query
,然后尝试从MySQL控制台或PHPMyAdmin执行该语句。
A good practice is to use prepared statements or PDO. 好的做法是使用准备好的语句或PDO。 check this PHP: PDO - Manual or Prepared Statements Manual . 检查此PHP:PDO-手册或预备语句手册 。 Those links are from Php.net and they contain a very good explanation with examples; 这些链接来自Php.net,其中包含很好的示例解释。
isset
return boolean value (true/false) not integer or something else ( http://www.php.net//manual/en/function.isset.php ) isset
返回布尔值(true / false)不是整数或其他值( http://www.php.net//manual/zh/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 在此处查看有关此内容的更多信息: 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. 另外,请考虑使用某种循环来分配这些变量(如果需要的话),而不要大量复制代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.