在我的mysql数据库中向表添加记录的正确方法是什么?

时间:2018-02-21 23:35:30

标签: php mysql mysqli

我有两张桌子:

CREATE TABLE `czujniki` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(20) COLLATE utf8_polish_ci NOT NULL,
  `name` varchar(20) COLLATE utf8_polish_ci NOT NULL,
  `address` varchar(20) COLLATE utf8_polish_ci NOT NULL,
  `desc` varchar(255) COLLATE utf8_polish_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;


CREATE TABLE `temperature` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_czujnik` int(11) NOT NULL,
  `time` int(11) NOT NULL,
  `temp` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;

还有3个值addresstimetemp将此值添加到表中的最佳方法是什么?
我已经用这种方式在PHP中做到了:

$z = mysqli_query($con, "SELECT `id` 
                        FROM `czujniki` 
                        WHERE `address` = {$address}");
  $row = mysqli_fetch_all($z,MYSQLI_NUM);
  $id = $row[0][0]
  $z = mysqli_query($con, "INSERT INTO `temperature` 
                                    (`id_czujnik`, `time`, `temp`) 
                            VALUES ({$id}, {$time}, {$temp})");

有更好的方法吗?

0 个答案:

没有答案