# Error in displaying message for inserting new records

**URL:** <https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814>\
**Category:** programming\
**Created:** [June 10, 2008, 6:52am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814 "2008-06-10T06:52:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 10, 2008, 6:52am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/1 "2008-06-10T06:52:39Z")

</div>

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 😃

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);

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 10, 2008, 8:42am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/2 "2008-06-10T08:42:52Z")

</div>

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

```php

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 (:

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 10, 2008, 8:52am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/3 "2008-06-10T08:52:57Z")

</div>

> [@ahmednuaman;2339927](#):
>
> What errors, if any, are you getting?

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

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 10, 2008, 9:32am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/4 "2008-06-10T09:32:14Z")

</div>

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

```php

//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:

```php

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 (:

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 10, 2008, 9:49am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/5 "2008-06-10T09:49:09Z")

</div>

> [@ahmednuaman;2339954](#):
>
> How is it validated?

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

```

}

?\>

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 10, 2008, 3:16pm UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/6 "2008-06-10T15:16:48Z")

</div>

> [@ahmednuaman;2339967](#):
>
> So where’s this in relation to the mysql code?

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 (:

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 11, 2008, 2:08am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/7 "2008-06-11T02:08:16Z")

</div>

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

```php

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 (:

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 11, 2008, 9:37am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/8 "2008-06-11T09:37:19Z")

</div>

> [@ahmednuaman;2340570](#):
>
> Where did you put it?

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

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 12, 2008, 8:19am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/9 "2008-06-12T08:19:06Z")

</div>

hi there. i have tried different type of insertion method and it could work now 😃 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!

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 13, 2008, 3:34am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/10 "2008-06-13T03:34:24Z")

</div>

> [@ahmednuaman;2341430](#):
>
> Can we see your code now?

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

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

\</body\>  
\</html\>

---

<div class="post-metadata">

**Author:** ![helloanddie](https://avatars.discourse-cdn.com/v4/letter/h/ecb155/32.png) [@helloanddie](https://forum.kirupa.com/u/helloanddie)\
**Post date:** [June 13, 2008, 8:58am UTC](https://forum.kirupa.com/t/error-in-displaying-message-for-inserting-new-records/262814/11 "2008-06-13T08:58:15Z")

</div>

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

```php

<?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 😃 😃 😃
