ok so i am trying to make a program that is able to viewed on the web using mysql and php together. i have to show a little icon or something saying would you like to view the listing you submitted. this is my program but i keep getting a error as Parse error: syntax error, unexpected T_STRING in /home/bfinnegan/public_html/listclass.php on line 15
here is listclass.php

<?php
include("include.php");
doDB;

if(!$_POST)
{       $display_block="<h1>Select an entry</h1>";

        $get_list_sql="SELECT id,
                        CONCAT_WS(name,last) AS display_name
                        FROM student_name ORDER BY name, last";
        $get_list_res="mysqli_query($mysqli,$get_list_sql)
                        or die(mysqli_error($mysqli));

        if(mysqli_num_rows($get_list_res)<1)
        {       $display_block .= "Sorry, no records found";
        }else if{
                $display_block .= "
                <form method=\"post\" action=\"".SERVER["PHP_SELF"]."\">
                <p><strong>Select a name to view:</strong><br/>
                <select name=\"sel_id\">
                <option value=\"\">-- Select One --</option>";

                while($recs=mysqli_fetch_array($get_list_fes))
                {       $id=recs['id'];
                        $display_name=stripslashes($recs["display_name"]);
                        $display_block .="<option value=\"".$id."\">".
                        $display_name."</option>";
                }
                $display_block .="
                </select>
                <input type=\"submit\" name=\"submit\"
                                value=\"View Selected Name\"></p>
                </form>";
        }

mysqli_free_result($get_list_res);
} else if($_POST)
{       if($_POST["sel_id"] ==""){
        header("Location: listclass.php");
        exit;
}

        $get_student_sql="SELECT concat_ws(' ',name,last, class_name,class_num,profes,class_name2,class_num2,profes2) as
                display_name FROM student WHERE id='".$POST["sel_id"]."'";
        $get_student_res=mysqli_query($mysqli,$get_student_sql)
                        or die(mysqli_error($mysqli));

        while($name_info=mysqli_fetch_array($get_student_res))'{
                $display_name=stripslashes($name_info['display_name']);
        }
        $display_block="<h1>Showing List for".$display_name."</h1>";
        mysqli_free_result(get_student_res);

        $display_block .="<br/>
        <p align=\"center\">
        <a href=\"".$SERVER["PHP_SELF"]."\">select another</a></p>";
}

mysqli_close($mysqli);
?>
<html>
<head>
<title>Listing</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>

Dani AI

Generated

Good progress so far: correctly spotted the parse/quote issue and the unnamed form field that prevented the “add another” path. The remaining problem described in later posts points to a missing/failed database connection — the mysqli functions are being handed a null handle, which means the connection variable was never set or is out of scope when queries run.

Quick checklist to diagnose and fix the DB handle problem:

  • Confirm what the included file actually does: does it create and assign a connection to a global variable, or does it return a connection? If it is a function, call it (with parentheses) and ensure the connection variable is either returned or declared global inside the function.
  • Turn on development error reporting so hidden warnings show: enable E_ALL and display_errors temporarily.
  • Immediately after the include/call, dump or log the connection variable to verify it is a valid mysqli object/handle.
  • If the connect call can fail, log the connect error rather than continuing to call query functions.
  • Watch for simple typos and case-sensitive names (superglobals and user variables are common culprits).

Minimal connection-check pattern (illustrative):

$mysqli = mysqli_connect('host','user','pass','dbname');
if (!$mysqli) {
  error_log('DB connect failed: '.mysqli_connect_error());
  exit;
}

Other practical tips: use prepared statements for inserts/selects (safer and more reliable), free results and close connections when done, and ensure redirects are issued before any HTML output (use headers_sent() to check). For reference see the PHP manual on mysqli connection and prepared statements: mysqli_connect and prepared statements.

Connectivity is most likely the root cause now (as suggested). Checking the include file and verifying the connection variable immediately after it runs will quickly confirm the exact failure mode.

Recommended Answers

All 7 Replies

Member Avatar for Member #203197

Go back to line 11 - there's an extra double-quote in there.

ah ok thank you so much! i have another problem with a part that goes to this program. it is suppose to go to another page that says would you like to add another but it doesnt. it stays on the same page.

include("include.php");
        if(!$_POST){
        $display_block="
        <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
        <p><strong>First/Last Name:</strong><br/>
        <input type=\"text\" name=\"name\" size=\"20\" maxlength=\"50\">
        <input type=\"text\" last=\"last\" size=\"20\" maxlength=\"50\"></p>

        <p><strong>Class Name:</strong><br/>
        <input type=\"text\" name=\"class_name\" size=\"20\" maxlength=\"50\"></p>

        <p><strong>Class Number:</strong><br/>
        <input type=\"text\" name=\"class_num\" size=\"20\" maxlength=\"25\"></p>

        <p><strong>Professor:</strong><br/>
        <input type=\"text\" name=\"profes\" size=\"20\" maxlength=\"50\"></p>

        <p><strong>Class Name:</strong><br/>
        <input type=\"text\" name=\"class_name2\" size=\"20\" maxlength=\"50\"></p>

        <p><strong>Class Number:</strong><br/>
        <input type=\"text\" name=\"class_num2\" size=\"20\" maxlength=\"25\"></p>

        <p><strong>Professor:</strong><br/>
        <input type=\"text\" name=\"profes2\" size=\"20\" maxlength=\"50\"></p>

        <p><input type=\"submit\" name=\"submit\" value=\"Add Class\"></p>
        </form>";

        }else if($_POST)
        {//add to tables
        if(($_POST["name"]== "") || ($_POST["last"]== "")){
                header("Location: addclass.php");
                exit;
        }

        //connect to the database
        doDB;

        //add to student_name table
 $add_student_sql="INSERT INTO student_name (name, last,class_name,class_num,profes,class_name2,class_num2,profes2)
        VALUES('".$_POST["name"]."','".$_POST["last"]."','".$_POST["class_name"]."','".$_POST["class_num"]."','".$_POST["profes"]."',
                '".$_POST["class_name2"]."','".$_POST["class_num2"]."','".$_POST["profes2"]."' )";
        $add_student_res=mysqli_query($mysqli,$add_student_sql)
                        or die(mysqli_error($mysqli));


        mysqli_close($mysqli);
        $display_block="<p>Your class has been added.
        Would you like to <a href=\"addclass.php\">add another</a>?</p>";
}
?>
<html>
<head>
<title>Add A Class</title>
</head>
<body>
<h1>Add a Class</h1>
<?php echo $display_block;?>
</body>
</html>
Member Avatar for Member #203197

ah ok thank you so much! i have another problem with a part that goes to this program. it is suppose to go to another page that says would you like to add another but it doesnt. it stays on the same page.

That's because you added a condition that if both the First and Last name are blank, just go to the original page. This condition is always true because you didn't give the "last" field a name:

<input type=\"text\" name=\"name\" size=\"20\" maxlength=\"50\">
        <input type=\"text\" last=\"last\" size=\"20\" maxlength=\"50\"></p>

That last line should say name=\"last\" .

ah ok thanks! now i get this error Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/bfinnegan/public_html/addclass.php on line 46

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home/bfinnegan/public_html/addclass.php on line 47

Member Avatar for Member #203197

Looks like you're not properly connecting to the database. But I can't very well test that - looks like the db connection code is in your include file.

look the problem is with connectivity only ```try out that

ah ok.. ill look at it and try to fix thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.