Page 1 of 1

trouble installing table into mysql

Posted: Fri Dec 31, 2010 5:14 pm
by drayarms
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">

<html xmlns="http://www.w3.org/1999/xhtml">





<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>table2</title>


</head>














<body>

<?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 '<p>The table has been created.</p>';
} else {
die('<p>Could not create the table becasue: <b>' .mysql_error().'</b>.</p><p>The query being run was: '.$query.'</p>');
}

mysql_close();// Close the database connection.


?>

</body>
</html>



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 '<p> Your entry has been submitted. Thank you!</p>';
print '<p> <h3><a style="text-decoration:none" href="blogs.php">Return to hahap tok</a></h3></p>';
} else {
print "<p>Could not add the entry because: <b>" . mysql_error() .
"</b>. The query was $query.</p>";
}
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?????

Re: trouble installing table into mysql

Posted: Fri Dec 31, 2010 5:37 pm
by anantha
entry VARCHAR( 50 ) NOT NULL ,

remove ,

it should be

entry VARCHAR( 50 ) NOT NULL

Re: trouble installing table into mysql

Posted: Fri Dec 31, 2010 5:44 pm
by DigitalMind

Code: Select all

$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 , <-- remove ",". don't add "," before ")"

)";
BTW, MEDIUMINT = 3. Use BIGINT instead of MEDIUMINT(8).

Re: trouble installing table into mysql

Posted: Fri Dec 31, 2010 9:01 pm
by drayarms
Thank you all so much for your help. That had puzzled me for days and driven my project to a standstill. I'm extremely grateful.