0

我想使用php将值保存在数据库中,我没有错误,但是数据库是空的,它没有保存数据。

我放了 if 语句来检查之前保存的值是否

请检查我的代码

请帮我

<?php
$submit = $_POST['submit'];
$sid  = strip_tags($_POST['sid']);
$Desc = strip_tags($_POST['Desc']);    
if ($submit) {
    // open data base
    $connet = mysql_connect("localhost", "root", "hahaha1");
    mysql_select_db("senior");        
    $sensorcheck = mysql_query("SELECT sid FROM availblesensors WHERE sid='$sid'");
    $count       = mysql_num_rows($sensorcheck);
    if ($count != 0) {
        die("You add this Sensor ID before!");
    }        
    if ($sid && $Desc) {
        mysql_query("INSERT INTO availblesensors ('$sid','$Desc')");
        // success 
        die("You have add new sensor Successfully !! <a href='admin.php'>click here to return to admin page</a> or Here to
 <a href='gotoaddsensor.php'>add new sensor</a>");
    } else {
        echo "<h3><font color='red'>Please fill in <b>all</b> feilds!</font></h3>";
    }        
}
?>




<form action="gotoaddsensor.php" method="post" class="one">

<div class="info">

    <center><div>Please add availble sensors with a few description of it if you need</div></center><br/>
</div>


                <div><b><label for="sid">Sensor ID</label>:</b>
                <input name="sid" value="" id="sid" type="text" size="15"/> </div>
    <br />
                <div><b><label for="password">Description</label>:</b>
                <input name="Desc" value="" id="Desc" type="text" size="40"/></div>
    <br />

            <div id="login-button">
            <input  name="submit"  type="submit" value="submit"  />
                            <input type="reset" value="clear" />
 </div>

    </form>
4

6 回答 6

1

代码有一点小错误。通过以下代码更改。

mysql_query("INSERT INTO availblesensors ('$sid','$Desc')");

取而代之

 mysql_query("INSERT INTO availblesensors values ("'.$sid.'","'.$Desc.'")");
于 2012-12-11T12:27:11.487 回答
1

查询将是

mysql_query("INSERT INTO availblesensors VALUES ('$sid','$Desc')");

不是

mysql_query("INSERT INTO availblesensors ('$sid','$Desc')");`

您在查询中错过了VALUES关键字。

于 2012-12-11T12:04:58.400 回答
1

你的sql语句是错误的..

mysql_query("INSERT INTO availblesensors ('$sid','$Desc')");

您是说 $id 和 $Desc 是可用传感器中的列,您应该在实际值之前添加 VALUES

mysql_query("INSERT INTO availblesensors VALUES ('$sid','$Desc')");
于 2012-12-11T12:06:36.557 回答
0

这是所需的更改

$connet = mysql_connect("localhost", "root", "hahaha1");
mysql_select_db("senior", $connet);        
$sensorcheck = mysql_query("SELECT sid FROM availblesensors 
                                       WHERE sid='$sid'",$connet);
$count = mysql_num_rows($sensorcheck);
if ($count != 0) {
    die("You add this Sensor ID before!");
}        
if ($sid && $Desc) {
    mysql_query("INSERT INTO availblesensors ('$sid','$Desc') 
                                       VALUES('Sensor Id','Sensor Desc')", $connet);
于 2012-12-11T12:55:28.867 回答
0

将这些行更改为:

if ($sid && $Desc) {
    if(!mysql_query("INSERT INTO availblesensors values ('$sid','$Desc')")) {
        die(mysql_error());
    }  
} else {
    echo "<h3><font color='red'>Please fill in <b>all</b> feilds!</font></h3>";
}    

这样你就可以看到它给你的错误(如果有的话)

于 2012-12-11T12:03:35.933 回答
0

查询应该是

mysql_query("INSERT INTO availblesensors VALUES (' ".$sid." ',' ".$Desc." ')");

不是

mysql_query("INSERT INTO availblesensors ('$sid','$Desc')");
于 2012-12-11T12:07:09.677 回答