need help installing a table in mysql

I really need help. I’m somewhere between a beginner and an intermediary PHP programmer. I got the following code from a book by Larry Ullman, for installing tables into a database. Everything is letter for letter, except for the table column values and also the connect to database section has been placed in a separate external file.

“http//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

table2 <?php //this file installs tables into database. needs the connect or config script to access the database. //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //include the config file require_once("config.php"); //Define the query. $query = "CREATE TABLE blogs ( blog_id MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , id MEDIUMINT, entrydate_id MEDIUMINT, title VARCHAR( 50 ) NOT NULL, entry VARCHAR( 50 ) NOT NULL , )"; //Run the query. if (@mysql_query ($query)) { print '

The table has been created.

'; } else { die('

Could not create the table becasue: ' .mysql_error().'.

The query being run was: '.$query.'

'); } mysql_close();// Close the database connection. ?>

However, when I try to open this file, I get the following error message:

“http//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
Could not create the table becasue: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘)’ at line 8.

The query being run was: CREATE TABLE blogs ( blog_id MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , id MEDIUMINT, entrydate_id MEDIUMINT, title VARCHAR( 50 ) NOT NULL, entry VARCHAR( 50 ) NOT NULL , )

Obviously, something is preventing the table from getting installed. Now when I use this other code which I had previously successfully used for another table, I get a message which suggest that the table was successfully inserted into my database.

<?php //this file installs tables into database. needs the connect or config script to access the database. //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //include the config file require_once("config.php"); //define the query $query = mysql_query(" CREATE TABLE `blogs` ( blog_id MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , id MEDIUMINT, entrydate_id MEDIUMINT, title VARCHAR( 50 ) NOT NULL, entry VARCHAR( 50 ) NOT NULL, PRIMARY KEY ( `id` ) , UNIQUE ( `username` ) )"); //display all tables echo "Installed! Please delete this file."; ?>

However, when I subsequently try to run the following query that’s supposed to insert data into the table, I get an error message which says the table doesn’t exist.

<?php //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); if (isset ($_POST['submit'])) { //handle the form. //connect to database require_once("config.php"); //define the query. $query = "INSERT INTO blogs (blog_id, title, entry) VALUES (0, '{$_POST['title']}', '{$_POST['entry']}')"; "INSERT INTO entrydates (entrydate_id, entrydate) VALUES (0, NOW())"; //execute the query if (@mysql_query ($query)) { print '

Your entry has been submitted. Thank you!

'; print '

Return to hahap tok

'; } else { print "

Could not add the entry because: " . mysql_error() . ". The query was $query.

"; } mysql_close(); } //Display the form. ?>
                      <p><h2>Please, Add Your Contribution to Half Tok Library!</h2></p>

                      <p>
                        
                          <form action ="blog_entries.php" method="post">
                            <p>Title:       <input type="text" name =title" size="40" maxsize="100" /></p>
                            <p>Explanation: <textarea name= "entry" cols="40" rows="5"></textarea></p>
                            <!-- It is good practice to use the same name from inputs as the corresponding column names in databse, avoiding confusion.--> 
                                            <input type="submit" name="submit" value="Post your Entry!"> 

                          </form>
                       

                      </p>






                ?>

So obviously, for some wierd reason, my table isn’t getting into the database like can want it to. Can anybody help???

I’m pretty sure that you need to specify how many integers you want for the mediumint for id and entrydate_id. Try and put a value after the MEDIUMINT and see if it works:

id MEDIUMINT,
entrydate_id MEDIUMINT,

id MEDIUMINT(5),
entrydate_id MEDIUMINT(5),

Try this syntax and tell me if it works. You also might want to use a GUI MySQL utility like phpmyadmin (most web hosts have it installed in their CPanel and most downloadable web hosting software has something like it) to try and debug some of your SQL scripts. Hope this helps =D.

Sponsor our Newsletter | Privacy Policy | Terms of Service