?
Solved

MYSQL syntax Error

Posted on 2003-03-26
7
Medium Priority
?
336 Views
Last Modified: 2013-12-12
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
Comment
Question by:Oj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 

Expert Comment

by:baeuml
ID: 8218157
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
 

Author Comment

by:Oj
ID: 8219650
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
 

Expert Comment

by:baeuml
ID: 8220054
Could you post the resulting SQL string, what you get from an

echo $sql;

0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 

Author Comment

by:Oj
ID: 8220264
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
 

Accepted Solution

by:
baeuml earned 300 total points
ID: 8220497
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
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 9682842
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question