Simple php insert driving me crazy

Discussion in 'PHP' started by passingTime, Aug 31, 2011.

  1. #1
    I can't figure this annoying thing out I've tried it over and over but still getting this error:

    Notice: Undefined index: example1 in G:\xampp\htdocs\testing\whatever.php on line 15

    Notice: Undefined index: example2 in G:\xampp\htdocs\testing\whatever.php on line 16

    Please help :s

    The code:

    <?php
    $sql = mysql_connect("localhost","root","") or die (mysql_error());

    if (!isset($sql)) {echo "Could not connect to DB";}

    $select_db = mysql_select_db("test", $sql);

    if (!isset($select_db)) { echo "Could not connect to DB";}


    $example1=($_POST['example1']);
    $example2= ($_POST['example2']);

    mysql_query ("INSERT INTO test (test1, test2) VALUE ('$example1', '$example2')");

    ?>

    <html>
    <body>

    <form action="whatever.php" method="post">
    Username: <input type="text" name="example1" /><br>
    Password: <input type="password" name="example2" /><br>
    <input type="submit" value ="register" />
    </form>


    </body>
    </html>
     
    Solved! View solution.
    passingTime, Aug 31, 2011 IP
  2. Make a perfect site

    Make a perfect site Well-Known Member

    Messages:
    376
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    155
    #2
    Instead of this, use:

    
    $example1 = $_POST['example1'];
    $example2 = $_POST['example2'];
    
    mysql_query("INSERT INTO test (test1, test2) VALUES ('$example1', '$example2')");
    
    PHP:
     
    Make a perfect site, Aug 31, 2011 IP
  3. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply but the error is still the same... Driving me crazy...
     
    passingTime, Aug 31, 2011 IP
  4. #4
    You need to remember, your using php with error reports set to all, which includes notices, which aren't that important and most likely wont appear on a hosted website. Your concerns should be whether the script works or not.

    For a quick fix just write the following before:

    
    $example1 = "";
    $example2 = "";
    
    $example1 = $_POST['example1'];
    $example2 = $_POST['example2'];
    PHP:
    Or use:
    
    if isset() or if empty()
    PHP:
    Not Tested try this:
    
    $example1 = isset($_POST['example1']) && !empty($_POST['example1']);
    $example2 = isset($_POST['example2']) && !empty($_POST['example2']);
    
    PHP:
     
    Last edited: Aug 31, 2011
    MyVodaFone, Aug 31, 2011 IP
  5. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks a lot your suggestion worked :) finally can carry on thanks to you.
     
    passingTime, Aug 31, 2011 IP
  6. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    mm actually now there's another problem. I tried your way and the errors no longer appeared however no data was being sent to the database. I also tried doing it this way:

    if (isset($_POST['example1']) && isset($_POST['example2'])){
    $example1 = $_POST['example1'];
    $example2 = $_POST['example2'];

    mysql_query ("INSERT INTO test (example1, example2) VALUE ('$example1'.'$example2')");

    }
     
    passingTime, Aug 31, 2011 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    mysql_query ("INSERT INTO test (example1, example2) VALUE ('$example1','$example2')"); // you had a dot. here should have been a comma,
    PHP:
    You could always just do this:

    
    
    mysql_query("INSERT INTO test (example1, example2) VALUES ('$_POST['example1']','$_POST['example2']')");
    
    
    PHP:
     
    Last edited: Aug 31, 2011
    MyVodaFone, Aug 31, 2011 IP
  8. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Edit... It's working now thanks to the comma fix. Can't believe i wasted your time with that... Really appreciate the help though it's working now.
     
    Last edited: Aug 31, 2011
    passingTime, Aug 31, 2011 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    No problem: just a short note to tidy things up, you could use this if its any use.

    
    <?php
    
    if(isset($_POST['example1']) && $_POST['example1']!='') {
    
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    
      mysql_query("INSERT INTO test (example1, example2) VALUES ('$_POST['example1']','$_POST['example2']')");
      echo "All done";
    }
    
    ?>
    
    <html>
    <body>
    
    <form action="#" method="post">
    Username: <input type="text" name="example1" /><br>
    Password: <input type="password" name="example2" /><br>
    <input type="submit" value ="register" />
    </form>
    
    </body>
    
    Code (markup):
     
    MyVodaFone, Aug 31, 2011 IP