• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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>
0
Oj
Asked:
Oj
  • 3
  • 2
1 Solution
 
baeumlCommented:
Hi!

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

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

Regards,
-baeuml
0
 
OjAuthor Commented:
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.
0
 
baeumlCommented:
Could you post the resulting SQL string, what you get from an

echo $sql;

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
OjAuthor Commented:
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
0
 
baeumlCommented:
OK, I tried your scripts, but got different errors...

$sql ="CREATE TABLE $_POST['table_name'] (";

should be

$sql ="CREATE TABLE {$_POST['table_name']} (";

so php won't get confused by the ' '

You might wanna always use $_POST['table_name'] instead of $_POST[table_name] and put those { } brackets around your variable if your inside a string.


After that it works, if i fill in all the fields.
If I don't, the script produces and SQL string like

CREATE TABLE martin (asdf char (4),aaasdf char (), char (), char (), char ())



Replace your loop with

for ($i=0; $i < count($_POST['field_name']); $i++){
    if (!empty($_POST['field_name'][$i])) {
        $sql .= $_POST['field_name'][$i]." ".$_POST['field_type'][$i];
        if (!empty($_POST['field_length'][$i])) $sql .= "(".$_POST['field_length'][$i].")";
        $sql .= ",";
    }
}

then it works fine.

I hope it works for you, too.

-baeuml
0
 
snoyes_jwCommented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now