Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: php error

  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    248
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php error

    this is also coming up with an error


    Notice: Undefined variable: first in \\insert.php on line 9

    Notice: Undefined variable: last in \\insert.php on line 9

    Notice: Undefined variable: phone in \\insert.php on line 9

    Notice: Undefined variable: mobile in \\insert.php on line 9

    Notice: Undefined variable: fax in \\insert.php on line 9

    Notice: Undefined variable: email in \\.php on line 9

    Notice: Undefined variable: web in \\insert.php on line 9




    line 9
    $query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";

    PHP Code:

    <?
    include("dbinfo.php");

    $first=$_POST['first'];
    $last=$_POST['last'];
    $phone=$_POST['phone'];
    $mobile=$_POST['mobile'];
    $fax=$_POST['fax'];
    $email=$_POST['email'];
    $web=$_POST['web'];

    mysql_connect("xxx.xxx.xxx.xxx",$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");

    $query "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
    mysql_query($query);

    mysql_close();
    ?>

    this is how am submiting the data

    PHP Code:

    <form action="insert.php" method="post">
    First Name: <input type="text" name="first"><br>
    Last Name: <input type="text" name="last"><br>
    Phone: <input type="text" name="phone"><br>
    Mobile: <input type="text" name="mobile"><br>
    Fax: <input type="text" name="fax"><br>
    E-mail: <input type="text" name="email"><br>
    Web: <input type="text" name="web"><br>
    <
    input type="Submit">
    </
    form
    Last edited by ChronicleX.com; 06-10-2005 at 10:50 AM. Reason: New errors
    services

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For the first one what is the actual error?
    Where is $first coming from? Is it POST or GET are using using SuperGlobals $_POST or $_GET?

    If not Id suggest doing so and validating the data before going into the database, you dont want any nasties happening.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    248
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it seems that my 1st error has fixed it self? but the 2nd error i posted at the top i still have no idea why it dont work???

    [HELP]
    services

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Instead of
    PHP Code:
    $query "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')"
    Which will only work with register_globals On Like i said use $_POST

    PHP Code:
    $query "INSERT INTO contacts VALUES ('','".$_POST['first']."','".$_POST['last']."','".$_POST['phone']."','".$_POST['mobile']."','".$_POST['fax']."','".$_POST['email']."','".$_POST['web']."')"
    Just remember if its post data use $_POST if its in the Query string or a form using get use $_GET

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    248
    Thanks
    0
    Thanked 0 Times in 0 Posts
    where do i start!!!!


    Notice: Undefined index: first in \\insert.php on line 4

    Notice: Undefined index: last in \\insert.php on line 5

    Notice: Undefined index: phone in \\insert.php on line 6

    Notice: Undefined index: mobile in \\insert.php on line 7

    Notice: Undefined index: fax in \\insert.php on line 8

    Notice: Undefined index: email in \\insert.php on line 9

    Notice: Undefined index: web in \\insert.php on line 10

    Notice: Undefined index: first in \\insert.php on line 15

    Notice: Undefined index: last in \\insert.php.php on line 15

    Notice: Undefined index: phone in \\insert.php on line 15

    Notice: Undefined index: mobile in \\insert.php on line 15

    Notice: Undefined index: fax in \\insert.php on line 15

    Notice: Undefined index: email in \\insert.php on line 15

    Notice: Undefined index: web in \\insert.php on line 15



    line4,5,6,7,8,9,10

    $first=$_POST['first'];
    $last=$_POST['last'];
    $phone=$_POST['phone'];
    $mobile=$_POST['mobile'];
    $fax=$_POST['fax'];
    $email=$_POST['email'];
    $web=$_POST['web'];

    line 15

    $query = "INSERT INTO contacts VALUES ('','".$_POST['first']."','".$_POST['last']."','".$_POST['phone']."','".$_POST['mobile']."','".$_POST['fax']."','".$_POST['email']."','".$_POST['web']."')";
    services

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You usually get this when the variables you're trying to access don't exist or haven't been declared. Generally you can get around the problem using isset($variable) to test each of the variables before you try and use them. For instance, with the variables you're calling from $_POST, try this to make sure that any that are empty are still declared and don't cause an error. You can either do this for each of your $_POST variables:
    PHP Code:
    if(isset($_POST['first'])) {
        
    $first $_POST['first'];
    } else {
        
    $first '';

    Or a neater way to write the same thing that won't take up so much space is:
    PHP Code:
    $first = (isset($_POST['first']) ? $_POST['first'] : ''); 

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try using print_r($_POST) also if your going to define $first = $_POST['first'] you dont need the $_POST['first'] in the Query.

    Also before you do the insert into the query maybe even try this

    PHP Code:
    // maybe changing <input type="Submit"> to <input type="Submit" name="submit"> as this is how its going to work
    if (isset($_POST['submit']))
    {
        include(
    "dbinfo.php");

        
    $first=$_POST['first'];
        
    $last=$_POST['last'];
        
    $phone=$_POST['phone'];
        
    $mobile=$_POST['mobile'];
        
    $fax=$_POST['fax'];
        
    $email=$_POST['email'];
        
    $web=$_POST['web'];

        
    mysql_connect("xxx.xxx.xxx.xxx",$username,$password);
        @
    mysql_select_db($database) or die( "Unable to select database");

        
    $query "INSERT INTO contacts VALUES     ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
        
    mysql_query($query);

        
    mysql_close(); 
    }
    else
    {
        echo 
    "Error Didnt press Submit";


  • #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    248
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how come when i press submit it opens like am trying to download it? php is working i did phpinfo;


    [code]

    <form action="insert.php" method="post">
    First Name: <input type="text" name="first"><br>
    Last Name: <input type="text" name="last"><br>
    Phone: <input type="text" name="phone"><br>
    Mobile: <input type="text" name="mobile"><br>
    Fax: <input type="text" name="fax"><br>
    E-mail: <input type="text" name="email"><br>
    Web: <input type="text" name="web"><br>
    <input type="Submit" name="submit">

    [code]
    services

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just tried it here and it seems fine. I can only think that there's either a problem with php or apache/IIS whatever you have unless there is something in insert.php or one of the included files that's sending a header prompting the download dialogue.

  • #10
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this:

    PHP Code:
    $query "INSERT INTO contacts VALUES ('','{$first}','{$last}','{$phone}','{$mobile}','${fax}','{$email}','{$web}')";
    mysql_query($query); 

  • #11
    Regular Coder
    Join Date
    Mar 2005
    Posts
    248
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by delinear
    I just tried it here and it seems fine. I can only think that there's either a problem with php or apache/IIS whatever you have unless there is something in insert.php or one of the included files that's sending a header prompting the download dialogue.

    after looking at my code for 4 hours and kicking the pc a few times i found what it was i must of saved the file wrong and not as a php file even though it say insert.php

    so when i pressed submit it tried to download



    grrrrrrr or maybe not

    when i take this away

    <html xmlns="http://www.w3.org/1999/xhtml">

    it trys to download but when i add it back in it shows php as html even with <?php tags

    any ideas guys?????
    Last edited by ChronicleX.com; 06-10-2005 at 05:43 PM.
    services

  • #12
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, the joys of programming The problem is never what you expect it to be.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •