简体   繁体   中英

Inserting data into MySQL database (no data)

I want to save values in the database by using php, I got no error, but the database is empty it is not saved the data.

I put if statements to check if the value saved before

please check my code

please help me

<?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>

The query will be

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

not

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

You missed VALUES keyword in query.

your sql sentence is wrong..

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

you're saying that $id and $Desc are columns in availablesensors, you should add VALUES before the actual values

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

There is a little mistake in code. Change by below code.

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

Replaced by

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

Change the lines to this:

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>";
}    

this way you can see the error it gives you if any

Query should be

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

not

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

Here are the required changes

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM