开发者

How to find out if the following SQL statement is working

I'm fairly new to mysql and I was wondering if the following code should be working. I've been checking with my database after submission of this form and nothing is getting inputted into the database. Please let me know what I'm doing wrong, Thank you!

 <?php
$username = $_SESSION['username'];
$email = $_POST['email'];
$desc = $_POST['desc'];
$url = $_POST['url'];
$priority = $_POST['priority'];


        if( strlen($username) > 0 && strlen($email) > 0 && strlen($desc) > 0)
        {
            $sql = "INSER开发者_JAVA技巧T INTO feature_request_table (username, desc, email, url, priority, status)
            VALUES( '$username' , '$desc' , '$email' , '$url' , '$priority' , '0' )";
            echo "The sql statement is: " . $sql . "</br>";
            mysql_query($sql);  
            //echo "The result is: " . $results . "</br>";  
            echo "Your request have been sent. Please allow a brief period of time for your webmaster implement your request. Thank you!";
        }


mysql_close($con);
?>


Use the return value of mysql_query() and check if it is NULL to find out if the query was successful:

$result = mysql_query($sql);

// A NULL value of $result indicates failure
if (!$result) {
  // something went wrong!

  // See the error...
  echo mysql_error();
}

Also, we don't see in the posted code that mysql_connect() was called. Also check that the connection was successfully made:

$conn = mysql_connect(all the connection details...);
if (!$conn) {
  // connection failed
}


One thing I haven't seen mentioned yet is that you may not have auto-commit turned on, in which case your insert will return TRUE, but you will not see any change in the DB until you commit the insert.


You should really use the isset() function instead of strlen. Also, it does look like your never actually connecting to a mysql server according to the given code. Try posting the sql statement that is echoed into phpMyAdmin directly.


Following are the changes you need to do.

  • Use the column name desc with some other name of enclose in 'desc'.

  • Always print the error message or even terminate the output.

  • Check whether the POST Items are received correctly or not.

Here is the modified code.

if( isset($username) && isset($email)  && isset($desc) )
        {
            $sql = "INSERT INTO feature_request_table (username, 'desc', email, url, priority, status)
            VALUES( '$username' , '$desc' , '$email' , '$url' , '$priority' , '0' )";
            echo "The sql statement is: " . $sql . "</br>";
            mysql_query($sql) or die (mysql_error());  
            //echo "The result is: " . $results . "</br>";  
            echo "Your request have been sent. Please allow a brief period of time for your webmaster implement your request. Thank you!";
        }


This sould do it :

$data = mysql_query($sql);

if($data === false) { // TODO: better error handling

// Do something...

  echo mysql_error();

// Or see the errors...

}

Note : if you want to have valid HTML pages in all situations handle your errors don't use OR DIE().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜