Jump to content

Recommended Posts

Hi Sir,

            below script...i wrote for updating row into trackroute table from data gpstext file and planroute table. Here gpstext file will change every 5seconds and the row has to insert in trackroute table for each 5 seconds.

 

<?php

 

// requesting data base details

 

require("routedb.php");

 

// Gets data from text file

 

$myFile = "gpsfile.txt";

$fh = fopen($myFile, 'r');

$gpsdata = fread($fh, filesize($myFile));//u"gps data,pos_lat,pos_long,time,speed, v1

fclose($fh);

$data=explode(",",$gpsdata);

$time=$data[3];//time stamp

$geo_points=$data[1] . "," . $data[2];//latitude/longitude values

$vehicle_id=$data[5];

$speed=$data[4];

$date=date("d/m/y");

$days=date("D");

//Opens a connection to mysql server

 

$con=mysql_connect($servername,$username,$password);

 

if(!$con)

{

die("server is not connected " . mysql_error());

}

 

// Set mysql database

 

$select_db=mysql_select_db($database,$con);

 

if(!$select_db)

{

die("$database is not connected:" . mysql_error());

}

 

//$qry="SELECT plan_id,days,starttime,endtime FROM planroute where vehicle_id=$vehicle_id";

$qry = "SELECT GROUP_CONCAT(plan_id, ',', days, ',', starttime, ',',endtime separator ';') AS plandata FROM planroute WHERE vehicle_id='$vehicle_id';";

$rslt = mysql_query($qry);

if (!$rslt)

{

die('Invalid query: ' . mysql_error());

}

 

//Iterate through the MySQL results

$row = @mysql_fetch_assoc($rslt);

//$array=$row['plandata'];

//echo $array;

$pd=explode(";",$row['plandata']);

//echo count($pd);

 

for($i=0;$i<count($pd);$i++)

{

$d=$pd[$i];

$routedata=explode(",",$d);

$daydata=explode(",",$routedata[1]);

 

for($j=0;$j<count($daydata);$j++)

{

if($days==$daydata[$j])

{

$st=explode(":",$routedata[2]);

$et=explode(":",$routedata[3]);

$gpstime=explode(":",$time);

if(($st[0]<=$gpstime[0])&&($gpstime<=$et[0]))

{

if(($st[1]<=$gpstime[1])&&($gpstime<=$et[1]))

{

if(($st[2]<=$gpstime[2])&&($gpstime<=$et[2]))

{

$plan_id=$route_data[0];

//Insert new row to trackroute with gps data

 

$query = sprintf("INSERT INTO trackroute " .

"(plan_id, vehicle_id,speed,geo_points, days,date,time) " .

" VALUES ('%s', '%s', '%s','%s','%s','%s','%s');",

mysql_real_escape_string($plan_id),

mysql_real_escape_string($vehicle_id),

mysql_real_escape_string($speed),

mysql_real_escape_string($geo_points),

mysql_real_escape_string($days),

mysql_real_escape_string($date),

mysql_real_escape_string($time));

 

$result = mysql_query($query);

 

if (!$result) {

die('Invalid query: ' . mysql_error());

}

 

 

}

 

}

}

 

}

}

 

}

 

?>

 

Hi, can any one please explain why my code is not inserting data into table.

 

 

Thanks in advance,

 

with sincerely,

sravan

 

Link to comment
https://forums.phpfreaks.com/topic/152775-insert-row-in-mysql-db-problem/
Share on other sites

redo your query like this:

 

$query = sprintf("INSERT INTO trackroute (plan_id, vehicle_id,speed,geo_points, days,date,time) VALUES ('%s','%s','%s','%s','%s','%s','%s')", mysql_real_escape_string($plan_id), mysql_real_escape_string($vehicle_id), 
mysql_real_escape_string($speed), 
mysql_real_escape_string($geo_points), 
mysql_real_escape_string($days), 
mysql_real_escape_string($date), 
mysql_real_escape_string($time));

 

depending on your values, check here and ensure your type specifier is matching your argument(s) appropriately when using sprintf .. http://ca3.php.net/manual/en/function.sprintf.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.