Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > syntax errors...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-02, 14:41
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
Arrow syntax errors...

I get this SQL syntax error when I try to use a script I made to create a table in a database. Any idea whats going on here?

http://www.eyesontennis.com/serror.gif
Reply With Quote
  #2 (permalink)  
Old 12-10-02, 17:15
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
well can't see the rest of the page, your error seems to be in the query but you cut it off. just post the create table code so we can help you.
__________________
www.snippetlibrary.com
Reply With Quote
  #3 (permalink)  
Old 12-10-02, 18:08
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
Ah sorry, thought the error was in the top 2 lines! Anyway...

Code:
<?php // db connection // $connection = mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("nameofdb") or die(mysql_error()); // db querys // $sql = "CREATE TABLE $_POST[table_name] ("; // loop start // for ($i = 0; $i < count($_POST[fieldname]); $i++) { // field name query // $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i]; // field definintions // if ($_POST[field_length][$i] != "") { $sql .= " (".$_POST[field_length][$i]."),"; // crappy other block thing // } else { $sql .= ","; } // close loop // } // some crappy func // $sql = substr($sql, 0, -1); // close parent // $sql .= ")"; // db query shit // $result = mysql_query($sql,$connection) or die(mysql_error()); // testing values // if ($result) { $msg = "<p>".$_POST[table_name]." has been created!</p>"; } ?> <HTML> <HEAD> <TITLE>Create a Database Table: Step 3</TITLE> </HEAD> <BODY> ## nice info ## <h1>Adding table to database...</h1> // print message // <?php echo "$msg"; ?> // validate // </BODY> </HTML>
Reply With Quote
  #4 (permalink)  
Old 12-10-02, 18:17
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
you forgot your ending ) in this line

PHP Code:
if ($_POST[field_length][$i] != "") {
   
$sql .= " (".$_POST[field_length][$i].")")";

// crappy other block thing //

} else {
  $sql .= "
,";

also you can't have a , at the end...
__________________
www.snippetlibrary.com
Reply With Quote
  #5 (permalink)  
Old 12-11-02, 09:48
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
I get a parse error on line 45..
Reply With Quote
  #6 (permalink)  
Old 12-11-02, 10:08
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
oh sorry I forgot a " in there, try this and tell us what it echos out?
PHP Code:
<?php

// db connection //

$connection mysql_connect("localhost""username""password") or die(mysql_error());
mysql_select_db("nameofdb") or die(mysql_error()); 


// db querys //

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

// loop start //

for ($i 0$i count($_POST[fieldname]); $i++) {

// field name query //

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

// field definintions //

if ($_POST[field_length][$i] != "") {
   
$sql .= " (".$_POST[field_length][$i]."),";

// crappy other block thing //

} else {
  
$sql .= ",";
}

// close loop //

}

// some crappy func //

$sql substr($sql0, -1);

// close parent //

$sql .= ")";

// db query **** //
echo $sql;
$result mysql_query($sql,$connection) or die(mysql_error());
if (!
$result){echo mysql_error();}
// testing values //

if ($result) {
   
$msg "<p>".$_POST[table_name]." has been created!</p>";
}

?>

<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE>
</HEAD>
<BODY>

## nice info ##

<h1>Adding table to database...</h1>

// print message //

<?php echo "$msg"?>

// validate //

</BODY>
</HTML>
__________________
www.snippetlibrary.com
Reply With Quote
  #7 (permalink)  
Old 12-11-02, 12:03
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
CREATE TABLE )You have an error in your SQL syntax near ')' at line 1
Reply With Quote
  #8 (permalink)  
Old 12-11-02, 15:46
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
what does your form look like?
__________________
www.snippetlibrary.com

Last edited by scoutt : 12-11-02 at 16:26.
Reply With Quote
  #9 (permalink)  
Old 12-11-02, 16:33
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
I noticed that you have different names for the fieldname

$_POST[fieldname] and

$_POST[field_name]

make sure you get the right one.
__________________
www.snippetlibrary.com
Reply With Quote
  #10 (permalink)  
Old 12-12-02, 07:23
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
Corrected the error, but I still get the same syntax error as before. :/
Reply With Quote
  #11 (permalink)  
Old 12-12-02, 09:44
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
and I still want to know what your form looks like. is field_name an array?
__________________
www.snippetlibrary.com
Reply With Quote
  #12 (permalink)  
Old 12-12-02, 11:55
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
PHP Code:
<?
// checker //

if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
   echo 
"You will need to enter values for <b>each</b> field.";
   exit;
}



$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>"
;



for (
$i 0$i $_POST[num_fields]; $i++) {



$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>"
;



}



$form_block .= "<TR><TD ALUGN=CENTER COLSPAN=3><INPUT TYPE=\"submit\" VALUE=\"Create Table\"></TR></TD></TABLE></FORM>";



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

<!-- display form block -->

<? echo "$form_block"?>

<!-- validate -->

</BODY>
</HTML>


Thats the form that links to the create table file. This file is linked with a previous form to find out the table name and number of fields.
Reply With Quote
  #13 (permalink)  
Old 12-12-02, 14:47
scoutt scoutt is offline
Registered User
 
Join Date: Dec 2002
Posts: 11
well, I changed this

for ($i = 0; $i < count($_POST[fieldname]); $i++) {


to this


for ($i = 0; $i < count($_POST[field_name]); $i++) {

and it worked fine.
__________________
www.snippetlibrary.com
Reply With Quote
  #14 (permalink)  
Old 12-12-02, 17:25
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
I fixed mine with what you did but I still get the same syntax error.

Maybe the database is corrupted? Version of PHP? What things would cause this?
Reply With Quote
  #15 (permalink)  
Old 12-12-02, 17:28
noodle noodle is offline
Registered User
 
Join Date: Dec 2002
Posts: 17
Just tested it, it worked!

Thankyou so much for your help! *is very happy now *
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On