3

I am taking two comma separated values from the database from different coloumn(supplierrouteallocation:1000,1200 and routesymbol:10xx,12xx) and i want to insert this value into the another table into the different column for example like this

route    symbol

1000     10xx

1100     11xx

but i tried its not working can anyone helpme .thanks

my code

$routes = explode(",",$supplierrouteallocation);
$symbol = explode(",",$routesymbol);

$count=count($routes,$symbol);
for($i=0;$i<$count;$i++)
{

$sql_insertroute="INSERT INTO `fms`.`routestable` (
    `id` ,  

`route` 
`routesymbol`       
)
VALUES (NULL ,'$routes[$i]','$symbol[$i]')
";

mysql_query($sql_insertroute);

}
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Xavi
  • 2,552
  • 7
  • 40
  • 59
  • 1
    [Please, stop using mysql_* functions](http://stackoverflow.com/q/12859942/1238019) in new code, they are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Instead of, have a look on [prepared statements](http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html), and use [Mysqli](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php). – zessx Nov 18 '13 at 08:56
  • Missing comma after `route`. But you should know that, there was probably an error message saying so, that you forgot to share with us. – András Szepesházi Nov 18 '13 at 09:03

2 Answers2

2

You've got various mistakes :

  • count() only accepts a single array
  • you're missing a coma in your request : ('id', 'route'[,] 'routesymbol')

Here's a piece of code which should work for you :

// get data
$routes = explode(",",$supplierrouteallocation);
$symbols = explode(",",$routesymbol);

// prepare the request
$sql_insertroute = "INSERT INTO `fms`.`routestable` (`id`, `route`, `routesymbol`) VALUES ";

// create an insert line per data couple
$vals = array();
$n = max(count($routes), count($symbols));
for($i=0;$i<$n;$i++) {
    $route = isset($routes[$i]) ? "'".$routes[$i]."'" : "NULL";
    $symbol = isset($symbols[$i]) ? "'".$symbols[$i]."'" : "NULL";
    array_push($vals, "(NULL, $route, $symbol)");
}

// join those lines with comas, and add'em to the request
$sql_insertroute .= implode(', ', $vals);
zessx
  • 68,042
  • 28
  • 135
  • 158
  • thank you for your reply i tried your piece of code it showing syntax error near array_push($vals, "(NULL, $route, $symbol)"; – Xavi Nov 18 '13 at 09:13
  • Sorry, I missed a closing parenthesis : `array_push($vals, "(NULL, $route, $symbol)");` – zessx Nov 18 '13 at 09:26
  • Are you running the query ? (If you look, I didn't put the initial `mysql_query($sql_insertroute);` in my code) – zessx Nov 18 '13 at 10:09
0

Try this

$sql_insertroute="INSERT INTO `fms`.`routestable` (`id`,`route`,`routesymbol`) VALUES (NULL,'".$routes[$i]."','".$symbol[$i]."')";
Shafeeque
  • 2,039
  • 2
  • 13
  • 28