简体   繁体   English

PHP将正则表达式结果插入数据库

[英]PHP Insert regex results into database

I'm learning php. 我正在学习php。 I'm writing a php script. 我正在写一个php脚本。 This script should get a value of column "street" from table "branch" which is in my database. 该脚本应该从数据库中的表“ branch”中获取“ street”列的值。 Values in this column are in format "Krakowska 50A". 此列中的值格式为“ Krakowska 50A”。 Then I use preg match to split this value to a street name "Karkowska" and house number "50A". 然后,我使用预匹配将这个值拆分为街道名称“ Karkowska”和门牌号“ 50A”。 Results of pregmatch, I want to insert into table "location" in columns "street" and "house_number" Can someone explain to me what is wrong with my code and why this not working? pregmatch的结果,我想在“ street”和“ house_number”列的表“ location”中插入表。有人可以向我解释我的代码有什么问题,为什么这不起作用? Thanks :) 谢谢 :)

EDIT: Preg match works, the problem is query not working. 编辑:Preg匹配有效,问题是查询不起作用。

EDIT2: Ok, I guess the problem is when the $streetName or $streetNumber is empty and i have to insert NULL. EDIT2:好的,我想问题是当$ streetName或$ streetNumber为空时,我必须插入NULL。 Is that right? 那正确吗? How to change query to do that? 如何更改查询来做到这一点?

EDIT3: Update code, now it works, but only insert the first value of preg match results. EDIT3:更新代码,现在可以使用,但是只能插入preg匹配结果的第一个值。 I don't know how to insert values to table location for id=1, id=2, id=3,... from table branch. 我不知道如何从表分支中为id = 1,id = 2,id = 3,...向表位置插入值。

<?php

$db = mysqli_connect('localhost','root','','jakoscobslugi')
or die('Error connecting to MySQL server.');

$query0 = "TRUNCATE location";
mysqli_query($db,$query0) or die('Error query0.');

$result = mysqli_query($db, "SELECT street FROM branch;");

while($row = mysqli_fetch_assoc($result))
{
    preg_match('/\s?([^\d]+\s?)\s?(\d+.*)*/', $row['street'], $street);

    //$streetName = $street[1];

    //$streetNumber = $street[2];     

    echo "[". $street[1] ."] [". $street[2] ."]";
    echo "<br />";  

    $query2 = "INSERT INTO location(id, street, house_number, phone_number)
    SELECT id, '$street[1]', '$street[2]', phone
    FROM branch
    ON DUPLICATE KEY UPDATE id=branch.id, phone_number=branch.phone";
    mysqli_query($db, $query2) or die('Error query2: ' . mysqli_error($db));
}
?>

This is my table location which I didn't write and I can't change it: 这是我未写的表位置,无法更改:

CREATE TABLE `location` (
  `id` int(11) NOT NULL,
  `company_data_id` int(11) DEFAULT NULL,
  `street` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `house_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `apartment_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `postal_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `phone_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `location`
  ADD PRIMARY KEY (`id`),
  ADD KEY `IDX_5E9E89CB17A7BBEB` (`company_data_id`);

There is no primary key, your table definition is wrong. 没有主键,您的表定义错误。

Should be: 应该:

  CREATE TABLE `location` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `company_data_id` int(11) DEFAULT NULL,
  `street` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `house_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `apartment_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `postal_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `phone_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

In your case: 在您的情况下:

ALTER TABLE location CHANGE id id int(11) AUTO_INCREMENT PRIMARY KEY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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