0

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>
4
  • 2
    add or die(mysql_error()); to the end of your insert line and see if it raises an error. Commented Dec 11, 2012 at 11:58
  • 2
    mysql_* functions are deprecated, use mysqli_* or PDO instead. Commented Dec 11, 2012 at 11:59
  • check the query in the mysql log, if you try to run the query from the mysql console it will probably fail Commented Dec 11, 2012 at 12:02
  • 1
    Remember that you shoul always escape values before you use them in queries. php.net/manual/en/function.mysql-real-escape-string.php Commented Dec 11, 2012 at 12:08

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@SaraSayyar If you found the answer useful then mark the answer as correct so other users can refer the answer. :)
1

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')");

Comments

1

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.'")");

1 Comment

thanks for your help, i forgot to write "values" word, like this mysql_query("INSERT INTO availblesensors VALUES('$sid','$Desc')")
0

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

Comments

0

Query should be

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

not

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.