I've been reading about MySQL Foreign Keys and using Parent/Child relationship tables.
Below is the table relationship (phpmyadmin). Table "dpuchanges" has a foreign key (PARENT) relationship with table "opendpu", column (ECRNUM).
For some reason I am not pulling any data (see php code below). I'm thinking its my SQL statement, but I cannot figure out where it went wrong. I'm not receiving any PHP errors. Can anyone help?
<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
<?php
require ('config.php');
$db = null;
$limit = 10;
$counter = 0;
while (true) {
try {
$db = new PDO($dsn, $uname, $pword);
$db->exec( "SET CHARACTER SET utf8" );
$db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
$db->setAttribute( PDO::ATTR_PERSISTENT, true );
break;
}
catch (Exception $e) {
$db = null;
$counter++;
if ($counter == $limit)
throw $e;
}
}
$stmt = $db->prepare("SELECT ACTION, PARTNO, ACTIONTXT, REV_WAS, REV_NOW, QTY_FROM, QTY_TO FROM dpuchanges JOIN opendpu ON opendpu.ECRNUM = dpuchanges.PARENT WHERE opendpu.ECRNUM = 82095");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$action=$row["ACTION"];
$partno=$row["PARTNO"];
$actiontxt=$row["ACTIONTXT"];
$rev_was=$row["REV_WAS"];
$rev_now=$row["REV_NOW"];
$qty_from=$row["QTY_FROM"];
$qty_to= $row["QTY_TO"];
?>
<tr>
<td>Action = <?php echo $action; ?></td>
<td>PartNo = <?php echo $partno; ?></td>
<td>Description = <?php echo $actiontxt; ?></td>
<td>REV WAS = <?php echo $rev_was; ?></td>
<td>REV NOW = <?php echo $rev_now; ?></td>
<td>QTY FROM = <?php echo $qty_from; ?></td>
<td>QTY TO = <?php echo $qty_to; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Your datatypes and SIZES of the related columns "dpuchanges.parent" and "openpdu.ecrnum" MUST be the same.
Source: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
I think you are missing ;
<td>Action = <?php echo $action; ?></td>
<td>PartNo = <?php echo $partno; ?></td>
<td>Description = <?php echo $actiontxt; ?></td>
<td>REV WAS = <?php echo $rev_was; ?></td>
<td>REV NOW = <?php echo $rev_now; ?></td>
<td>QTY FROM = <?php echo $qty_from; ?></td>
<td>QTY TO = <?php echo $qty_t; ?></td>
Related
I'm trying to display generated data from PHPMyAdmin into a table using MySQLi and I can't seem to figure it out.
<tr>
<th scope="col">Link</th>
<th scope="col">Category</th>
</tr>
<?php
//Connection Information
$connection = mysqli_connect('localhost','root',''); //establish connection to db
$selected = mysqli_select_db($connection, 'sample'); //select db
//SQLi Statements
$viewQuery = "select * from link JOIN categories";
$execute = mysqli_query($connection,$viewQuery);
if($execute)
{
while($row = mysqli_fetch_array($execute))
{
$link = $row['link'];
$category = $row['category'];
}
}
?>
<tr>
<td><?php echo $link; ?></td>
<td><?php echo $category; ?></td>
</tr>
</table>
Is there something I'm missing? I'm new to MySQLi
You are overwriting the variables without using it. It should be like this i guess
while($row = mysqli_fetch_array($execute)){
$link = $row['link'];
$category = $row['category'];
?>
<tr>
<td><?php echo $link; ?></td>
<td><?php echo $category; ?></td>
</tr>
<?php
}
I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,
This page isn’t working.
localhost is currently unable to handle this request.
Here is my code,
<html>
<body>
<table style="width:100%">
<tr>
<th>Driverid</th>
<th>Truckid</th>
<th>Imagecount</th>
<th>Trainingstatus</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
</table>
<?php}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
please replace your code
from
driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];
to
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];
please reader the section about how to define variables in php on the PHP Manual ,after that, check the variables defined in your code.
you have not started table tag, also not in loop, and main part is sdd space <?php} to <?php }
if ($result->num_rows > 0) {
echo '<table>';
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
<?php }
echo '</table>';
} else {
echo "0 results";
}
hey guys i've been trying to insert an bunch of data from array and variable into database using button , but it can't be inserted and the database still empty , is there anyone who know where is my mistake ?
here is my code
<?php
include 'koneksi.php';
$arr_nf = array(1,0.334,-0.334,-1);
$arr_ef = array(0,0.333,0.667,1);
$arr_lf = array(1,0.667,0.333,0);
$kelas = "80 keatas" ;
$iduser = "8" ;
$query = mysql_query(" select id_alternatif from tb_alternatif where kelas = '".$kelas."' order by nama "); // run query
$idalter = array();
while($row = mysql_fetch_assoc($query)){
$idalter[] = $row;
}
////////////////////////////////////////// RANGKING LF
foreach ($arr_lf as $val){ // net flow kali 1000
$arr_lfx[]= $val * 1000 ;
}
$int_lfx = array_map( // rubah jd integer
function($value) { return (int)$value; },
$arr_lfx );
$lfx_copy = $int_lfx;
rsort($lfx_copy);
$lfx_copy = array_flip($lfx_copy);
foreach($int_lfx as $val){
$rangkinglf[] = ($lfx_copy[$val]+1);
}
//////////////////////////////////////////
$jumlah_atlet = count($arr_nf);
if (isset($_POST['submit'])){
for($x=0; $x<$jumlah_atlet ;$x++){
mysql_query(" INSERT INTO tb_histori(id_histori, id_user, id_alternatif, kelas, nilai, rangking, waktu) values(NULL, '$iduser', '".$idalter[$x]['id_alternatif']."', '$kelas', '$arr_nf[$x]', '$rangkingnf[$x]', now());") or die(mysql_error()); };
};
print_r($jumlah_atlet);
?>
<html>
<head>
<title> insert data array using only button </title>
</head>
<body>
<form action="" method="post">
<input name="submit" type="button" value="Print & Save" onClick="" />
</form>
</body>
<?php
$data = mysql_query("select * from tb_histori");
$no = 1;
while($d = mysql_fetch_array($data)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['id_user']; ?></td>
<td><?php echo $d['id_alternatif']; ?></td>
<td><?php echo $d['kelas']; ?></td>
<td><?php echo $d['nilai']; ?></td>
<td><?php echo $d['rangking']; ?></td>
<td><?php echo $d['waktu']; ?></td>
</tr>
<br>
<?php } ?>
<br>
</html>
I am trying to perform a search action by getting a value from HTML form using PDO and display the results in the same page. I am getting no errors also no results. Please advise! i am beginner.
<?php
require_once 'db_alternate2.php';
session_start();
if (isset($_POST['submit'])) {
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$payrl_search = trim($_POST['empname_search']);
$sql = "SELECT * FROM payroll_db
WHERE 'pay_staff_name'
LIKE '$%payrl_search%'";
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
}
my php code inside html is
<table>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['pay_emp_id'])?></td>
<td><?php echo htmlspecialchars($r['pay_staff_name']); ?></td>
<td><?php echo htmlspecialchars($r['pay_month_sal']); ?></td>
<td><?php echo htmlspecialchars($r['pay_amount']); ?></td>
<td><?php echo htmlspecialchars($r['pay_bankname']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
I am getting these errors:
Undefined variable: q
Fatal error: Call to a member function fetch() on a non-object
i think my error part is
<?php while ($r = $q->fetch()): ?>
I am not looking for alternating rows... Rather, I have two tables A and B and Field1 is present in both. I want to present TableA and everywhere that the field1 matches the field1 in TableB, I would like to highlight the row. How can I do this?
I know how to do this in SQL, but the question is how to present this in the browser. I think I must use javascript. Any advice?
Here is the PHP Script that generates Table A. I would like to change the background of rows in this output where the aif_id is also present in Table B
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif ORDER BY aif_id DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table id=\"all_aifs\">";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
PHP Script for TableB
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining LIMIT 0,50");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table>";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
UPDATED CODE
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining");
$result->setFetchMode(PDO::FETCH_ASSOC);
$match = $dbh->query("SELECT a_aif.aif_id FROM a_aif INNER JOIN a_aif_remaining WHERE a_aif_remaining.aif_id = a_aif.aif_id");?>
<?php if ( !empty($result) ) : ?>
<table id="all_aifs">
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr>
<?= $row['aif_id'] == true ? ' class="match"' : '' ?>
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td>Placeholder</td>
</tr>
<?php endforeach; ?>
</table>
<br>
<?php endif;
$dbh = NULL;
?>
Perform your check in SQL (since you know how to do so).
In PHP use the result of your check to add an (additional) class-attribute to the HTML element that needs the highlight (just like 'alternating rows' do):
<?php
$query = ... // perform your SQL query
// TODO Open HTML table
// TODO Loop though results:
$row = ... // fetch you row here
?>
<tr <?= $row['my_check'] == true ? ' class="match"' : '' ?>>
<td><?= $row['field'] ?></td>
</tr>
<?php
// TODO close HTML table
This (incomplete) example uses the SQL check (named my_check) and adds the match class when required. In CSS you can use this match class to apply highlighting:
.match {
background-color: red;
}
First of all, if you are going to keep your code maintainable and clean as well, then you should avoid printing HTML TAGS.
The way not to go:
echo "<table>";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
The way to go:
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining LIMIT 0,50");
$result->setFetchMode(PDO::FETCH_ASSOC);
?>
<?php if ( !empty($result) ) : ?>
<table>
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr>
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td>Placeholder</td>
</tr>
<?php endforeach; ?>
</table>
<br />
<?php endif; ?>
Conclusion:
1) The code now looks more readable and clean as well
2) You separated PHP from HTML
3) You can easily determine a problem once it occurs (like, parse error at .., unexcepted ';' ...
Back to your question
So how can you highlight a row you need? Well it's easy, just use if/else in a foreach loop, like
<?php foreach($result as $k => $v): ?>
<?php if ( $k == 'some_value_you_expect_to_be_highlighted' ) : ?>
<tr>... should be highlighted ...</tr>
<?php else : ?>
<tr>.... a regular row</tr>
<?php endif; ?>
<?php endforeach; ?>
You should simply add a class to the rows where the data matches, and set a background color in the associated CSS.
<table> <tr class="matching"> ... </tr>
How to write the appropriate CSS is beyond the scope of this answer. ;-)
Whether that class is added by your web server's PHP code, or by some JavaScript in the client, depends on the design of your Web application.
No, you don't 'have' to use JavaScript. You can use PHP.
When generating your rows, check whether the condition is met and put your 'highlighted' class into the row definition.
Pseudocode:
<?php
if(Condition) echo "<tr class='highlighted'>";
else echo "<tr>";
echo "<td>CellStuff</td>";
echo "</tr>";
?>
I'm not sure if I could understand your problem correct but I dont think you need JS.
You can calculate if a cell/row should be highlighted in PHP and set a css class, also in PHP.