hi,

I am doing a insert query over here and if the query is executed successfully, there will be a success message. However, whenever the query is being executed successfully, the success message is not being displayed.

here is a snipet, can anybody guide me along? thanks alot.

//Execute SQL Statement and store results as a recordset
  $result= mysql_query($sql) or die (mysql_error());

if ( $result != ""  )
  { 
	echo "Contact successfully added.";
  }
  else
   {
echo "Please try again."; 
   }

Dani AI

Generated

Good points from and . For 's symptom (a "success" message showing on first page load) the root is usually not the return value test itself but when the insert code runs and what value is being tested. Common real checks that help pinpoint the problem: confirm the insert block only runs after a POST/submit check, log the full SQL and the raw return value (var_dump or error_log) so the script output can be inspected, and ensure no previous assignment or caching keeps a truthy value in $result.

Quick troubleshooting checklist:

  • Guard the insert with a POST or submit-button check so it never runs on an initial GET.
  • Temporarily log the SQL string and the raw result to the error log to see exactly what the script returned.
  • Use Post/Redirect/Get after a successful insert to avoid stale success messages on refresh.
  • Move off the old mysql extension (removed in modern PHP) and use mysqli or PDO with prepared statements for clearer errors and safer inserts. See the mysqli documentation.

Example pattern with mysqli (prepared stmt + affected-rows check):

$mysqli = new mysqli('host','user','pass','db');
$stmt = $mysqli->prepare("INSERT INTO contacts (name,email) VALUES (?, ?)");
$stmt->bind_param('ss', $name, $email);
if ($stmt->execute() && $stmt->affected_rows > 0) {
    echo "Contact successfully added.";
} else {
    error_log("Insert failed: " . $stmt->error);
}
$stmt->close();
$mysqli->close();

Avoid printing raw DB errors in production; use logging for debugging and prepared statements to prevent injection.

Recommended Answers

All 4 Replies

Hi!

When executing a query which alters the table (UPDATE, INSERT, etc...), mysql_query returns TRUE on success and FALSE on error.

So your if-clause should read

if ($result == TRUE) {

or simply

if ($result) {

Hope that helps!

Happy coding! :)

Hi!

When executing a query which alters the table (UPDATE, INSERT, etc...), mysql_query returns TRUE on success and FALSE on error.

So your if-clause should read

if ($result == TRUE) {

or simply

if ($result) {

Hope that helps!

Happy coding! :)

hey,

I've tried your method, but it is still not working and the success message is being displayed whenever it loads for the first time. Any idea why is this happening?

Regards,
heels

This should be your code.

$result = mysql_query($sql);

if ( $result )
{ 
  echo "Contact successfully added.";
}
else
{
  echo mysql_error(); 
}

If this does not work, their may be something wrong in your sql statement.

This should be your code.

$result = mysql_query($sql);

if ( $result )
{ 
  echo "Contact successfully added.";
}
else
{
  echo mysql_error(); 
}

If this does not work, their may be something wrong in your sql statement.

ah, thanks alot. :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.