I can't put a table around my results from my code and need some help as I've tried but it comes back with "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\search_go.php on line 27". So could I get help with how to insert a table?
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "calendar"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM caltbl WHERE evtDate LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
<table>
/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
<tr>
$output .= "date: " . $row['evtDate'] . "<br />";
$output .= "Name: " . $row['patient'] . "<br />";
$output .= "Course: " . $row['patientId'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
You can't just insert a HTML tag inside PHP code:
You can however just use an echo to send it out directly:
echo "<table>";
while($row = mysqli_fetch_array($results))
{
$output = "<tr>";
// <tr> This is the problem line.
$output .= "<tr>";
$output .= "<td>date: " . $row['evtDate'] . "<br /></td>";
$output .= "<td>Name: " . $row['patient'] . "<br /></td>";
$output .= "<td>Course: " . $row['patientId'] . "<br /></td>";
$output .= "</tr>";
echo $output;
}
Additionally you didn't close your <tr>. I added some extra snippets to make each field a TD in the table and then closed the row.
Related
view_products() below displays only one data from my table product which contains 20 data. Depending on where you place return $output; either inside while loop which displays the first data or outside the while loop which displays the last data.
<?php echo view_products(); ?>
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password#edadmin";
$dbname = "estore";
$dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//Test if connection occurred,
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")");
}
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($products = mysqli_fetch_assoc($result)) {
$output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<i class=\"ion ion-eye\"></i>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p>" . $products['ProductName'] . " </p>";
$output .= "<p>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
return $output;
}
} else {
return "No product yet";
} // return $output;
}
The reason is that you are resetting the content of $output in the first line of your loop $output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
So if you put the return at the end of the loop, in the first loop it will return the first record and exit the function, if you put it at the end of the function, in each loop $output will be cleared and the content of that loop will only be written in $output so at the end of the function you will only have the content of the last loop in $output
What you can to is to set $output to an empty string and then just append everything in your loop.
Also set the value of $output in the else block and then at the end return $output
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
$output = "";
while ($products = mysqli_fetch_assoc($result)) {
$output .= "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<i class=\"ion ion-eye\"></i>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p>" . $products['ProductName'] . " </p>";
$output .= "<p>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
}
} else {
$output = "No product yet";
} // return $output;
return $output;
}
Hi everyone im new at php so here is my code i am having trouble displaying php in many to many relationship im doing a student information system
here is my database
student
student_id
name
address
course
subject
subject_id
subject_name
subject_code
student_subject
subject_id
student_id
Here is my query
$sql = "SELECT subject_name
FROM subject
LEFT JOIN student_subject ON (student.student_id = 'student_id')
LEFT JOIN subject ON (subject.subject_id = 'subject_id' )";
$records = mysql_query($sql);
and now this is my code to display it please help me how to display this example i just only want to display the subject name and the subject code
while($student=mysql_fetch_array($records)){
echo $student['subject_name'];
echo $student['subject_code'];
}
this is only sample because this is only what i want to dispaly please help me thanks sorry for my bad english. cant search anything about displaying many to many relationship in google i can see some query but i cant search about the code i tried many but i always failed thank you so much
desired Output:
Subject Name ---|--- Subject CODE
ICT___________||_________IT 101
ALGEBRA_______||_________ MATH101
remove the underscore in your vision cant fix the output new here in stackoverflow
<?php
$con = mysql_connect("localhost", "root", "") or die('There is an error to connect DB' . mysql_error());
mysql_select_db('test') or die ('Unable to select DB' . mysql_error());
$query = 'SELECT ST.student_id, ST.name, ST.address, SB.subject_name, SB.subject_code FROM student ST, subject SB, `student_subject` STSB WHERE STSB.student_id=ST.student_id AND STSB.subject_id=SB.subject_id';
$query = mysql_query($query) or die('Unable to execute query' . mysql_error());
if ($query) {
$str = '';
$header = '<tr><td>Student ID</td><td>Name</td><td>Address</td><td>Subject</td><td>Subject Code</td></tr>';
while ($row = mysql_fetch_array($query)) {
$str .= '<tr>';
$student_id = $row['student_id'];
$name = $row['name'];
$address = $row['address'];
$subject = $row['subject_name'];
$subject_code = $row['subject_code'];
$str .= '<td>' . $student_id . '</td>';
$str .= '<td>' . $name . '</td>';
$str .= '<td>' . $address . '</td>';
$str .= '<td>' . $subject . '</td>';
$str .= '<td>' . $subject_code . '</td>';
$str .= '</tr>';
}
if ($str) {
$table = '<table border="1">' . $header . $str . '</table>';
} else {
$tdata = '<tr><td colspan="5">No data found</td></tr>';
$table = '<table border="1">' . $header . $tdata . '</table>';
}
echo $table;
}
Output will be like this:
I've commented out the bottom part, and the SQL query works fine. Its the displaying of the query where the error is coming from i believe.
$host = "127.0.0.1";
$user = "root";
$pass = "Toom13371!";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
// 2. Selecting DB.
$dbname = "filters";
mysql_select_db($dbname);
// 3. Build/Test SQL Query
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
//echo $sql; //Comment/Uncomment to test sql query.
// 4. Retrieve info from MySQL.
$query = mysql_query($sql);
// 5. Display Query.
echo "<table border='1'>
<tr>
<th>Low Frequency</th>
<th>High Frequency</th>
</tr>";
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Any help would be appreciated, I'm sure it's going to be some small stupid error i've over looked.
Thanks in advance :)
I'm guessing, based on your query, that you need to change this
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
and
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
to
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['start_passband'] . "</td>";
echo "<td>" . $row['stop_passband'] . "</td>";
echo "</tr>";
}
Change line
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
Also before query check
$_POST['Lowfreq'] and $_POST['Highfreq']
If there is no value in these variables query will must return empty.
In your query there should not brackets for string.
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
should be:
$sql = "select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'";
I have a database with employee information being displayed into a table and I have managed to make the search box, I have connected to my database and everything now the problem is, when I search for the name i.e. "Daniel" it just gives me back the table which I already have, so then there are two of the tables instead of "Daniels" information.
Heres my code-
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="../Css/index_stylesheet.css">
</head>
<body>
<p>
<?php
$form = "<html>
<h1>Search!</h1>
<form method=\"get\">
<label>Name:
<input type=\"text\" name=\"keyname\" />
</label>
<input type=\"submit\" value=\"Search\" />
</form>
</body>
</html>";
//capture search term and remove spaces at its both ends if there is any
if(!empty($_GET['keyname'])){
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//database connection info
$host = "localhost"; //server
$db = "development_suite"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT firstname ,surname,address,mobile,email
FROM development_suite.eeg_staff;";
$results = mysqli_query($link,$query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1){
$output = "$row_1";
while($row = mysqli_fetch_array($results))
{
$output .= "First Name: " . $row['firstname'] . "<br />";
$output .= "Surname: " . $row['surname'] . "<br />";
$output .= "Address: " . $row['address'] . "<br />";
$output .= "Mobile: " . $row['mobile'] . "<br />";
$output .= "Email: " . $row['email'] . "<br /><br />";
}
}else{
$output = "There was no matching record for the name " .
strip_tags($searchTerm);
}
} else {
$output = "Enter name you are searching for.";
}
echo "$form\n$output";
?>
</body>
</html>
You must add where clause in your query. Something like ...
$query = "SELECT firstname ,surname,address,mobile,email
FROM development_suite.eeg_staff
WHERE firstname LIKE '" . $_REQUEST['keyname'] . "';";
Best regards,
Nebojsa
I have developed a search function which finds patients by their forename and surname and displays the results. However, after implementing the PHP code, the search results are not displaying.
Please note: The error messages are not displaying either;
Does anyone have any idea's why it is not displaying the search results?
<html>
<h1>Search By Name</h1>
<form action="" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="submit" />
</form>
</body>
</html>
<?php
//capture search term and remove spaces at its both ends if there is any
if(isset($_GET['submit'])){
if(!isset($_GET['keyname'])){
$_GET['keyname'] = "";
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "a&e"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE 'Forename' = '$keyname' OR 'Surname' = '$keyname'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "PatientID: " . $row['PatientID'] . "<br />";
$output .= "Forename: " . $row['Forename'] . "<br />";
$output .= "Surname: " . $row['Surname'] . "<br />";
$output .= "Gender: " . $row['Gender'] . "<br />";
$output .= "Illness: " . $row['Illness'] . "<br />";
$output .= "Priority: " . $row['Priority'] . "<br />";
$output .= "Patient History: " . $row['Patient_History'] . "<br /><br />";
}
echo $output;
}
else {
echo "There was no matching record for the name " . $searchTerm; }
}
}
?>
Tried to post this on your previous question. If you want people to answer these you'll have to leave them up long enough for people to answer.
<?php
$form = "<html>
<h1>Search By Name</h1>
<form method=\"get\">
<label>Name:
<input type=\"text\" name=\"keyname\" />
</label>
<input type=\"submit\" value=\"Search\" />
</form>
</body>
</html>";
//capture search term and remove spaces at its both ends if there is any
if(!empty($_GET['keyname'])){
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//database connection info
$host = "localhost"; //server
$db = "a&e"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE Forename LIKE '%$searchTerm%' OR Surname LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1){
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "PatientID: " . $row['PatientID'] . "<br />";
$output .= "Forename: " . $row['Forename'] . "<br />";
$output .= "Surname: " . $row['Surname'] . "<br />";
$output .= "Gender: " . $row['Gender'] . "<br />";
$output .= "Illness: " . $row['Illness'] . "<br />";
$output .= "Priority: " . $row['Priority'] . "<br />";
$output .= "Patient History: " . $row['Patient_History'] . "<br /><br />";
}
}else{
$output = "There was no matching record for the name " . strip_tags($searchTerm);
}
} else {
$output = "Enter name you are searching for.";
}
echo "$form\n$output";
?>
You should put your search code into a block that gets run only if the search term has a value:
if( empty($searchTerm) )
{
echo "Enter name you are searching for.";
}
else
{
// run your search code here and display the result.
}