将数据插入MySQL数据库(无数据)

时间:2012-12-11 11:56:33

标签: php mysql

我想使用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>

6 个答案:

答案 0 :(得分:1)

查询将是

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

不是

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

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

答案 1 :(得分:1)

你的sql句子错了..

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

你说$ id和$ Desc是availablesensors中的列,你应该在实际值之前添加VALUES

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

答案 2 :(得分:1)

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

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

替换为

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

答案 3 :(得分: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>";
}    

这样你可以看到它给你的错误

答案 4 :(得分:0)

查询应该是

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

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

答案 5 :(得分: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);