Parse error: syntax error, unexpected '"' in ....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • riverdale1567
    New Member
    • Dec 2009
    • 13

    Parse error: syntax error, unexpected '"' in ....

    Hi All
    More help required by a newbie, thx in advance...

    I am trying to run a query
    Code:
    $query = 'SELECT * FROM BIZ_APARTMENTS WHERE ((bizState='" . mysql_real_escape_string($_POST['b']) .
    "'&&(bizCity='" . mysql_real_escape_string($_POST['c']) . "')';
    what is the proper way to format this?
    thx again
  • Scooby10
    New Member
    • Jan 2010
    • 5

    #2
    Switch your quotes around...

    Comment

    • riverdale1567
      New Member
      • Dec 2009
      • 13

      #3
      Bigger Question...

      First thanks Scooby for your help.
      I am trying to post data from 2 different scripts to the query you see below
      Code:
      $query = 'SELECT * FROM BIZ_APARTMENTS WHERE ((bizState="' . mysql_real_escape_string($_POST['b']) .
      '"&&(bizCity="' . mysql_real_escape_string($_POST['c']) . '")';
      the post data is coming from 2 different pages.

      This one generates a drop down
      Code:
      <?php
      /*  Program name: buildSelect.php
       *  Description:  Program builds a selection list
       *                from the database.
       */
      ?>
      <html>
      <head><title>Building info by state</title></head>
      <body>
      <?php
        $user="XXXXXXXXXXt";
        $host="XXXXXXXXXXXX";
        $password="XXXXXXXXX";
        $database = "XXXXXXXXXXXX";
      
        $cxn = mysqli_connect($host,$user,$password,$database)
               or die ("couldn't connect to server");
        $query = "SELECT DISTINCT bizState FROM BIZ_APARTMENTS ORDER BY bizState";
        $result = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
      
       /* create form containing selection list */
        echo "<form action='processform41.php' method='POST'>
              <select name='b'>\n";
      
        while ($row = mysqli_fetch_assoc($result))
        {
           extract($row);
           echo "<option value='$bizState'>$bizState\n";
        }
        echo "</select>\n";
        echo "<input type='submit' value='Select State in which building is located'>
              </form>\n";
      ?>
      </body></html>
      and passes the info post [b] to this script
      Code:
      <?php
      
      $username="XXXXXXXXX";
      $password="XXXXXXXXXX";
      $database="XXXXXXXXXXt";
      $table="BIZ_APARTMENTS";
      $column="bizState";
      
      error_reporting(E_ALL);
      mysql_connect("localhost",$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      
      
      $query = "SELECT DISTINCT bizCity FROM BIZ_APARTMENTS WHERE bizState='" . mysql_real_escape_string($_POST['b']) . "'";
      
      
      $result=mysql_query($query);
      $ret = mysql_query($query) or die(mysql_error());
      $num=mysql_numrows($result);
      
      
      
      
      
      
       echo "<b><center>Cities in State</center></b><br><br>";
      
       /* create form containing selection list */
        echo "<form action='city.php' method='POST'>
              <select name='c'>\n";
      
        $i=0;
        while ($i < $num) {
      
      
        $city=mysql_result($result,$i,"bizCity");
      
      
           echo "<option value='$city'>$city\n";
           $i++;
        }
        echo "</select>\n";
        echo "<input type='submit' value='Select City in which building is located'>
              </form>\n";
       print_r($result);
      
       ?>
      which in turn should pass both post[b] and post[c] to this form for output
      Code:
      <?
      $username="xxxxxxxxxx";
      $password="xxxxxxxxxxx";
      $database="xxxxxxxxx";
      $table="xxxxxxxxx";
      $column="bizState";
      error_reporting(E_ALL);
      
      mysql_connect("localhost",$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      
      
      
      
      $query = 'SELECT * FROM BIZ_APARTMENTS WHERE ((bizState="' . mysql_real_escape_string($_POST['b']) .
      '"&&(bizCity="' . mysql_real_escape_string($_POST['c']) . '")';
      
      
      
      $result=mysql_query($query);
      $ret = mysql_query($query) or die(mysql_error());
      $num=mysql_numrows($result);
      
        mysql_close();
      
      
      
      
       echo "<b><center>Buildings in City</center></b><br><br>";
      
      $i=0;
      while ($i < $num) {
      $name=mysql_result($result,$i,"bizName");
      $address=mysql_result($result,$i,"bizAddr");
      $city=mysql_result($result,$i,"bizCity");
      $state=mysql_result($result,$i,"bizState");
      $zip=mysql_result($result,$i,"bizZip");
      $phone=mysql_result($result,$i,"bizPhone");
      $email=mysql_result($result,$i,"bizEmail");
      
       echo "<b>Name: $name</b><br>Phone: $phone<br>Type: $type<br>Address: $address<br>City: $city<br>State: $state<br>Zip: $zip<br>Email:$email<br>";
      
      $i++;
       }
      print_r($city);
       ?>
      BUT I am getting error message
      Notice: Undefined index: b in /home/attorney/public_html/in-url.com/bigdump/city.php on line 15
      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 '' at line 1
      thanks again, the help i have received from this forum has been priceless..

      Comment

      • Scooby10
        New Member
        • Jan 2010
        • 5

        #4
        Could it be the && in the query? Try replacing that with AND..

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          that’s not it, there’s simply no field named "b" in the last script.

          Comment

          • riverdale1567
            New Member
            • Dec 2009
            • 13

            #6
            so how do I transfer data from the field [b] in the first script into the 3rd script so the query in the 3rd script can use fields 'b' and 'c'?
            Code:
            $query = 'SELECT * FROM BIZ_APARTMENTS WHERE ((bizState="' . mysql_real_escape_string($_POST['b']) .
            '"&&(bizCity="' . mysql_real_escape_string($_POST['c']) . '")';

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              you could add a hidden field to pass down the b value
              Code:
              echo '<input type="hidden" name="b" value="' . $_POST["b"] . '">';

              Comment

              • riverdale1567
                New Member
                • Dec 2009
                • 13

                #8
                syntax error in line 1...

                First, let me say thank you again to both of you for you time and effort in helping me.
                In my 3rd script, I am still getting the error:
                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 '' at line 1
                this is the script:
                Code:
                <?
                $username="xxxxxxxxxx";
                $password="xxxxxxxxx";
                $database="xxxxxxxxxx";
                $table="BIZ_APARTMENTS";
                $column="bizState";
                
                
                mysql_connect("localhost",$username,$password);
                @mysql_select_db($database) or die( "Unable to select database");
                
                
                
                
                $query = 'SELECT * FROM BIZ_APARTMENTS WHERE ((bizState="' . mysql_real_escape_string($_POST['b']) .
                '"&&(bizCity="' . mysql_real_escape_string($_POST['c']) . '")';
                
                
                
                $result=mysql_query($query);
                $ret = mysql_query($query) or die(mysql_error());
                $num=mysql_numrows($result);
                
                  mysql_close();
                
                
                
                
                 echo "<b><center>Buildings in City</center></b><br><br>";
                
                $i=0;
                while ($i < $num) {
                $name=mysql_result($result,$i,"bizName");
                $address=mysql_result($result,$i,"bizAddr");
                $city=mysql_result($result,$i,"bizCity");
                $state=mysql_result($result,$i,"bizState");
                $zip=mysql_result($result,$i,"bizZip");
                $phone=mysql_result($result,$i,"bizPhone");
                $email=mysql_result($result,$i,"bizEmail");
                
                 echo "<b>Name: $name</b><br>Phone: $phone<br>Type: $type<br>Address: $address<br>City: $city<br>State: $state<br>Zip: $zip<br>Email:$email<br>";
                
                $i++;
                 }
                print_r($city);
                 ?>
                Also I am not getting any output either.

                Thank you very much,
                Josh

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  there are 2 closing parentheses missing in the SQL.

                  Comment

                  • riverdale1567
                    New Member
                    • Dec 2009
                    • 13

                    #10
                    Dormilich
                    Dude you are awesome, thanks ever so much. It always amazes when the s*** actually works.
                    Danke
                    Last edited by Dormilich; Jan 18 '10, 12:26 PM. Reason: let’s be a little more polite …

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      thinking like a parser helps. and experience.

                      Comment

                      • riverdale1567
                        New Member
                        • Dec 2009
                        • 13

                        #12
                        Sorry about the profanity, it the Bronx in me that just comes out sometimes :-)

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          that’s why there is an "edit" link.

                          Comment

                          • riverdale1567
                            New Member
                            • Dec 2009
                            • 13

                            #14
                            How many years have you been programming?

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              started with HTML and JS about 10 years ago and doing PHP for about 5 years. but that’s not much since I do it for a hobby.

                              Comment

                              Working...