SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to everyone that has tried to help me, but my form will still not insert any data into database. I have included your suggestions and have even cut and pasted the format from other php forms in the tutorial. What am I doing wrong, or what is missing?

    Thanks!
    ----------------------------
    <?

    //Check to see if the form was submitted
    if ($submit) {

    //Sets the intial error flag to 0
    $error = 0;

    //If name was submitted empty set $error = 1 and add error msg to errors array
    if (!$contactname) {
    $error = 1;
    $errors[] = "Missing Name";
    }
    //If email was submitted empty set $error = 1 and add error msg to errors array
    if (!$email) {
    $error = 1;
    $errors[] = "Missing Email";
    }

    //If data had no errors
    if ($error == 0) {
    $dbcnx = @mysql_connect("localhost", "XXXXXXx", "XXXXXX");
    mysql_select_db("XXXXXX");
    $inserted = mysql_query("$sql");
    $sql = "INSERT INTO Business SET ".
    "E_Mail='$email', " .
    "Contact_Name='$contactname'";




    }


    }
    ?>
    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    <?

    //Check to see if either the form wasn't submitted or there was an error and display the form
    if (!$submit || ($error == 1)) {

    print '<h2>Sample Form</h2>';
    //If $error = 1 loop through the errors array and print each error
    if ($error == 1) {
    print "<ul>";
    while (list ($key, $val) = each ($errors)) {
    printf('<li>%s', $val);
    }
    print "</ul>";
    }
    printf('<form action="%s" method="post">', $PHP_SELF);
    printf('Name: <input type="text" name="contactname" value="%s"><br>', $contactname);
    printf('Email: <input type="text" name="email" value="%s"><br>', $email);
    printf('<input type="submit" name="submit" value="submit">');
    print '</form>';
    }

    //If no errors print the confirmation page
    else {
    printf('Thank you %s, your information was submitted successfully', $name);
    }


    ?>



    </body>
    </html>

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your problem lies in the fact that you are calling mysql_query($sql) before you define $sql. Glad to see someone is using my code.
    See below:
    //If data had no errors
    if ($error == 0) {
    $dbcnx = @mysql_connect("localhost", "XXXXXXx", "XXXXXX");
    mysql_select_db("XXXXXX");
    $inserted = mysql_query("$sql");
    $sql = "INSERT INTO Business SET ".
    "E_Mail='$email', " .
    "Contact_Name='$contactname'";
    }
    Should be:
    //If data had no errors
    if ($error == 0) {
    $dbcnx = @mysql_connect("localhost", "XXXXXXx", "XXXXXX");
    mysql_select_db("XXXXXX");
    $sql = "INSERT INTO Business SET ".
    "E_Mail='$email', " .
    "Contact_Name='$contactname'";
    $inserted = mysql_query($sql);
    }

    If that is still not working try commenting out the actual line that does the query and and print $sql then take that output to your commandline and paste it in there and run it to check for errors, that will help to eliminate certian errors
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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