Link to home
Start Free TrialLog in
Avatar of Oj
Oj

asked on

MYSQL syntax Error

Hello,

I am Using mysql for win32.

heres what i get
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 '((fddfchar (3))' at line 1



<?
//indicate the database you want to use
$db_name ="testDB";

//connect to database
$connection = @mysql_connect("1.1.1.1","hhgg","hggfg") or die(mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

//start creating the SQL statement
$sql ="CREATE TABLE $_POST['table_name'] ((";

//continue the SQL statement for each new field
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 .= ",";
     }
}

//clean up the end of the string
$sql = substr($sql, 0, -1);
$sql .= ")";

//execute the query
$result = mysql_query($sql,$connection) or die(mysql_error());

//get a good message for display upon success
if ($result) {
     $msg ="<P>".$_POST[table_name]." has been created!</P>";
}

?>

<HTML>
<HEAD>
<TITLE>Create a Database Table:Step 3</TITLE>
</HEAD>
<BODY>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>

<? echo "$msg"; ?>

</BODY>
</HTML>
Avatar of baeuml
baeuml

Hi!

You need a space between your field name and your field type...

$sql .= $_POST['field_name'][$i]." ".$_POST['field_type'][$i];

Regards,
-baeuml
Avatar of Oj

ASKER

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 '(fdfd char (3),fdfd char (3))' at line 1

didn't work.


Thank You.
Could you post the resulting SQL string, what you get from an

echo $sql;

Avatar of Oj

ASKER

i actually did not get anything.. same old error.

i will paste all the codes that are connected with this one.
---------------------------------------
tables.html
<HTML>
<HEAD>
<TITLE>Adding Tables</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="do_showfielddef.php">
<P><strong>Table Name:</strong><br>
<INPUT TYPE="text" NAME="table_name" SIZE=30></P>
<P><strong>Number of fields</strong><br>
<INPUT TYPE="text" NAME="num_fields" SIZE=5></P>

<INPUT TYPE="submit" NAME="submit" Value="submit">
</FORM>
</BODY>
</HTML>
-----------------------------------------------
do_showfielddef.php
<?
//validate important input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
     header("Location:http://127.0.0.1/show_createtable.html");
     exit;
}

//begin creating form for display
$form_block ="
<FORM METHOD=\"POST\" ACTION=\"do_createtable.php\">

<INPUT TYPE=\"hidden\" NAME=\"table_name\" VALUE=\"$_POST[table_name]\">

<TABLE CELLSPACING=5 CELLPADDING=5>
<TR>
<TH>FIELD NAME</TH><TH>FIELD TYPE</TH><TH>FIELD LENGTH</TH></TR>";

//count from 0 until you reach the number of fields
for ($i =0;$i < $_POST[num_fields];$i++) {

     //add to the form,one row for each field
     $form_block .="
     <TR>
     <TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"field_name[]\" SIZE=\"30 \"></TD>
     <TD ALIGN=CENTER>
     <SELECT NAME=\"field_type[]\">
     <OPTION VALUE=\"char \">char</OPTION>
     <OPTION VALUE=\"date \">date</OPTION>
     <OPTION VALUE=\"float \">float</OPTION>
     <OPTION VALUE=\"int \">int</OPTION>
     <OPTION VALUE=\"text \">text</OPTION>
     <OPTION VALUE=\"varchar \">varchar</OPTION>
     </SELECT>
     </TD>
     <TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"field_length[]\" SIZE=\"5\"></TD>
     </TR>";
}

//finish up the form
$form_block .="
<TR>
<TD ALIGN=CENTER COLSPAN=3><INPUT TYPE=\"submit\" VALUE=\"Create Table\"></TD>
</TR>
</TABLE>
</FORM>";

?>

<HTML>
<HEAD>
<TITLE>Create a Database Table:Step 2</TITLE>
</HEAD>
<BODY>
<H1>Define fields for <? echo "$_POST[table_name]"; ?></H1>

<? echo "$form_block"; ?>

</BODY>
</HTML>
-------------------------------------------------------
do_createtable.php
<?
//indicate the database you want to use
$db_name ="testDB";

//connect to database
$connection = @mysql_connect("12.254.250.215","ojundi","tatung") or die(mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

//start creating the SQL statement
$sql ="CREATE TABLE $_POST['table_name'] (";

//continue the SQL statement for each new field
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 .= ",";
     }
}

//clean up the end of the string
$sql = substr($sql, 0, -1);
$sql .= ")";

//execute the query
$result = mysql_query($sql,$connection) or die(mysql_error());

//get a good message for display upon success
if ($result) {
     $msg ="<P>".$_POST[table_name]." has been created!</P>";
}

?>

<HTML>
<HEAD>
<TITLE>Create a Database Table:Step 3</TITLE>
</HEAD>
<BODY>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$sql"; ?>
<? echo "$msg"; ?>

</BODY>
</HTML>
-------------------------------------------

THANK YOU ALL
ASKER CERTIFIED SOLUTION
Avatar of baeuml
baeuml

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation in the Cleanup topic area:

Answered by baeuml

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

snoyes_jw
EE Cleanup Volunteer