Click here to Skip to main content
15,997,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai to all



here is my table data..

(4 Columns/1row in mysql table)

id                 bno                            date                      results
310                001	 			  02-15-2020               MI,108.53
									   Oh,194.57
									   PA,182.22
									   WA,238.57

How to split(statemiles) single column into (state, Miles) two columns and output like following 5 columns /4rows in mysql using php code.

(5 Columns in mysql table)
------------------------------------------------------------------------------------

id                  bno                             date                  result1     result2

310                001	 			  02-15-2020               MI          108.53

310                001	 			  02-15-2020		   Oh          194.57

310                001	 			  02-15-2020		   PA          182.22

310                001	 			  02-15-2020		   WA          238.57


What I have tried:

Following php code split textarea multi values split into columns successfully.

<?php
//invoice.php  
include('../includes/config.php');

if(isset($_POST["add"]))
{
$order_no=$_POST['order_no'];
$comma = preg_replace('/,\\s*/', "', '", $_POST["stateandmileslist"]);
$row = preg_replace('/\\n/', "'), \n('", $comma);
$query = "insert into tis_satesmiles (states, miles) values ('" . $row . "')";
$statement = $dbh->prepare($query);
$statement->execute();
}
?>
Posted
Updated 9-Feb-20 21:05pm

1 solution

The simplest method is: change your DB. Don't store multiple values in a single column, no matter how much easier it may be from your presentation software to dump it in there. Store it in separate columns, and it's trivial to "join them together" if you need that. (Heck, you can generate a calculated column that does it automatically when needed!)
But it's also trivial deal with the data as separate objects that are guaranteed correct (the Miles columns would be DECIMAL(7,2) so it only stores valid numbers), and splitting it in your presentation language is simple, as is verifying the user input as a valid state code, and a valid distance.

SQL string handling is primitive at best, and it's a whole load easier to do the job properly than to mess about with breaking columns up when you want to process the data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900