-1

I am trying to add php to a form to populate it from a database, for a homework assignment. I am recieving a parse error "Parse error: syntax error, unexpected end of file in /l7p1.php on line 97". I cannot seem to find my mistake after staring at this file for hours.

I was originally getting "unexpected (T_VARIABLE)" at any point with double quotes, after changing them all to single quotes (hoping there was something to that I didn't understand, because I'm not sure how that changes things) and that moved the error to the final line of code and the "unexpected end of file".

This is the first bit of php, intended to populate a form with items (tools) from a database


<?php
                                $db=mysli_connect(null,null,null,'weblab')
                                  or die("Can't connect to db:".mysqli_connect_error($db));
                                $q = "select tool_item_no, tool_name, tool_price, tool_weight";
                                $q .= "from tool_t";
                                $q .= "order by tool_name";
                                $dbResult = mysqli_query($db,$q)
                                  or die("Database query error" . mysqli_error($db));
                                $num = mysqli_num_rows($dbResult);
                                if ($num == 0){
                                        echo"<h3>No Data</h3>";
                                        }
                                while ($row = mysqli_fetch_assoc($dbResult)) {
                                        $tool_item_no = $row["tool_item_no"];
                                        $tmool_name = $row['tool_name'];
                                        $tool_price = $row['tool_price'];
                                        $tool_weight = $row['tool_weight'];
                                        echo "<label for = '$tool_name'>Name : $toolname<br/>Price : $tool_price<br/>Weight : $tool_weight<br/>How many
                                        $tool_name for this order?
                                        <input type = 'text' name = '$tool_name' id = '$tool_item_no' size = ]'5'/></label><br/><br/>
                                }
?>

This is the second bit of php in my code, intended to populate a drop-down with a list of states from a database

    <?php
                                $db=mysli_connect(null,null,null,'weblab')
                                  or die('Can't connect to 
                                        db:'.mysqli_connect_error($db));
                                $q = 'select state_abbr, state_name ';
                                $q .= 'from state_t ';
                                $q .= 'order by state_name;';
                                $dbResult = mysqli_query($db,$q)
                                  or die('Database query error' . 
                                               mysqli_error($db));
                                $num = mysqli_num_rows($dbResult);
                                if ($num == 0){
                                                        echo '<option value= 
                                       '$state_abbr' >$state_name</option>';
                        echo '<option>Error</option>';
                                        }
                                while ($row = mysqli_fetch_assoc($dbResult)) {
                                        $state_abbr = $row[state_abbr];
                                        $state_name = $row[state_name];
                                        echo '<option value= '$state_abbr'> 
                                             $state_name</option>';
                                        }
                                ?>
dgajh89
  • 3
  • 2
  • Check the way you nest quotes - for example the code in `echo ' – Nigel Ren Apr 19 '19 at 14:34
  • 2
    See the highlighting on this page, especially at the `or die('Can't connect to`. You'll see the mismatch there. Get yourself a good IDE/Editor with syntax highlighting and error checking, and it will go a long way in helping you find these issues. – aynber Apr 19 '19 at 14:39
  • Look at the places where you use the word `can't`. The use of the word with the single quote is the source of your problems. – Dave Apr 19 '19 at 14:47
  • Thank you so much for the help, @aynber unfortunately my prof requires me to only use notepad or nano (I am not sure if this is standard for college courses, seems a little unnecessary to me but....) to develop these assignments. Could you recommend a good IDE to use for html/php/js? I usually use sublime but not sure if there is a better option? – dgajh89 Apr 20 '19 at 03:47
  • I've never used Sublime, but Atom is good for multiple programming languages, with the right extensions installed. – aynber Apr 20 '19 at 04:03

3 Answers3

0

The difference between simple quotes and double quotes in PHP is that strings within double quotes can be interpreted but not within single quotes(see here).

In PHP when you get an unexpected EOF error, it is a lots of time because you forgot to close a bracket, a parenthesis, a quote, ...

In the first bit of code you posted, the syntax highlighting shows you that on the last line you forget to close the double quote in your echo statement.

Maybe one of the problems you had with single quotes was due to the single quotes in your string. If there is a single quote in a string enclosed in single quotes, the PHP interpreter think you are closing your string.

f222
  • 258
  • 2
  • 9
0

The following should work. You had a mix of single quotes and you also had Can't which needs to be wrapped in "" to escape the single quote. I also added "" in you php and added periods. You need to look up more tutorials on php variables and strings.

    $db=mysli_connect(null,null,null,'weblab')
      or die("Can't connect to db:".mysqli_connect_error($db));
    $q = "select tool_item_no, tool_name, tool_price, tool_weight";
    $q .= "from tool_t";
    $q .= "order by tool_name";
    $dbResult = mysqli_query($db,$q)
      or die("Database query error" . mysqli_error($db));
    $num = mysqli_num_rows($dbResult);
    if ($num == 0){
            echo"<h3>No Data</h3>";
            }
    while ($row = mysqli_fetch_assoc($dbResult)) {
            $tool_item_no = $row["tool_item_no"];
            $tmool_name = $row['tool_name'];
            $tool_price = $row['tool_price'];
            $tool_weight = $row['tool_weight'];
            echo "<label for = ".$tool_name.">Name : ".$toolname".<br/>Price : ".$tool_price."<br/>Weight : ".$tool_weight."<br/>How many
            ".$tool_name." for this order?
            <input type = 'text name = ".$tool_name." id = ".$tool_item_no." size = ]'5'/></label><br/><br/>"
    }



    $db=mysli_connect(null,null,null,'weblab')
      or die("Can't connect to 
            db:".mysqli_connect_error($db));
    $q = 'select state_abbr, state_name ';
    $q .= 'from state_t ';
    $q .= 'order by state_name;';
    $dbResult = mysqli_query($db,$q)
      or die('Database query error' . 
                   mysqli_error($db));
    $num = mysqli_num_rows($dbResult);
    if ($num == 0){
                            echo '<option value= 
           '$state_abbr' >$state_name</option>';
    echo '<option>Error</option>';
            }
    while ($row = mysqli_fetch_assoc($dbResult)) {
            $state_abbr = $row[state_abbr];
            $state_name = $row[state_name];
            echo '<option value= '.$state_abbr.'> 
                 '.$state_name.'</option>';
            }
Brian Nowlan
  • 127
  • 1
  • 7
  • Thanks! It appears I still have a problem with my select statement, but I think this has solved my EOF/ quotations problems! Definitely relieves some frustration! – dgajh89 Apr 20 '19 at 03:50
-1

In your first bit of code. Your echo needs closing quotes and a semicolon.

atippin
  • 1
  • 1