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';
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:
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?
$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"
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'.
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>";
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?)
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
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
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.
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!