Posts: 77
Name: Daniel
Location: Stony Point , Noth Carolina
|
OK, Im at home now I can just copy and paste my code. I was at work when i made this post. So I was going from memory as to how the code goes. OK first of all here is the error message that I am getting:
"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 '" (ITEM_ID int (5),ITEM_TITLE varchar (50),ITEM_DESC text,"
Here is the code of the form that is generated by php that gives me the error
PHP Code:
<?php if ( (!$_POST[table_name]) || (!$_POST[num_fields])) { header("Location: http://localhost/show_createtable1.html"); exit; } ?> <HTML> <HEAD> <TITLE>Create a Database Table: Step 2</TITLE> </HEAD> <BODY> <H1> Define fields for <?php echo "$_POST[table_name]"; ?></H1> <FORM method="POST" action="do_createtable.php"> <INPUT type="hidden" name="table_name" value=<?php echo "$_POST[table_name]"; ?>"> <table cellspacing=5 cellpadding=5> <tr> <th>FIELD</th><th>FIELD TYPE</th> <th>FIELD LENGTH</th></tr> <?php for ($i = 0 ; $i <$_POST[num_fields]; $i++) { echo " <tr> <td align=center> <input type= 'text' name= 'field_name[]' size= '30'> </td> <td align=center> <select name='field_type[]'> <option value='int'>int</option> <option value='text'>text</option> <option value='varchar'>varchar</option> <option value='float'>float</option> </select> </td> <td align=center> <input type='text' name='field_length[]' size='5'> </td> </tr>"; } ?> <tr> <td align=center colspan=3> <INPUT type="submit" value"Create Table"></td> </tr> </table> </FORM> </BODY> </HTML>
And here is the code that suppose to make the table in MySQL.
PHP Code:
<HTML> <HEAD> <TITLE>Create a Database Table: Step 3</TITLE> </HEAD> <BODY>
<h1>Adding table <?php echo "$_POST[table_name]"; ?> </h1> <?php $sql = "CREATE TABLE $_POST[table_name] ("; for ($i = 0; $i < count($_POST[field_name]); $i++){ $sql .= $_POST[field_name] [$i]. " " . $_POST[field_type][$i]; if ($_POST[field_length] [$i] != "") { $sql .= " (" . $_POST[field_length][$i ]."),"; } else { $sql .= ","; } } $sql = substr($sql, 0, -1); $sql .= "); ";
//create connection $conn = mysql_connect("localhost","user","password") or die (mysql_error() ); //select database $db = mysql_select_db("bravo" , $conn) or die(mysql_error() ); //execute SQL query and get result $sql_result = mysql_query($sql , $conn) or die(mysql_error() ); //print success message if ($sql_result) { echo "<p>$_POST[table_name] has been created!</p>"; } ?> </BODY> </HTML>
Ive been looking at this code now for close to 8 hours. Maybe someone here can give me a reason for the error.
__________________
What is Yuwie?
Please login or register to view this content. Registration is FREE
|