无法从php脚本插入数据库我正在使用WAMP

时间:2015-06-24 13:58:01

标签: php mysql

我正在尝试从php脚本将数据插入MySQL数据库。我正在使用WAMP。当通过phpmyadmin插入数据时一切正常,数据也可以显示在网页上,但是当尝试手动插入数据时,php脚本没有任何反应(我这样做是为了测试我的目标是将数据加载到服务器中,直到Arduino) 。 BElow是代码:

<?php

    $link=mysqli_connect("localhost", "root", "", "database");

    if (mysqli_connect_errno()) {
     printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }

    $temp="";
    $hum="";
    if(isset($_POST["temp1"])){ $temp = $_POST["temp1"];}
    if(isset($_POST["hum1"])){ $hum = $_POST["hum1"];}

    $safetemp1= mysqli_real_escape_string($link, $temp);
    $safehum1= mysqli_real_escape_string($link, $hum); 

    $query = "INSERT INTO tempLog (temperature, humidity) 
     VALUES (10, 10)";

    $result = mysqli_query($link,$query);
    if (!$result) {
       die("Nothing was inserted, something went wrong.");
    }
    mysqli_close($link);
    header("Location: indexx.php")

1 个答案:

答案 0 :(得分:0)

尝试此操作并查看是否存在错误,也许您拼错了行或表名称。

echo("Error description: " . mysqli_error($link));

$link=mysqli_connect("localhost", "root", "", "database");

if (mysqli_connect_errno()) {
 printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$temp="";
$hum="";
if(isset($_POST["temp1"])){ $temp = $_POST["temp1"];}
if(isset($_POST["hum1"])){ $hum = $_POST["hum1"];}

$safetemp1= mysqli_real_escape_string($link, $temp);
$safehum1= mysqli_real_escape_string($link, $hum); 

$query = "INSERT INTO tempLog (temperature, humidity) 
 VALUES (10, 10)";

$result = mysqli_query($link,$query);
if (!$result) {
   echo("Error description: " . mysqli_error($link));
   die("Nothing was inserted, something went wrong.");
}
mysqli_close($link);
header("Location: indexx.php")