SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help with PHP + MySQL

    Hey guys,

    I have been running into problems with this code for a bit now. Since I am just learning PHP, I decided that the best way to learn is to start building. So I got part of the script done, but ran into a problem adding data to a database.

    I have a file called ADD dot php which looks like this:


    <?php include "base dot php"; ?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Beta Test</title>
    <link rel="stylesheet" href="style dot css" type="text/css" />
    </head>
    <body>

    <h1>Add Domain</h1>

    <form method="post" action="addomain dot php" name="loginform" id="loginform">
    <fieldset>
    <label for="domain">Domain/label><input type="text" name="domain" id="domain" /><br /><br />
    <input type="submit" name="submit" id="submit" value="Add Domain!" />
    </fieldset>
    </form>


    <a href="logout dot php">Logout</a>
    and a file called addomain dot php that includes this

    <?php
    $first = $_POST['domain'];
    $sql = "INSERT INTO (domains) VALUES ('$first')";
    mysql_query($sql);
    {
    ?>
    But for some reason, I can't add whatever the user imput into the form into the database. Is there anything wrong with this? It gives me a syntax error like this

    Parse error: syntax error, unexpected $end in addomain dot php on line 6
    If anyone could help, I would really appreciate it.

    Thanks,

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What is the last "{" in the file for? It doesn't match anything, which is probably why it's complaining.

    You need to connect to the database before you can issue queries to it

  3. #3
    SitePoint Member
    Join Date
    May 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, That works. No more syntax erroe but now for some reason, its not adding anything to the database. :/

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Did you connect to the database before calling mysql_query?

    Also, your query is not correct. It's missing either the table name or the name of the column, I'm not sure which...

    PHP Code:
    $sql "INSERT INTO table_name (column_name) VALUES ('$first')"

  5. #5
    SitePoint Member
    Join Date
    May 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow thanks man. That works!

  6. #6
    SitePoint Member
    Join Date
    May 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I try to add another domain, it doesnt add a new row. Why is there?

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Two things:

    1) Share the schema of the table. Show the CREATE TABLE query that created it.

    2) Share the error message.

    PHP Code:
    $ret mysql_query($sql);
    if (!
    $ret) {
      echo 
    "Error: " mysql_error();


  8. #8
    SitePoint Member
    Join Date
    May 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what you mean for #1 but if I got to phpMyAdmin, and click the table, it says this for the MySQL query

    SELECT *
    FROM `domains`
    LIMIT 0 , 30
    Im getting this for the error

    Error: Duplicate entry '' for key 1

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to set the primary key of the table to an auto_incrementing integer.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    You need to set the primary key of the table to an auto_incrementing integer.
    Why?

    Domains are unique, they would make a suitable key without adding an artificial key.

    We don't even know what this table looks like.

  11. #11
    SitePoint Member
    Join Date
    May 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have 3 rows in the database. uid, username, and domain. I'm not worried about the username and uid now, just need to add the domains.

  12. #12
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cezary Kubel View Post
    I have 3 rows in the database. uid, username, and domain. I'm not worried about the username and uid now, just need to add the domains.
    Your table has unique constraints on one or more of the columns, and by not specifying values for all the columns, you're violating that constraint by trying to insert nothing twice.


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
  •