helloanddie Posted June 10, 2008 Share Posted June 10, 2008 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."; } Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/ Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 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. Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561844 Share on other sites More sharing options...
helloanddie Posted June 10, 2008 Author Share Posted June 10, 2008 Hi. I have tried that method already and it seems that the message is display even before the contact is inserted successfully into the database. Thanks for your suggestion though Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561847 Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 What do you mean by that? The value returned by mysql_query() is returned only after the query was/wasn't executed. So if you get true returned, that means the query executed successfully. Can you explain what's currently wrong? Orio. Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561848 Share on other sites More sharing options...
helloanddie Posted June 10, 2008 Author Share Posted June 10, 2008 hi, what i meant was when the page loads for the first time, the success message is being displayed even though nothing is being done. regards, helloanddie Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561852 Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 Can you show the full code so I could understand better? Orio. Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561857 Share on other sites More sharing options...
helloanddie Posted June 10, 2008 Author Share Posted June 10, 2008 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> Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561861 Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 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. Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561865 Share on other sites More sharing options...
helloanddie Posted June 10, 2008 Author Share Posted June 10, 2008 yes that is why. What can i do to correct it? Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561868 Share on other sites More sharing options...
formula455 Posted June 10, 2008 Share Posted June 10, 2008 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."; } Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561873 Share on other sites More sharing options...
helloanddie Posted June 10, 2008 Author Share Posted June 10, 2008 thanks man, but it is still not working. Link to comment https://forums.phpfreaks.com/topic/109530-error-in-displaying-message-for-inserting-new-records/#findComment-561887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.