KyleL
KyleL

Reputation: 89

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 = "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);  
            //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);
?>

Upvotes: 1

Views: 641

Answers (5)

Sparkup
Sparkup

Reputation: 3754

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().

Upvotes: 0

AjayR
AjayR

Reputation: 4179

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

Upvotes: 0

uadnal
uadnal

Reputation: 11435

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.

Upvotes: 0

Jason
Jason

Reputation: 421

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.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270637

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
}

Upvotes: 1

Related Questions