I have some php querying and printing the info of a mysql table on a medical recruitment website.
Sometimes the users enter, causing line breaks where I would rather not have them, because
it affects the layout of the rendered HTML table in a way that it shouldn't.
I can trim the strings when they fill in the forms and the data get written into the table, but there's already a lot of data there, so i decided to remove the line breaks on the page
where php reads and prints the results.
i declared one of the strings where this causes trouble as such:
$specialtr = trim($special, "\n");
and then where the html table gets rendered:
echo '<td width="150">' . $specialtr . '</td>';
Here is the whole code:
All the code works, it's only the parts above that I added that now causes the column with $specialtr variable not to print/print as empty.
<form action="selected.php" method="POST">
<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">
<tr>
<td>
<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error($conn));
mysql_select_db($dbname) or die(mysql_error($conn));
$specialtr = trim($special, "\n");
echo '<table border="1" cellpadding="2" cellspacing="2" style="margin-left: auto; margin-right: auto;">';
$query = 'SELECT userid FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
$num_entries = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<tr>';
echo '<td><input type="radio" name="employee" value="' . $row['userid'] . '"/></td>';
echo '<td>Select</td>';
}
}
echo '</tr>';
echo '</table>';
?>
</td>
<td>
<?php
echo '<table border="1" cellpadding="2" cellspacing="2" style="width: 98%; margin-left: auto; margin-right: auto;">';
echo '<tr>';
$query = 'SELECT userid, name, surname, sex, age, nationality, email, telnr, special FROM employee
ORDER BY userid ASC';
$result = mysql_query($query, $conn) or die(mysql_error($conn));
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo '<td>' . $userid . '</td>';
echo '<td>' . $name . '</td>';
echo '<td>' . $surname . '</td>';
echo '<td>' . $sex . '</td>';
echo '<td>' . $age . '</td>';
echo '<td>' . $nationality . '</td>';
echo '<td>' . $email . '</td>';
echo '<td>' . $telnr . '</td>';
echo '<td width="150">' . $specialtr . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</td>
</tr>
</table>
<br/>
<br/>
<input type="submit" name="go" value="Go!" />
</form>
The code worked well enough, but since I added the trim, that entire column (the $special column disappears).
I hope maybe someone can tell me why it disappears or if my syntax has a little mistake.
Using extract() like that is a horrible thing. The function should be avoided as a general rule.
Also note that you're doing your trim() call BEFORE $special is defined, so your $specialtr variable is going to be an empty string. Calling the function before you start fetching your database results and extracting a result row into $special will not magically apply trim() to reach row you've fetched. If you want the results to be trimmed as you fetch them, then you have to apply the trim AFTER you do the fetch:
while ($row = mysql_fetch_assoc($result)) {
echo '<td>' . $row['userid'] . '</td>';
...
echo '<td width="150">' . trim($row['special']) . '</td>';
^^^^^^^^^^^^^^^^^^^^^
}
Related
I am trying to create a table that will display the new suggested products by users to allow a moderator to to choose which get approved. For now, all I am trying to do is show the image, as well as the user that submitted it and some other info. I know my problem is with the bottom echo statement that tries to put together a working url by combining the file location and the name of the file itself from the database, which is stored like "Roots.jpg" or like "brock.jpg"
Thanks for any help. I am pretty new to php.
<html>
<title>Forum Approval</title>
<body>
<?php
$db_host = "host";
$db_username = "user";
$db_pass = "pass";
$db_name = "name";
$pdo = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT * FROM add_new_product";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<table border='1' align='center'>
<caption>Products to approve</caption>
<tr>
<th>Product Id</th>
<th>User Id</th>
<th>Name</th>
<th>Company</th>
<th>Image</th>
</tr>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>' . $row['new_prod_ID'] . '</td>';
echo '<td>' . $row['user_ID'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['company'] . '</td>';
echo "<td><img src = "a link/uploads/ . $row['image']" . width='100'></td>";
echo '</tr>';
}
?>
</table>
</body>
</html>
Try changing the echo to something like...
echo "<td><img src=\"path/to/directory/".$row['image']."\" width='100'></td>
The src would be path/to/directory/Roots.jpg
I've seen similar questions on here but the code is different when I try to implement it. I'm using php and html to connect to my db2 database, pull data from a table and display the pulled data in an html table. I'm trying to add functionality to my 'delete' button and would like to be able to mark a check-box that would highlight the selected row in the table. I'm working on my delete.php but for now I would like to implement this row selection. I found a line of code that I put at the start of my table, but how do I highlight the row? Here's what I have so far:
<html>
<head><title>DB Testing</title></head>
<style>
table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 80%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
<body>
<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";
//create connection
$conn = db2_connect($database, $user, $password);
//check connection
if($conn) {
//echo "DB2 Connection succeeded.<br><br>";
} else{
exit("failed".db2_conn_errormsg());
}
//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);
//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';
echo'<th></th>';
echo'<th>A</th>';
echo'<th>B</th>';
echo'<th>START_TIME</th>';
echo'<th>END_TIME</th>';
echo'<th>START_DAY</th>';
echo'<th>END_DAY</th>';
echo'<th>C</th>';
echo'<th>ID</th>';
echo'<th>BUSINESS_AREA</th>';
echo'<th>ENVIRONMENT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = db2_fetch_assoc($stmt)) {
echo '<tr>';
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" /></td>";
echo '<td>' . $row['A'] . '</td>';
echo '<td>' . $row['B'] . '</td>';
echo '<td>' . $row['START_TIME'] . '</td>';
echo '<td>' . $row['END_TIME'] . '</td>';
echo '<td>' . $row['START_DAY'] . '</td>';
echo '<td>' . $row['END_DAY'] . '</td>';
echo '<td>' . $row['C'] . '</td>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
echo '<td>' . $row['ENVIRONMENT'] . '</td>';
echo '</tr>';
echo '</tbody>';
}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);
?>
<?php
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
?>
</body>
</html>
You'll need some javascript to do the trick, see the snippet:
function hl(ob){
if(ob.checked == true){
ob.parentNode.parentNode.style.backgroundColor='red';
}
if(ob.checked != true){
ob.parentNode.parentNode.style.backgroundColor='white';
}
}
<table border=1 cellspacing=0 cellpadding=5>
<tr>
<td><input type="checkbox" name="checkbox" value="1" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" value="2" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox" value="3" onClick="hl(this)" /></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
EDIT
<html>
<head>
<title>DB Testing</title>
<script type="text/javascript">
function hl(ob){
if(ob.checked == true){
ob.parentNode.parentNode.style.backgroundColor='red';
}
if(ob.checked != true){
ob.parentNode.parentNode.style.backgroundColor='white';
}
}
</script>
<style>
table{
font-family: arial, sans-serif;
border-collapse: collapse;
width: 80%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
</head>
<body>
<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";
//create connection
$conn = db2_connect($database, $user, $password);
//check connection
if($conn) {
//echo "DB2 Connection succeeded.<br><br>";
} else{
exit("failed".db2_conn_errormsg());
}
//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);
//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';
echo'<th></th>';
echo'<th>A</th>';
echo'<th>B</th>';
echo'<th>START_TIME</th>';
echo'<th>END_TIME</th>';
echo'<th>START_DAY</th>';
echo'<th>END_DAY</th>';
echo'<th>C</th>';
echo'<th>ID</th>';
echo'<th>BUSINESS_AREA</th>';
echo'<th>ENVIRONMENT</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = db2_fetch_assoc($stmt)) {
echo '<tr>';
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" onClick=\"hl(this)\" /></td>";
echo '<td>' . $row['A'] . '</td>';
echo '<td>' . $row['B'] . '</td>';
echo '<td>' . $row['START_TIME'] . '</td>';
echo '<td>' . $row['END_TIME'] . '</td>';
echo '<td>' . $row['START_DAY'] . '</td>';
echo '<td>' . $row['END_DAY'] . '</td>';
echo '<td>' . $row['C'] . '</td>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
echo '<td>' . $row['ENVIRONMENT'] . '</td>';
echo '</tr>';
echo '</tbody>';
}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);
?>
<?php
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
?>
</body>
</html>
I am trying to update each row with a value of 1 if the checkbox is checked and do nothing if not.
$query= "SELECT Name, Surname, Age, Club, age_group, School, team_select FROM players WHERE Age < 9";
$statement = $db->prepare($query);
$statement->execute();
$players = $statement->fetchAll(PDO::FETCH_ASSOC);
echo '<form nethod="POST" action="add_to_team.php">';
echo '<p align="center"><a href="new.php" >Add Player</a></p>';
echo '<table class="table table-bordered"';
echo '<tr><th>Name</th>
<th>Surname</th>
<th>Club</th>
<th>Age Group</th>
<th>School</th>
<th>Edit</th>
<th>Delete</th>
<th>Add to Team</th></tr>';
// loop through results of database query, displaying them in the table
foreach ($players as $player) {
// echo out the contents of each row into a table
echo '<tr>';
echo '<td>' . $player['Name'] . '</td>';
echo '<td>' . $player['Surname'] . '</td>';
echo '<td>' . $player['Club'] . '</td>';
echo '<td>' . $player['age_group'] . '</td>';
echo '<td>' . $player['School'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo '<td><input type="checkbox" name="team_select" value="1"> </td>';
echo '</tr>';
}
echo '</table>';
echo'<input type="submit" value="Submit" name="submit"><br/>';
Code (It just won't work)
<?php
include 'connect.php';//database connection
isset($_POST['team_select'])
?>
You need to give the checkboxes different names. If they're all named team_select there will just be one $_POST['team_select'], but you won't be able to tell which checkboxes were checked.
Use name="team_select[]" and they'll all be put into an array. Then you can put the player name into the value, so the array will contain the names of all the players who should be added.
echo '<td><input type="checkbox" name="team_select[]" value="' . $player['Name'] . '"> </td>';
When you're processing the form, you can do:
foreach ($_POST['team_select'] as $name) {
...
}
to process all the players who were selected.
I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.
In PHP MYSQL_FETCH_ASSOC is omitting Last Row. This never happened. But this time it put me into soup at the last moment.
Even I've put up mysql_num_rows the result is 14 records -- but on list it shows only 13 records and the 14th record is omitted.
Any kind of help is Appreciated.
$uno = $_GET["uno"];
$xtc1 = 'select * from rform where uno="' . $uno . '" order by rno DESC';
$xtc = mysql_query($xtc1) or die('User Reservation Retrival Error : ' . mysql_error());
$trno = mysql_fetch_assoc($xtc);
$trow = mysql_num_rows($xtc);
echo '<p>List of Onlilne Reservations made by <strong style="font-weight:bold; color:red;">' . ucwords($trno["cname"]) . ' (' . $trow . ')</strong></p>';
echo '<table cellpadding="5" cellspacing="0" border="1">';
echo '<tr>';
echo '<td colspan="5" style=" font-size:14px; text-align:center; font-weight:bold; color:red;">' . ucwords($trno["cname"]) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>R.NO</th>';
echo '<th>From</th>';
echo '<th>To</th>';
echo '<th>Date & Time of<Br>Travel</th>';
echo '<th>Reserved On</th>';
echo '</tr>';
while($mtn = mysql_fetch_assoc($xtc)){
$dt = $mtn["csdate"] . ' ' . $mtn["ctime"];
echo '<tr>';
echo '<td>' . $mtn["rno"] . '</td>';
echo '<td>' . $dt . '</td>';
echo '<td>' . $mtn["caddr"] . '</td>';
echo '<td>' . $mtn["cdest"] . '</td>';
echo '<td>' . date('d-M-Y',strtotime($mtn["tstamp"])) . '</td>';
echo '</tr>';
}
echo '</table>';
You have an extra $trno = mysql_fetch_assoc($xtc) that you sem to be discarding. This is your missing row. Just remove that line.
deleting the first $trno = mysql_fetch_assoc($xtc); will solve this problem.
In case you need to read the first line of $xtc before the loop. You can change while loop to do-while, without deleted the first $mtn = mysql_fetch_assoc($xtc);
do{
//whatever
}while($mtn = mysql_fetch_assoc($xtc));