i'm having a problem that once i load the webpage, the data is automatically inserted into the database. i have no idea what had went wrong. please help and below are the codes which i had coded... thank you in advance. insertmem.php (file name) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Name</title> </head> <body> <?php $con = mysql_connect("127.0.0.1","root","2614352"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("cbfsms", $con); $sql="INSERT INTO member (MemName, MemAdd, MemContact, MemEmail) VALUES ('$_POST[MemName]','$_POST[MemAdd]','$_POST[MemContact]','$_POST[MemEmail]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> <form action="" method="post"> Name: <input name="Text1" type="text" /><br /> <br /> Address: <input name="Text2" type="text" /><br /> <br /> Email: <input name="Text3" type="text" /><br /> <br /> Contact: <input name="Text4" type="text" /><br /> <br /> <input name="clear" type="button" value="clear" /> <input name="submit" type="button" value="submit" /> <input name="cancel" type="button" value="cancel" /></form> </body> </html>
Before this: $con = mysql_connect("127.0.0.1","root","2614352"); PHP: Add this: if (isset($_POST['submit'])) { PHP: Then, after this: mysql_close($con); PHP: Add this: } PHP: That way, the query only runs if the form has been submitted.
i get this error after i added those codes... any idea what happened? HTTP Error 500.0 - Internal Server Error The page cannot be displayed because an internal server error has occurred.
test the coded but can't also the error was this HTTP Error 500.0 - Internal Server Error The page cannot be displayed because an internal server error has occurred. here's the code after i added and tried as Bohra coded. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Name</title> </head> <body> <?php if (isset($_POST['submit'])) { $con = mysql_connect("127.0.0.1","root","2614352"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("cbfsms", $con); $sql="INSERT INTO member (MemName, MemAdd, MemContact, MemEmail) VALUES ('$_POST[MemName]','$_POST[MemAdd]','$_POST[MemContact]','$_POST[MemEmail]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) } ?> <form action="" method="post"> Name: <input name="Text1" type="text" /><br /> <br /> Address: <input name="Text2" type="text" /><br /> <br /> Email: <input name="Text3" type="text" /><br /> <br /> Contact: <input name="Text4" type="text" /><br /> <br /> <input name="clear" type="button" value="clear" /> <input name="submit" type="button" value="submit" /> <input name="cancel" type="button" value="cancel" /></form> </body> </html>
still having the same error HTTP Error 500.0 - Internal Server Error The page cannot be displayed because an internal server error has occurred.
Strange, since I don't see any other syntax errors. Have you made any configuration changes recently? Do other PHP scripts in the same directory have the same error? Does it work if you revert the code to how it was in your first post?
yes it works if i revert it back to old code, which it will automatically insert blank data into my database. i haven't done any configuration lately to my laptop. well i have another file also but having the same error, which is insert blank data into the database automatically after it load the webpage.
I'm not sure why that line of code would cause an error, but you could try some alternate lines of code to see if any of them work. Try replacing: if (isset($_POST['submit'])) { PHP: with these, one at a time, and see if any of them work for you: if ($_POST['submit']) { PHP: if (isset($_POST['Text1'])) { PHP: if ($_POST['Text1']) { PHP: if (sizeof($_POST)) { PHP:
This should work: 1. Your form field names do not match your $_POST variables! (theirfore I've changed this). 2. Use mysql_real_escape_string() on user submitted data before querying to a db. (For security purposes and to avoid sql parsing issues). 3. Don't use die() -> http://www.phpfreaks.com/blog/or-die-must-die 4. Unless the $_POST array element is an integer always wrap quotes around it such as $_POST[MemContact] -> $_POST['MemContact'] ------- 5. When posting code post within the php bbcode tags -> http://forums.digitalpoint.com/misc.php?do=bbcode#php (this will help us to debug your code, as the syntax will be highlighted) 6. Always censor your password when posting it publically. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Name</title> </head> <body> <?php if (isset($_POST['submit'])){ $con = mysql_connect("127.0.0.1","root","2614352") or trigger_error("Connection Failed: ".mysql_error($con), E_USER_ERROR); mysql_select_db("cbfsms", $con); $_POST = array_map('mysql_real_escape_string', $_POST); $sql="INSERT INTO member (MemName, MemAdd, MemContact, MemEmail) VALUES ('{$_POST['MemName']}','{$_POST['MemAdd']}','{$_POST['MemContact']}','{$_POST['MemEmail']}')"; if (mysql_query($sql,$con)){ echo "1 record added"; } else { trigger_error("Query Failed: ".mysql_error($con), E_USER_ERROR); } mysql_close($con); } ?> <form method="post"> Name: <input name="MemName" type="text" /><br /> <br /> Address: <input name="MemAdd" type="text" /><br /> <br /> Email: <input name="MemEmail" type="text" /><br /> <br /> Contact: <input name="MemContact" type="text" /><br /> <br /> <input name="clear" type="button" value="clear" /> <input name="submit" type="button" value="submit" /> <input name="cancel" type="button" value="cancel" /></form> </body> </html> PHP:
the above code is ok, since it din't auto insert to my database anymore, but i can't insert when i click the button insert.