once load the page, record added without data

Discussion in 'PHP' started by yuyue_hirakiseira, May 9, 2010.

  1. #1
    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 />
    &nbsp;&nbsp;&nbsp; <input name="clear" type="button" value="clear" />&nbsp;&nbsp;&nbsp;
    <input name="submit" type="button" value="submit" />&nbsp;&nbsp;&nbsp;
    <input name="cancel" type="button" value="cancel" /></form>

    </body>

    </html>
     
    yuyue_hirakiseira, May 9, 2010 IP
  2. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    Qc4, May 9, 2010 IP
  3. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    yuyue_hirakiseira, May 9, 2010 IP
  4. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Could you paste the entire updated code here?
     
    Qc4, May 9, 2010 IP
  5. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #5
    Try this code

     
    Bohra, May 9, 2010 IP
  6. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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 />
    &nbsp;&nbsp;&nbsp; <input name="clear" type="button" value="clear" />&nbsp;&nbsp;&nbsp;
    <input name="submit" type="button" value="submit" />&nbsp;&nbsp;&nbsp;
    <input name="cancel" type="button" value="cancel" /></form>

    </body>

    </html>
     
    yuyue_hirakiseira, May 9, 2010 IP
  7. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Add a semicolon at the end of this line: mysql_close($con)

    Does that fix the problem?
     
    Qc4, May 9, 2010 IP
  8. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    still having the same error
    HTTP Error 500.0 - Internal Server Error
    The page cannot be displayed because an internal server error has occurred.
     
    yuyue_hirakiseira, May 9, 2010 IP
  9. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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?
     
    Qc4, May 10, 2010 IP
  10. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    yuyue_hirakiseira, May 10, 2010 IP
  11. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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:
     
    Qc4, May 10, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    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 />
    &nbsp;&nbsp;&nbsp; <input name="clear" type="button" value="clear" />&nbsp;&nbsp;&nbsp;
    <input name="submit" type="button" value="submit" />&nbsp;&nbsp;&nbsp;
    <input name="cancel" type="button" value="cancel" /></form>
    
    </body>
    
    </html> 
    PHP:
     
    Last edited: May 10, 2010
    danx10, May 10, 2010 IP
  13. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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.
     
    yuyue_hirakiseira, May 10, 2010 IP
  14. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Change
    to
    Also, the other buttons don't do anything unless you attach some javascript to them.
     
    musicmasteria, May 10, 2010 IP
  15. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Problem resolved~!!!!!!!!
    Thank you so much for the guidance ^^
     
    yuyue_hirakiseira, May 10, 2010 IP