Could someone give me a clue please?

Discussion in 'PHP' started by Mitchell, Jun 3, 2010.

  1. #1
    The code seems to almost work. I get this error.

    An error has occurred. The item was not added.

    Can someone help me understand why this insert into database code will not work? Thanks.

    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>post</title>
    </head>
    <body>
    <table>
    <form action="new_post.php" method="post">
    <tr>
    <td>cat:</td>
    <td><input type="text" name="cat"
    size="40" maxlength="40"/>
    </td>
    </tr>
    <tr>
    <td>bird:</td>
    <td><input type="text" name="bird"
    size="40" maxlength="40"/>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <input type="submit" value="register" name="post" width="99" height="39"/>
    </td>
    </tr>
    </form>
    </table>
    </body>
    </html>


    <html>
    <head>
    </head>
    <body>

    <?php
    // create short variable names
    $cat=$_POST['cat'];
    $bird=$_POST['bird'];

    if (!$cat || !$bird) {
    echo "You have not entered all the required details.<br />"
    ."Please go back and try again.";
    exit;
    }

    if (!get_magic_quotes_gpc()) {
    $cat = addslashes($cat);
    $bird = addslashes($bird);
    }

    @ $db = new mysqli('localhost', 'animal', 'passwordlife', 'animal');

    if (mysqli_connect_errno()) {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
    }

    $query = "insert into animal values
    ('".$cat."', '".$bird."')";
    $result = $db->query($query);

    if ($result) {
    echo $db->affected_rows." book inserted into database.";
    } else {
    echo "An error has occurred. The item was not added.";
    }

    $db->close();
    ?>
    </body>
    </html>


    create database animal;

    use animal;

    create table catbird
    (
    postid int unsigned not null auto_increment primary key,
    cat char(20) not null,
    bird char(20) not null
    );


    grant select, insert, update, delete
    on animal.*
    to animal@localhost identified by 'passwordlife';
     
    Mitchell, Jun 3, 2010 IP
  2. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Please give mysql a chance to report the issue:

    Replace line 69-74 with:
    
    if ($result) {
      echo $db->affected_rows." book inserted into database.";
    } else {
      echo "An error has occurred. The item was not added.";
    echo "<br><strong>MYSQL Error".mysql_error()."</strong><br>";
    }
    
    PHP:
     
    flexdex, Jun 3, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your response.

    Please excuse me, I am new at this.

    I added what you suggested. I get this on the web page.

    An error has occurred. The item was not added.
    MYSQL Error


    Am I supposed to check MYSQL some place in my computer for information about this error?
     
    Mitchell, Jun 4, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    $query = "insert into catbird (cat, bird) VALUES ('".$cat."', '".$bird."')";
    $result = $db->query($query);

    equals

    INSERT INTO table_name (column1, column2) VALUES (value1, value2) // table_name and not the database_name

    $db = connection process to database "animal"
     
    Last edited: Jun 4, 2010
    MyVodaFone, Jun 4, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Use this instead - that code is wrong.

    echo "<br><strong>MYSQL Error".mysqli_error($db)."</strong><br>";
    PHP:
    However - It looks lke you are using the wrong table name -

    $query = "insert into animal values('".$cat."', '".$bird."')";
    PHP:
    Yet, your ghave called your table 'catbird'.
     
    lukeg32, Jun 4, 2010 IP
  6. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, thanks lukeg32.
    Its early for me too.
     
    flexdex, Jun 4, 2010 IP
  7. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I should have picked a different table name so as to not confuse.

    "catbird" is the table name and "cat" and "bird" are being input. Is it not OK to name my table "catbird".


    Sorry this dose not seem to work for me.
    echo "<br><strong>MYSQL Error".mysqli_error($db)."</strong><br>";
     
    Mitchell, Jun 4, 2010 IP
  8. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #8
    No - this is fine.

    However you are not usnig the table 'catbird'.

    Your query is inserting into a table called 'animal'

    $query = "insert into animal values('".$cat."', '".$bird."')";
    PHP:

    Whats the problem with it (or is it not echoing anything?)
     
    lukeg32, Jun 4, 2010 IP
  9. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I guess the problem is, that your script triesto fill the value for cat into postid and bird into cat.

    That should work:
    ... (cat, bird) values ('".$cat."', '".$bird."')";

    See how MyVodaFone done it, and use your table name instead the name of the database at all. animal.catbird seems to be the name of the db.table
     
    flexdex, Jun 4, 2010 IP
  10. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you guys very much. This worked. I don't fully understand why yet. It is after 1am here so I am going to have to continue this tomorrow. Thanks again.

    $query = "insert into catbird (cat, bird) values
    ('".$cat."', '".$bird."');" or die(mysql_error());


    Database name: animal.
    Table name: catbird.

    My query is inserting into a database called 'animal'.
    $query = "insert into animal values ('".$cat."', '".$bird."');";

    An error has occurred. The item was not added.
    Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\htdocs\clphp02\new_post.php on line 37

    MYSQL Error
     
    Mitchell, Jun 4, 2010 IP
  11. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #11
    in your previous code, you are using mysqli - mysql_error here will not return anything.


    You do not insert into the database per se; You insert into a TABLE inside the database.

    I.E The insert statement you have above is inserting into to TABLE animal - which doesnt exist.

    The SQL you have at the top is correct; with the minor exception of the mysql_error call.
     
    lukeg32, Jun 4, 2010 IP
  12. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Are you using that SQL statement?

    Is the table not called "catbird" and locate below database called "animal" ?
    Then it should be the following SQL statement. Either, you should give mysql the chance to know for what columns you are inserting because there is a row "postid"
    $query = "insert into animal.catbird (cat,bird) values ('".$cat."', '".$bird."');";

    have a good night!
     
    flexdex, Jun 4, 2010 IP