Error in displaying message for inserting new records

hi there. i have just started learning php codes and information can be inserted into mysql successfully however there are errors in displaying successful messages upon inserting new contacts into mysql. I have tried all ways to solve this errors by not successful. So, i am wondering if any of you can help me with this error. Many many thanks in advance :smiley:

Here are the codes.

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

if ( $result != True )
{
echo ‘the data could not be inserted’;
}
else
{
echo “<p> the data has been inserted successfully</p>.”;
}
//close connection
mysql_close($conn);

[quote=ahmednuaman;2339921]Actually, best thing to do is included this where $result != true:


mail("me@me.com","mysql error",mysql_error());

Then you’ll get an email if something went wrong.[/quote]

hi there.i have tried it but it does not work at all. sorry for the trouble caused (:

the successful message is displayed even before the contacts can be inserted into mysql successfully. once again. sorry and thanks for your help (:

[quote=ahmednuaman;2339943]Ok let’s examine your code:


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

if ( $result != True )
{
echo 'the data could not be inserted';
}
else
{
echo "<p> the data has been inserted successfully</p>.";
}
//close connection
mysql_close($conn);

In regards to the var ‘$result’, if the script is running and no query is set up, then this will return a false. That’s why you see the message. What you need to do, depending on how the data is being received, is enclose it within a POST or GET condition:


if ($_POST) {
... // your code goes here
}

[/quote]

hi there. i have tried changing the var $result to true but it still does not work. what do you mean when i try enclosing the received data within a POST or GET condition because i have write that statement on the top when i trigger the submit button on the first time before writing my validation. Many many thanks (:

i shall paste you the codes of my validation.

<?php

$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values
$success_message = “”;

if (isset($_POST[‘AddContact’]))
{
// import the validation library
require(“validation.php”);

$rules = array(); // stores the validation rules

// standard form fields
$rules = “required,firstName,Please enter first name.”;
$rules = “required,lastName,Please enter last name.”;
$errors = validateFields($_POST, $rules);

// if there were errors, re-populate the form fields
if (!empty($errors))
{
$fields = $_POST;
}

// no errors! redirect the user to the thankyou page (or whatever)
else
{
$message = “”;

}
}

?>

<?php

// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user

if (!empty($errors))
{
echo “<div class=‘error’>Please fix the following errors:
<ul>”;
foreach ($errors as $error)
echo "<li>$error</li>
";

echo "&lt;/ul&gt;&lt;/div&gt;";

}

?>

hi there. the validation is placed before the head and the codes of the insertion of data is placed in the body before the form data (:

[quote=ahmednuaman;2340116]And change the the $result condition to the following:


if ( $result == true )
{
echo "<p> the data has been inserted successfully</p>.";
} elseif (!empty($_POST))
{
echo 'the data could not be inserted';
}


[/quote]

hi there. i tried this method and it still does not display message and this time the data could not be inserted successfully into mysql. many many thanks again (:

i paste the insertion codes under the validation codes as you have suggested. sorry for troubling you for so long.

hi there. i have tried different type of insertion method and it could work now :smiley: but i am not very sure about the stuff about header because i want to redirect the current page to a new page so i need to put in headers. But there is an error when i tried putting header into the if-else statement. MANY MANY SORRY!

this is part of the validation codes

$errors = validateFields($_POST, $rules);

// if there were errors, re-populate the form fields
if (!empty($errors))
{
$fields = $_POST;
}

// no errors! redirect the user to the thankyou page (or whatever)
else
{
$message = “”;

}

?>

[U]these will be the codes for the forms and insertion of data

[/U]<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

//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’]);

//insert data into MYSQL
$sql= “INSERT INTO contacts (firstName,lastName) VALUES “;
$sql.= “('”.$firstName.”', '”.$lastName.“')”;

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

if ( !$result )
{
echo ‘the data could not be inserted’;
}
else
{
echo “<p> the data has been inserted successfully</p>.”;
}
//close connection
mysql_close($conn);
}

?>

<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>
</tr>
<tr>
<td> </td>
<td><input name=“AddContact” type=“submit” id=“AddContact” value=“AddContact”>

</form>
</td>
</tr>
</table>

</body>
</html>

[quote=ahmednuaman;2342031]Both these piece of code are on one page yes? Ok, do this:


<?php
if (empty($errors)) {
//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']);


//insert data into MYSQL
$sql= "INSERT INTO contacts (firstName,lastName) VALUES ";
$sql.= "('".$firstName."', '".$lastName."')";

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

if ( !$result )
{
echo 'the data could not be inserted';
}
else
{
echo "<p> the data has been inserted successfully</p>.";
}
//close connection
mysql_close($conn); 
}
}
?>

[/quote]

hi there. the error is solved already. thanks alot for your help. MANY MANY MANY MANY THANKS once again :smiley: :smiley: :smiley: