Jump to content

Error in displaying message for inserting new records


helloanddie

Recommended Posts

hi,

 

I am doing an insert query 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 !== true )

{

echo "Contact successfully added.";

}

else

{

echo "Please try again.";

}

 

Your if is wrong. You are checking if the result is not true, instead of checking if it is true. Should be:

 

<?php

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

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

?>

 

 

Orio.

sure, thanks alot. :). so sorry to trouble you. Mind telling me where is my mistake?

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>AddContact</title>

<style type="text/css">

<!--

.style1 {

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: small;

}

.style2 {

font-family: Verdana, Arial, Helvetica, sans-serif;

font-size: smaller;

}

-->

</style></head>

 

<body>

 

<?php

if (isset($_POST['AddContact']))

{

 

//connecting to database

include ("config.php");

include ("opendb.php");

 

{

 

//get values from form

$firstName = mysql_real_escape_string($_POST['firstName']);

$lastName = mysql_real_escape_string($_POST['lastName']);

$companyName = mysql_real_escape_string($_POST['companyName']);

$companyAddr = mysql_real_escape_string($_POST['companyAddr']);

 

//insert data into MYSQL

$sql= "INSERT INTO contacts (firstName,lastName,companyName,companyAddr)

VALUES ('".$firstName."', '".$lastName."', '".$companyName."', '".$companyAddr."')";

 

$result= mysql_query($sql) or die (mysql_error());

  ?>

 

<table width="537" height="242" border="1" cellpadding="1" cellspacing="1">

<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">

  <tr>

    <td colspan="2"><span class="style1">Add Contact </span></td>

  </tr>

  <tr>

    <td colspan="2"> </td>

  </tr>

  <tr>

    <td width="140"><span class="style2">First Name : </span></td>

    <td width="384">

      <input name="firstName" type="text" id="firstName" value="<?=$fields['firstName']?>" size="50" />        </td>

  </tr>

  <tr>

    <td><span class="style2">Last Name : </span></td>

    <td>

      <input name="lastName" type="text" id="lastName" value="<?=$fields['lastName']?>" size="50" />    </td>

  </tr>

  <tr>

    <td><span class="style2">Company Name : </span></td>

    <td>

      <input name="companyName" type="text" id="companyName" value="<?=$fields['companyName']?>" size="50" />    </td>

  </tr>

  <tr>

    <td><span class="style2">Company Address : </span></td>

    <td>

      <textarea name="companyAddr" cols="47" id="companyAddr" value="<?=$fields['companyAddr']?>"></textarea>    </td>

  </tr>

 

<td><input name="AddContact" type="submit" id="AddContact" value="AddContact">

   

  </form>

      </td>

  </tr>

</table>

<? if ($result === true)

  {

  echo "Contact information saved successfully.";

  }

  else

  {

    echo "Please try again.";

  }

  //close connection

  mysql_close($conn); 

  }

}

?>

</body>

</html>

 

 

 

I see nothing wrong with the code. (Except the fact you have a block opened after the includes with no if/loop).

 

Let me get this straight again. Your problem is that you are getting a fail/success message on the very first page load? (Even when the form is yet to be submitted)

 

Orio.

hi,

 

I am doing an insert query 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 !== true )

{

echo "Contact successfully added.";

}

else

{

echo "Please try again.";

}

 

 

try

 

$result= mysql_query($sql) or die (mysql_error());

 

if ( $result ) {

echo "Contact successfully added.";

} else {

echo "Please try again.";

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.