SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    May 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Connecting PHP to Mysql database...

    Hi.Newbie question here so please forgive if it seems simple.I have PHP4 & MYSQL installed and using apache as the web server.I have done a few tutorials and PHP works great as does MYSQL.I am now using a HTML form to take a users name email etc and store it into my SQL database.
    <form action = "joinnewsletter.php" method = "post">
    <table>
    <tr><td>
    Name : </td>
    <td><input type = "text" name = "name">
    </td></tr>
    <tr><td>
    Email :
    </td>
    <td><input type = "text" name = "email">
    </td></tr>
    <tr><td>
    <input type = "submit" value = "join"></td>
    <td><input type = "reset" value = "clear"></td></tr>
    </table>
    </form>

    This I presume triggers this code in the "joinnewsletter.php" as follows:
    PHP Code:
    <?php
    //make a connection
    $connection mysql_connect("localhost","gary","1703")
    or die(
    "cannot make connection");
    //select our database email 
    $db mysql_select_db("email" $connection)
    or die(
    "cannot find database");
    //insert the values of our form into our database
    $sql_query "INSERT INTO email VALUES (\"$name\",\"$email\")";
    $results mysql_query($sql_query);
    //display a thank you message and a link
    echo ("Thank you for subscribing to our newsletter<br>");
    echo (
    "<a href = 'index.htm'>Back to index page</a>");
    ?>
    If I go to a mysql cmd prompt and view the table data it tells me "empty set(0.05 sec)".
    Can anyone please tell what is happening...or not happening more to the point!
    I can insert data into the table use cmd prompt btw.
    Thanx for your time
    G
    GARY JOHN CHARLES BRETT

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your query replace
    PHP Code:
    $sql_query "INSERT INTO email VALUES (\"$name\",\"$email\")";

    with

    $sql_query 
    "INSERT INTO email (name, email) VALUES ('$name','$email')"
    so it would be:
    PHP Code:
    <?php
    $c 
    mysql_connect("localhost""""") or die(mysql_error());
    mysql_select_db("email" $c) or die(mysql_error());
    $r mysql_query("INSERT INTO email (name, email) VALUES ('$name','$email')");
    if (
    $r == TRUE) {
      echo 
    "Thank you for subscribing to our newsletter<br>";
      echo 
    "<a href = 'index.htm'>Back to index page</a>";
    } else {
      echo 
    "There was an error inserting your information.";
    }
    ?>
    and that should work.
    Last edited by notepad_coder; May 1, 2002 at 09:17.
    - the lid is off the maple syrup again!

  3. #3
    SitePoint Member
    Join Date
    May 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    Cheers Notepad_Coder...thats done the trick...
    Appreciate your time m8
    GARY JOHN CHARLES BRETT

  4. #4
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure you always include the name of the row you want to insert something into like
    PHP Code:
    INSERT INTO table (row1row2row3VALUES ('$var1''$var2''$var3'); 
    And in MySQL use ' and not " for it
    - the lid is off the maple syrup again!

  5. #5
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And an additional note, do not put integers between quotes, neither double quotes, nor single ones.
    Cheers for that one Matt!

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup Mark.

    notepad_coder, you can use ' or " in MySQL; they'll work exactly the same. it's just usually easier to use ' in PHP so you don't have to escape the ".

    also,

    INSERT INTO VALUES ('$name', '$email')

    without the ` (name, email) ' part is fine as long as name and email (in that order) are the only columns in the table. however, i do like to include the column names so that things don't get messed up.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  7. #7
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by globalg
    "INSERT INTO email VALUES (\"$name\",\"$email\")"
    That is what I uwas referring to, you should use ' and not \".
    - the lid is off the maple syrup again!

  8. #8
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know that, but

    "INSERT INTO email VALUES (\"$name\",\"$email\")"

    will work fine (again, assuming that name and email are the only columns in the table).

  9. #9
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    argh stupid quotes, I always get them confused. My ways just looks better
    - the lid is off the maple syrup again!

  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, yes, it definitely looks better. i was just pointing out that the other way will work.


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
  •