#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2

    Object not found


    Hello all,

    I am taking my first step into PHP programming and I have encountered an error. This is my code:

    [code="PHP"]
    <?php //Newsletter_entry.php
    define('DB_NAME', 'captureandkeep');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected) {
    die('Can\'t use ' .DB_NAME . ': ' . mysql_error());
    }

    $value = $_POST['Name'];
    $value2 = $_POST['Postcode'];
    $value3 = $_POST['Mobile'];
    $value4 = $_POST['Email'];
    $value5 = $_POST['Newborn'];
    $value6 = $_POST['BabTod'];
    $value7 = $_POST['Cake'];
    $value8 = $_POST['3plus'];

    $sql = "INSERT INTO newsletter (Name, Postcode, Mobile, Email, Newborn, BabTod, Cake, 3plus) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8')";

    $result = mysql_query($sql);

    mysql_close()

    echo <<<_END
    <html>
    <head>
    <title>Newsletter entry form</title>
    </head>
    <body>
    <form action="post" newsletter_entry.php method="NEWSLETTER" />
    Name: <input type="text" name="Name" />
    <br />
    Postcode: <input type="text" name="Postcode" />
    <br />
    Mobile: <input type="text" name="Mobile" />
    <br />
    E-mail: <input type="text" name="Email" />
    <br/>
    Newborn: <input type="checkbox" name="Newborn" />
    <br/>
    Baby/Toddler: <input type="checkbox" name="BabTod" />
    <br/>
    Cake Smash: <input type="checkbox" name="Cake" />
    <br/>
    3+: <input type="checkbox" name="3plus" />
    <br/>
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>

    _END;
    ?>
    [/code]

    When I click submit, I get this URL come up.

    [code="URL"]
    http://localhost/CK/newsletter/post?Name=Test&Postcode=Test&Mobile=Test&Email=Test&Newborn=on&BabTod=on[/code]

    And an error on the page.

    [code="error"]
    Object not found!

    The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

    If you think this is a server error, please contact the webmaster.

    Error 404

    localhost
    Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7[/code]

    Can anyone see what I am doing wrong?


    Many thanks.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    This:

    Code:
    <form action="post" newsletter_entry.php method="NEWSLETTER" />
    is wrong.

    Your action should be the name of the file to which you're sending the information. Your method should be GET or POST. So it should be

    Code:
    <form action="newsletter_entry.php" method="POST" />

    Comments on this post

    • Mastah^ agrees : Thank you for the help.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    Thank you for your reply, ptr2void.

    I have amended the code and I am still getting the same error. I don't know if it is worth pointing out that the PHP code is all contained in the file called newsletter_entry.php.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Originally Posted by Mastah^
    Thank you for your reply, ptr2void.

    I have amended the code and I am still getting the same error. I don't know if it is worth pointing out that the PHP code is all contained in the file called newsletter_entry.php.
    Can we see the New code?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    Originally Posted by jimmyg999
    Can we see the New code?
    Sure, all I have done is changed the <form action="post" newsletter_entry.php method="NEWSLETTER" /> to <form action="newsletter_entry.php" method="POST" /> but here is the code in its entirety:

    [code="v2"]
    <?php //Newsletter_entry.php
    define('DB_NAME', 'captureandkeep');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected) {
    die('Can\'t use ' .DB_NAME . ': ' . mysql_error());
    }

    $value = $_POST['Name'];
    $value2 = $_POST['Postcode'];
    $value3 = $_POST['Mobile'];
    $value4 = $_POST['Email'];
    $value5 = $_POST['Newborn'];
    $value6 = $_POST['BabTod'];
    $value7 = $_POST['Cake'];
    $value8 = $_POST['3plus'];

    $sql = "INSERT INTO newsletter (Name, Postcode, Mobile, Email, Newborn, BabTod, Cake, 3plus) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8')";

    $result = mysql_query($sql);

    mysql_close()

    echo <<<_END
    <html>
    <head>
    <title>Newsletter entry form</title>
    </head>
    <body>
    <form action="newsletter_entry.php" method="POST" />
    Name: <input type="text" name="Name" />
    <br />
    Postcode: <input type="text" name="Postcode" />
    <br />
    Mobile: <input type="text" name="Mobile" />
    <br />
    E-mail: <input type="text" name="Email" />
    <br/>
    Newborn: <input type="checkbox" name="Newborn" />
    <br/>
    Baby/Toddler: <input type="checkbox" name="BabTod" />
    <br/>
    Cake Smash: <input type="checkbox" name="Cake" />
    <br/>
    3+: <input type="checkbox" name="3plus" />
    <br/>
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>

    _END;
    ?>
    [/code]
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    Although, now I have come back to run the code, I am getting these errors at the top of the webpage:

    [code="more errors"]
    Notice: Undefined index: Name in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 19

    Notice: Undefined index: Postcode in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 20

    Notice: Undefined index: Mobile in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 21

    Notice: Undefined index: Email in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 22

    Notice: Undefined index: Newborn in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 23

    Notice: Undefined index: BabTod in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 24

    Notice: Undefined index: Cake in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 25

    Notice: Undefined index: 3plus in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 26[/code]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Your action is:

    action='newsletter_entry.php'

    but I notice that your top of script comment is

    // Newsletter_entry.php


    Could this be a case issue?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Originally Posted by Mastah^
    Although, now I have come back to run the code, I am getting these errors at the top of the webpage:
    Well - this changes everything. Now you have found your script but you have new errors. Obviously this means that you are not receiving anything in your POST array, assuming that the line numbers mentioned refer to the lines grabbing your input values.

    Did you enter anything in the fields this time? Try to echo out your post array:

    foreach ($_POST as $k=>$v)
    echo "$k is $v<br>";

    and see if you have stuff there.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    Data is now being written to the database which is a step in the right direction. However, I have four boolean tuples that I wish to have either as true or false - dictated by the checkboxes on the webpage. I have problems with the checkboxes whether they are ticked or unticked. If they are ticked, the do not write a true statement in the boolean tuples, if they are unticked, I get a Notice: Undefined index: error again.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Simply google 'php checkbox'

    Comments on this post

    • Mastah^ agrees : Thank you for the help.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    I've almost got it working to how I want it to. I've split the HTML and PHP into two separate files - I'm not too sure if this is best practice or not but now I am actually getting checkboxes writing to the boolean fields in the database, however I am still getting the "Notice: Undefined index: Newborn in C:\xampp\htdocs\CK\newsletter\newsletter_entry.php on line 22" error when check boxes are empty. I have tried setting a default value of the check box to "0" and "false" but still get the errors.

    [code="newsletterform.html"]
    <html>
    <head>
    <title>Newsletter entry form</title>
    </head>
    <body>
    <form action="newsletter_entry.php" method="POST" />
    Name: <input type="text" name="Name" />
    <br />
    Postcode: <input type="text" name="Postcode" />
    <br />
    Mobile: <input type="text" name="Mobile" />
    <br />
    E-mail: <input type="text" name="Email" />
    <br/>
    Newborn: <input type="checkbox" name="Newborn" value="" />
    <br/>
    Baby/Toddler: <input type="checkbox" name="BabTod" value="" />
    <br/>
    Cake Smash: <input type="checkbox" name="Cake" value="" />
    <br/>
    3+: <input type="checkbox" name="3plus" value="" />
    <br/>
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    [/code]

    [code="newsletter_entry.php"]
    <?php //Newsletter_entry.php
    define('DB_NAME', 'captureandkeep');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected) {
    die('Can\'t use ' .DB_NAME . ': ' . mysql_error());
    }

    $value = $_POST['Name'];
    $value2 = $_POST['Postcode'];
    $value3 = $_POST['Mobile'];
    $value4 = $_POST['Email'];
    $value5 = $_POST['Newborn'];
    $value6 = $_POST['BabTod'];
    $value7 = $_POST['Cake'];
    $value8 = $_POST['3plus'];

    $value5 = (isset($_POST['Newborn']) ? true : false);
    $value6 = (isset($_POST['BabTod']) ? true : false);
    $value7 = (isset($_POST['Cake']) ? true : false);
    $value8 = (isset($_POST['3plus']) ? true : false);


    $sql = "INSERT INTO newsletter (Name, Postcode, Mobile, Email, Newborn, BabTod, Cake, 3plus) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8')";

    $result = mysql_query($sql);

    mysql_close();
    echo <<<_END


    _END;
    ?>

    <a href="newsletterform.html">Back</a>
    [/code]
  22. #12
  23. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    You're getting the error because of this line:
    $value5 = $_POST['Newborn'];
    That line is redundant anyway, because you overwrite $value5-$value8 a couple of lines later.

    Using isset or empty is the correct approach to check for checkboxes.

    Some other comments:
    - Your variable names should have meaning; the name "value5" has no meaning.
    - Your code is vulnerable to SQL injection exploits. At a minimum you need to use mysql_real_escape_string() to sanitize your values before putting them into your query. However, the best approach would be to switch to a modern database library like PDO.

    Comments on this post

    • Mastah^ agrees : Thank you for the help.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    Originally Posted by E-Oreo
    You're getting the error because of this line:


    That line is redundant anyway, because you overwrite $value5-$value8 a couple of lines later.

    Using isset or empty is the correct approach to check for checkboxes.

    Some other comments:
    - Your variable names should have meaning; the name "value5" has no meaning.
    - Your code is vulnerable to SQL injection exploits. At a minimum you need to use mysql_real_escape_string() to sanitize your values before putting them into your query. However, the best approach would be to switch to a modern database library like PDO.
    I did look at that and wonder if that was the problem - first time actually using PHP today so please forgive my n00biness :P

    Removing the $value5-$value8 variables without the issets worked a treat.

    For now, the form is for internal use but I will look to improving the security of the PHP when it goes live on a customer facing website. Are there any reference guides where I can review how secure my PHP scripting is against known attacks?

IMN logo majestic logo threadwatch logo seochat tools logo