PDA

View Full Version : PHP Error in displaying message for inserting new records



helloanddie
June 10th, 2008, 02:52 AM
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 :D

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

ahmednuaman
June 10th, 2008, 04:30 AM
You need to remove ' or die (mysql_error());' as 'die' stops PHP compiling and tells it to display mysql_error()

ahmednuaman
June 10th, 2008, 04:31 AM
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.

helloanddie
June 10th, 2008, 04:42 AM
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.

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

ahmednuaman
June 10th, 2008, 04:47 AM
What errors, if any, are you getting?

helloanddie
June 10th, 2008, 04:52 AM
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 (:

ahmednuaman
June 10th, 2008, 05:23 AM
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
}

helloanddie
June 10th, 2008, 05:32 AM
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
}


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

ahmednuaman
June 10th, 2008, 05:37 AM
How is it validated?

helloanddie
June 10th, 2008, 05:49 AM
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:\n<ul>";
foreach ($errors as $error)
echo "<li>$error</li>\n";

echo "</ul></div>";
}

?>

ahmednuaman
June 10th, 2008, 06:28 AM
So where's this in relation to the mysql code?

helloanddie
June 10th, 2008, 11:16 AM
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 (:

ahmednuaman
June 10th, 2008, 11:23 AM
You need to move the 'insertion data code' here:



if (!empty($errors))
{
$fields = $_POST;
// add insertion code here!
}

ahmednuaman
June 10th, 2008, 11:25 AM
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';
}

helloanddie
June 10th, 2008, 10:08 PM
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';
}



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

ahmednuaman
June 11th, 2008, 04:30 AM
Where did you put it?

helloanddie
June 11th, 2008, 05:37 AM
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.

helloanddie
June 12th, 2008, 04:19 AM
hi there. i have tried different type of insertion method and it could work now :D 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!

ahmednuaman
June 12th, 2008, 10:40 AM
Can we see your code now?

helloanddie
June 12th, 2008, 11:34 PM
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 = "";

}

?>

these will be the codes for the forms and insertion of data

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

ahmednuaman
June 13th, 2008, 04:31 AM
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);
}
}
?>

helloanddie
June 13th, 2008, 04:58 AM
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);
}
}
?>


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