Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Dec 2002
    Posts
    17

    Arrow Unanswered: 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

  2. #2
    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.

  3. #3
    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>

  4. #4
    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...

  5. #5
    Join Date
    Dec 2002
    Posts
    17
    I get a parse error on line 45..

  6. #6
    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>

  7. #7
    Join Date
    Dec 2002
    Posts
    17
    CREATE TABLE )You have an error in your SQL syntax near ')' at line 1

  8. #8
    Join Date
    Dec 2002
    Posts
    11
    what does your form look like?
    Last edited by scoutt; 12-11-02 at 16:26.

  9. #9
    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.

  10. #10
    Join Date
    Dec 2002
    Posts
    17
    Corrected the error, but I still get the same syntax error as before. :/

  11. #11
    Join Date
    Dec 2002
    Posts
    11
    and I still want to know what your form looks like. is field_name an array?

  12. #12
    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.

  13. #13
    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.

  14. #14
    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?

  15. #15
    Join Date
    Dec 2002
    Posts
    17
    Just tested it, it worked!

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •