简体   繁体   中英

PHP Insert regex results into database

I'm learning php. I'm writing a php script. This script should get a value of column "street" from table "branch" which is in my database. Values in this column are in format "Krakowska 50A". Then I use preg match to split this value to a street name "Karkowska" and house number "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? Thanks :)

EDIT: Preg match works, the problem is query not working.

EDIT2: Ok, I guess the problem is when the $streetName or $streetNumber is empty and i have to insert 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. I don't know how to insert values to table location for id=1, id=2, id=3,... from table branch.

<?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;

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