如何使用GET方法使用php在mysql中存储数据?

时间:2016-02-25 07:53:35

标签: php mysql

我正在尝试使用GET方法将数据从php存储到mysql,

   <?php 
    $user = "root";
    $password = "";
    $host = "localhost";

    $connection = mysql_connect($host,$user,$password);
    $select = mysql_select_db('dam',$connection);

    if($connection)
    {
        echo "connection succesfull";
    }
    else {
        echo "Error";
    }
 ?>

这是数据库连接代码connection.php,它还显示了localhos(浏览器)中的connectionsucccesfull,我试图使用GET方法将一些数据添加到数据库

    <?php 
    include("connect.php");

    $sensor1 = $_GET['sensor1'];
    $sensor2 = $_GET['sensor2'];
    $sensor3 = $_GET['sensor3'];



    $sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('$sensor1,$sensor2,$sensor3')";

    mysql_query($sql_insert);

    if($sql_insert)
    {
        echo "Saving succeed";
    }
    else{
        echo "Error occured";
    }
 ?>

当我输入这样的网址时

  

http://localhost/EPPF/index.php?sensor1=5.0&sensor2=3.0&sensor3=4.0

数据不存储在mysql数据库中

问题是什么?我该如何解决?

5 个答案:

答案 0 :(得分:1)

更改您的查询
$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('$sensor1,$sensor2,$sensor3')";

$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('".$sensor1."','".$sensor2."','".$sensor3."')";

我不知道您为这些字段分配的数据类型,所以我假设它为varchar

答案 1 :(得分:1)

更改您的查询

FROM

$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('$sensor1,$sensor2,$sensor3')";

$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('$sensor1','$sensor2','$sensor3')";

使用带有varibale名称的单引号,因为它是字符串。 如果它是数据库表中的flot或整数列,那么您可以直接使用它(不带单引号)

 $sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ($sensor1,$sensor2,$sensor3)";

答案 2 :(得分:1)

将您的查询更改为

$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('$sensor1','$sensor2','$sensor3')";

答案 3 :(得分:1)

您的查询有错误 查询在

下面
insert into tablearduino (sensor1,sensor2,sensor3) values ('".$sensor1."','".$sensor2."','".$sensor3."')"

答案 4 :(得分:0)

你写错了查询 您的查询是

$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('$sensor1,$sensor2,$sensor3')";

应该在哪里

$sql_insert = "insert into tablearduino (sensor1,sensor2,sensor3) values ('{$sensor1}','{$sensor2}','{$sensor3}')";

请注意引号位置