I'm having some trouble generating output from an sql query via php. When I execute the query "SELECT * from projectdb WHERE name = 'Crys' or ID = 14142" on phpmyadmin it returns valid results, but attempting to do so by passing a post value yields an empty table. See code below:
<html>
<title>Search result</title>
<body>
<table border="1px">
<tr>
<td>name</td>
<td>ID</td>
<td>position</td>
<td>job scope</td>
<td>contact_no</td>
<td>days_off</td>
<td>wages</td>
</tr>
<?php
if (isset($_POST['value']))
{
$ID=$_POST['staff ID'];
$name=$_POST['staff name'];
$admincon =mysqli_connect("localhost","root","","projectdb");
/* Query I need to execute and print in table*/
$sqlsrch2 =mysqli_query($admincon, "select * from staff where name='".$name."' or ID='".$ID."'");
while($result=mysqli_fetch_assoc($sqlsrch2)){
?>
/* table where results needs to be printed */
<tr>
<td><?php echo $result['name'];?></td>
<td><?php echo $result['ID'];?></td>
<td><?php echo $result['position'];?></td>
<td><?php echo $result['job_scope'];?></td>
<td><?php echo $result['contact_no'];?></td>
<td><?php echo $result['days_off'];?></td>
<td><?php echo $result['wages'];?></td>
</tr>
<?php
}
}
?>
</table>
</body>
</html>
A few points to note:
I'm just a beginner in PHP and SQL; I'm just looking for a simple answer.
Security is not at all a concern here; this is just a rough demo.
If this is marked as duplicate, do help redirect me to a link where I can get the solution. Thanks!
Related
underneath is a small script which shows if someone has a birthday today.
The only thing what i miss is a line under the name with the text "Happy birthday" It should only appear, ofcourse is someone has a birthday. how can i achieve this?
<html>
<head>
<title>Vandaag Jarig:</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "****");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("my_site_db");
$results = mysql_query
("SELECT * FROM aevinew2_verjaardagen WHERE DAY(geboortedatum) = DAY (CURDATE ()) AND MONTH(geboortedatum) = MONTH(CURDATE())");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Naam']?></td>
<td><?php echo $row['Afdeling']?></td>
</tr>
<?php
}
}else{
echo "Helaas geen taart vandaag, er is niemand jarig";
}
?>
</tbody>
</table>
</body>
</html>
Something like this? You had syntax error in your code aswell. Missing ; delimiter at end of echo command.
<td><?php echo $row['Naam'].'<br/>Happy Birthday!'; ?></td>
<td><?php echo $row['Afdeling']; ?></td>
Replace line 22 ,23 with these lines. You are missing ';' after echo statement.
<td><?php echo $row['Naam'].'<br/>Happy Birthday!'; ?></td>
<td><?php echo $row['Afdeling']; ?></td>
well, the script is working (it was already), but the only thing i need is an echo under the current echo which is just showing some text.
If i do that it letterly puts that kind of text on top include the text Echo
I have an activity table that contain the list of activity student had joined before. So if the student is new student, there will have been no activity for that student.
<table align="center" width="1000" border="1" >
<h3>Activity List</h3>
</br>
<tr align="center" style="font-weight:bold" >
<td>ID</td>
<td>Activity</td>
<td>Sem</td>
<td>Session</td>
<td>Achievement</td>
<td>Level</td>
</tr>
<?php do { ?>
<tr align="center">
<td><?php echo $row_Recordset1['student_id']; ?></td>
<td><?php echo $row_Recordset1['activity']; ?></td>
<td><?php echo $row_Recordset1['sem']; ?></td>
<td><?php echo $row_Recordset1['session']; ?></td>
<td><?php echo $row_Recordset1['achievement']; ?></td>
<td><?php echo $row_Recordset1['level']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
How can I make this table only show on screen if it is not empty. Btw, im using session to display the exist record.
The best way to do this would be to get your array via mysql(i)_fetch_array() of the query you have build and then to chec to see if the query has rows like:
$qry = "SELECT `this` FROM `table`"
while ($result = mysql_fetch_array($qry)) {
if (mysql_num_rows($result) > 0) {
echo "We have rows!";
}
else {
echo "Looks like we haven't got anything here!";
}
}
I hope this helps.
Might also help to look here: PHP mysql_num_rows method.
<?php if(!empty($activity))
{
your msg ..
}
else
{
}
?>
where empty() will check a given variable is empty or not
Well that's what the if statement is used for:
if(!count($activities)) {
echo "This student has no activities yet.";
} else {
//display activities
....
}
I have a simple issue I cannot figure out. I am trying to get the id of the record setup as a link to go to a second page that updates the record. I have the update working when I click on update it takes me to the record. I want the id to do the same.
<html>
<?php
require_once("../db_connect.php");
$stmt = $db->prepare("SELECT * FROM Users");
$stmt->execute();
?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table bgcolor=F2F2F2 width=1080 border='2'table-layout: fixed >
<br>
<tr>
<th>Id</th>
<th>Update</th>
<th>First Name</th>
<th>Last name</th>
<th>Address</th>
<th>Bio</th>
</tr>
<tr>
<?php echo "<td>
<a href='../update.php?id=" . $row['id'] . "'>ID</a></td>"?>
<?php echo "<td>
<a href='../update.php?id=" . $row['id'] . "'>Update</a></td>"?>
<td><?php echo $row['First Name']; ?></td>
<td><?php echo $row['Last Name']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Bio']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
In general, it is a good practice to put duplicated content into a function or variable and then call it when needed, to improve code readability & to save time/space.
I have also noticed many people struggling with new syntax so I have split the "one-liners" and left comments explaining how does new syntax works.
function col_gen($slug,$id=''){
return (!empty($id))? // If ID parameter exist and not empty generate column with a link
'<td>'.$slug.'</td>': //else
'<td>'.$slug.'</td>';
}
And then, in your case, you can run this function inside a loop:
....
foreach($row as $k=>$slug){
echo ($k==='id')? //if key equals "id"
col_gen($slug,$slug) // Output column for ID
.col_gen('Update',$slug) // Output column for keyword Update
:col_gen($slug); //else output just column with the slug
/**
* Learn one-line PHP, you will love it, once you understand it...
* Full Example Above:
* echo ($k==='id') ? col_gen($slug,$slug).col_gen('Update',$slug):col_gen($slug);
**/
}
This is my first posting in SO, my apologize if I opened an existing question. As I couldn't find the result in Google. Sorry to said but I'm still fresh in PHP PDO and in learning stage.
Back to my question, currently I'm building a customer visit logs from my wife but I'm stuck with the result. I have two table which one stores the customer information and another table store the visit details. I uploaded the test table at here: SQL Fiddle
And below is my current coding and I'm using PHP PDO while
<?php
require_once 'dbconnect.php';
$p_id = $_GET['name'];
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT customer.fname, customer.lname, customer.gender, services.treatment, services.date
FROM customer LEFT JOIN services
ON customer.id = services.customer_id
WHERE customer.slug LIKE :id";
$q = $conn->prepare($sql);
$q->execute(array('id' => $p_id));
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="container">
<h1>Customer Record</h1>
Name: <br />
Gender: <br />
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Gender</th>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['fname']), ' ', htmlspecialchars($r['lname'])?></td>
<td><?php echo htmlspecialchars($r['gender']); ?></td>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
Seach Again
</body>
</div>
</html>
And I achieve as what SQL Fiddle result, but what I wanted is, the name and gender is not keep repeating.
I attached together with the screenshot:
Screenshot
What I want is as per the screenshot image, Name: John Doe and Gender: Male, should be on top and not keep on repeating while the table below show all the visit details. I tried to modified the code but it seems it don't really work out.
Please advise me as I'm really out of idea how to achieve what I want.
Thank you so much.
Since you do a LEFT JOIN in your SQL query, you know ahead of time that all of the fname, lname and gender values returned by $q->fetch() are going to be for the same customer.slug, right? So you can count on that.
My suggestion would be to instead use the fetchAll() function to get an array of all records for customer.slug, and then render that in your view. For example (haven't tested this) you could add the following after $q->setFetchMode(PDO::FETCH_ASSOC); ...
$cs = $q->fetchAll(); // customer services join
Then, in your <html> view, you could do something like the following:
<h1>Customer Record</h1>
Name: <?php echo htmlspecialchars($cs[0]['fname'].' '.$cs[0]['lname']); ?> <br />
Gender: <?php echo htmlspecialchars($cs[0]['gender']); ?> <br />
<table>
<thead>
<tr>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach($cs as $r): ?>
<tr>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Of course, it might also be a good idea to check to see that any records were returned by your query and display a "not found" message if not. After all, $cs[0] might be empty, giving you a PHP error.
I currently have made a page that shows all the users, but I just want to only display the currently logged in user, Please help me im begging you I spent 4 hours of nothing T_T im new at php and im very lost, im so hopeless right now
<?PHP
$customerquery=mysql_query("select * from customerinfo");
while($customerrows=mysql_fetch_array($customerquery)){
?>
<tr>
<td>Id</td><td>First Name</td><td>Last Name</td><td>Address</td><td>Contact No</td> <td>Username</td><td>Password</td><td>Edit</td>
</tr>
<tr>
<td><?PHP echo $customerrows['id'];?></td>
<td><?PHP echo $customerrows['fname'];?></td>
<td><?PHP echo $customerrows['lname'];?></td>
<td><?PHP echo $customerrows['address'];?></td>
<td><?PHP echo $customerrows['contactno'];?></td>
<td><?PHP echo $customerrows['username'];?></td>
<td><?PHP echo $customerrows['password'];?></td>
<td>edit</td>
</tr>
<?PHP } ?>
You need to specify a WHERE condition in your query. That will fetch only one row, so you will not need a while loop:
$id = (int) $_GET['id']; // assuming you pass user id in URL
$customerquery = mysql_query("select * from customerinfo WHERE id = $id");
$customerrows = mysql_fetch_array($customerquery);
// rest of your html