COM port handling via PHP :- Device connected with my computer on COM port 6… I am able to read data coming on port through attached device in php but now i wanna know how can i save my COM port data in mysql… output is like 18008975A84C AT 16:21:01 ON 22:03:16. it includes id card no , time and date
Share
thank you all
its always goes to else part record not inserted and value not inserting in database…
Here is my config.php….
<?php
$username = 'root';
$password = 'root';
$hostname = '';
$db = 'demo';
//connection to the database
$dbhandle = mysql_connect($hostname, $username)
or die ("Could not connect to server … \n" . mysql_error ());
//echo "Connected to MySQL”;
mysql_select_db($db)
or die (”Could not connect to database … \n” . mysql_error ());
NOw here is my php page with insert query …
<?php
$device = "COM6";
exec("mode $device BAUD=9600 PARITY=n DATA=8 STOP=1 ");
$comport = fopen($device, "r+w");
if ($comport === false){
die("Failed opening com port”);
}else{
//echo ”connected”;
while (!feof($comport)) {
sleep(3);
// echo fgets($comport);
$buffer = fgets($comport);
echo ”Read: $buffer”;
sleep(3);
}
}
include(’config.php’);
if(isset($_POST[’buffer’])){
$buffer = $_POST[’buffer’];
$sql=(”INSERT INTO read (id,reading) VALUES (’’,’”.$buffer.”’)”)or die(mysql_error());
if (!mysql_query($sql,$dbhandle))
{
die(’Error: ’ . mysql_error());
}
echo ”1 record added”;
}
else{
echo”not inserted”;
}
fclose($comport);
?>
Most of the code was correct. In your code you have missed the mysql_query function which is used to execute the mySQL queries. I have updated the code and pasting it below.
<?php
$device = ”COM6”;
exec(”mode $device BAUD=9600 PARITY=n DATA=8 STOP=1 ”);
$comport = fopen($device, ”r+w”);
if ($comport === false){
die(”Failed opening com port”);
}else{
//echo ”connected”;
while (!feof($comport)) {
sleep(3);
// echo fgets($comport);
$buffer = fgets($comport);
echo ”Read: ”.$buffer;
sleep(3);
}
}
include(’config_port.php’);
if(isset($_POST[’buffer’])){
$buffer = $_POST[’buffer’];
$sql=mysql_query(”INSERT INTO read (id,reading) VALUES (’’,’”.$buffer.”’)”);
if($sql) echo ”Data Inserted!” else die(mysql_error());
} else {
echo ’Please select email address’;
}
fclose($comport);
?>
thanks for your quick response…. here is my code…
<?php
$device = "COM6";
exec("mode $device BAUD=9600 PARITY=n DATA=8 STOP=1 ");
$comport = fopen($device, "r+w");
if ($comport === false){
die("Failed opening com port”);
}else{
//echo ”connected”;
while (!feof($comport)) {
sleep(3);
// echo fgets($comport);
$buffer = fgets($comport);
echo ”Read: $buffer”;
sleep(3);
}
}
include(’config_port.php’);
if(isset($_POST[’buffer’]))
{
$sql=”INSERT INTO read (id,reading)
VALUES
(’’,’$_POST[buffer]’)”
or die(mysql_error());
}
else
{
echo ’Please select email address’;
}
fclose($comport);
?>
please do correct …
Firstly you have to assign the values in the variable. Like,
$idCardNumber = ’18008975A84C’; //you have to use the $_GET or $_REQUEST to get the dynamich value.
$dateOne = ’16:21:01′;
$dateTwo = ’22:03:16′;
Then you have to pass these values in the insert query. Like:
mysql_query(INSERT(’idCardNumber’,’dateOne’, ’datetwo’) VALUES($idCardNumber, $dateOne, $dateTwo));