I have a database with three tables "Cantiere "Affidataria "SubAffidataria"
"Cantiere" has a 1 to N relationship with Affidataria "Affidataria" has a 1 to N relationship with subAffidataria
so in Affidataria there is a fk of "Cantiere" called "fkIdCantiere" and in subAffidataria there is a fk of "Affidataria" called "fkIDAffidataria"
my problem is to go from a "function" to the id of fkIdAffidataria
in fact, if you notice in the screenshot you see the carers but not the sub-delegates. so I have to make sure to save the fkIdAffidataria to put it in the QUERY ..
enter image description here
"0 results" indicates that the query did not find any subAffidataria .. when in fact they are in the database
<!DOCTYPE html>
<?php
session_start();
if(!isset($_SESSION["username"]))
{
header('location: ../index.php');
}
else
{
?>
<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$idCantiere = $_GET['idCantiere'];
$sql1 = "SELECT idAffidataria,nomeCantiere,fkIDCantiere
FROM Affidataria
WHERE fkIDCantiere = '$idCantiere'";
echo($idAffidataria);
$idAffidataria = $_GET['idAffidataria'];
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc()) {
echo'<br><br><br>';
echo '<h3>Affidataria</h3>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">' . $row["nomeCantiere"] . '</a>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">Stampa PDF ' . $row["nomeCantiere"] . '</a>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">Esporta Directory Interna ' . $row["nomeCantiere"] . '</a>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">Stampa Personale ' . $row["nomeCantiere"] . '</a>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">Esporta Directory Personale ' . $row["nomeCantiere"] . '</a>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">Stampa Mezzi ' . $row["nomeCantiere"] . '</a>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/affidatario.php?idAffidataria=' . $row["idAffidataria"] . '">Esporta Directory Mezzi ' . $row["nomeCantiere"] . '</a>';
}
echo'<br><br><br>';
} else {
echo "0 results";
}
$conn->close();
?>
//subAffidatario
<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT * FROM SubAffidatario
WHERE fkIdAffidataria = '$idAffidataria'";
echo($idSubAffidatario);
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc()) {
echo'<br><br><br>';
echo '<a class="myButton" href="../../pagineHtml/visualizza/visualizzazione/subAffidatario.php?idSubAffidatario=' . $row["idSubAffidatario"] . '">' . $row["ragioneSocialeSubAffidatario"] . '</a>';
}
echo'<br><br><br>';
echo 'Nuovo Cantiere +';
} else {
echo "0 results";
}
$conn->close();
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Progetto Cantiere</title>
<!-- Bootstrap -->
<link href="../paginaIniziale/css/bootstrap-4.0.0.css" rel="stylesheet">
</head>
<body>
</body>
</html>
<?php } ?>
Related
I seem to be getting Undefined offset 1 error when I hit submit. I have been trying to play with the numbers but nothing seems to help. I added a fourth column right after the commented last name. Before the addition the code worked. I realized later on that I needed that third column. Ever since then I get an error and can not update the sql table.
Thank you in advance,
Avi
<!DOCTYPE html>
<?php
echo '<link rel="stylesheet" type="text/css" href="css/newStyle.css"></head>';
session_start();
if (isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn'] == true) && $_SESSION['admin'] == true) {
echo "<br><h3>Welcome to the administrative area Prof. " . $_SESSION['firstname'] . "!</h3><br><br>";
} else {
//echo "<br>Please log in first to see this page.";
header ('Location: index.php');
}
require_once 'login.php';
$connection = new mysqli($hn,$un,$pw,$db);
if($connection->connect_error) die($connection->connect_error);
if(isset($_POST['submit'])){
for($i = 0; $i < $_POST['totalGrades']; $i++){
echo $i . ': ' . $_POST['grade' .$i] . '<br>';
$parts = explode("|", $_POST['grade' .$i]);
$newGrade = "UPDATE Grades SET grade = '" . $parts[1] . "' WHERE gradeID = " .$parts[0];
$result = $connection->query($newGrade);
}
}
$username = "";
$courseId = "";
$grade = "";
$courseName = "";
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<form method= "post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<link rel="stylesheet" href="css/style.css">
<?php
// $courseSct =
"SELECT username, courseName, grade, gradeID FROM Courses\n"
// . "JOIN Grades\n"
// . "ON courses.courseId = Grades.courseId";
$courseSct =
"SELECT u.firstname, u.lastname, c.courseName, g.grade "
. " FROM Grades g "
. " INNER JOIN Courses c ON c.courseID = g.courseID "
. " INNER JOIN Users u ON u.userID = g.userID "
. " WHERE c.professorID = " .$_SESSION['userID'];
$result = $connection->query($courseSct);
$rows = $result->num_rows;
echo
"<table border = '1' width = '50%'>"
. "<caption><h2>Grades Table</h2></caption>"
. "<tr>"
. '<th>First Name</th>'
. "<th>Last Name</th>"
. "<th>Course Name</th>"
. "<th>Grade</th>"
//. "<th>New Value</th>"
. "</tr>";
for($j = 0; $j < $rows; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo
"<tr>" .
"<td>" . $row[0] . "</td>" . //First Name
"<td>" . $row[1] . "</td>" . //Last Name
"<td>" . $row[2] . "</td>";
"<td>"; //Grade
echo '<select name="grade' . $j . '" size="1" id="' . $row[3] . '">';
echo '<option value="select">Select</option>';
$letterGrade = 'A';
for($x = 0; $x < 6; $x++) {
echo '<option value="' . $row[3] . '|' . $letterGrade . '"';
if($letterGrade == $row[3]) {
echo ' selected';
}
echo '>' . $letterGrade++ . '</option>';
}
echo '</select><br>'. "</td>" . "</tr>";
}
echo "</table>";
?>
<input type="hidden" name="totalGrades" value="<?php echo $rows;?>">
<br>
<input type="submit" name="submit" value="Submit">
<br>
</form>
<a href='index.php?logout'><br>click here to log out<br></a>
</body>
</html>
Have you tried a foreach instead?
if(isset($_POST['submit'])){
foreach($_POST['totalGrades'] as $key => $value) {
echo $key . ': ' . $value . '<br>';
$parts = explode("|", $value);
$newGrade = "UPDATE Grades SET grade = '" . $parts[1]
. "' WHERE gradeID = " .$parts[0];
$result = $connection->query($newGrade);
}
}
I have a system where users can send and receive messages and I need to make a way for them to directly reply to the messages.
This is a test code to make the page echo what should be retrieved and set as a session, but it is not echoing anything.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Clan Kaos ● View Post</title>
<link rel="stylesheet" type="text/css" href="include/style/content.css" />
</head>
<body>
<?php
include ("include/header.html");
include ("include/sidebar.html");
include ("include/testpost.html");
?>
<div class="container">
<?php session_start();
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT username FROM kaoscraft_user WHERE user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$username1 = $row['username'];
// Grab the profile data from the database
$query = "SELECT `to`, `from`, rank, gender, picture, title, msg FROM kaoscraft_pm WHERE `to` = '$username1' ORDER BY msg_id DESC";
$data = mysqli_query($dbc, $query);
$gender = $row['gender'];
$username2 = $_SESSION['reply'];
while ($row = mysqli_fetch_array($data)) {
$_SESSION['reply'] = $row['from'];
echo '<div class="viewpost">';
echo '<div class="vpside">';
if(!empty($row['picture'])) {
echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
}
if(!empty($row['from'])) {
echo '<p>From:<br />' . $row['from'] . '</p>';
echo '<p> ' . $username2 . '</p>';
}
if(!empty($row['rank'])) {
echo '<p>Rank:<br />' . $row['rank'] . '</p>';
}
if(!empty($row['gender'])){
echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
}
echo '</div>';
if(!empty($row['title'])) {
echo'<h4><u>' .$row['title']. '</u></h4>';
}
if(!empty($row['msg'])) {
echo '<p class="">' . $row['msg'] . '</p>';
}
echo '<div class="sig">';
if(!empty($row['bio'])) {
echo '<p>' . $row['bio'] . '</p>';
}
echo '</div>';
echo '</div><br />';
}
mysqli_close($dbc);
?>
I need the session $_SESSION['reply'] to be set as the variable $username2 and it does not seem to be working.
Move session_start(); on top of the PHP code. You are accessing $_SESSION['reply'] = $username2; before that which wont work.
What I was doing was incorrect. I had to move
$_SESSION['reply'] = $row['from'];
$username2 = $_SESSION['reply'];`
from after the query to
while ($row = mysqli_fetch_array($data)) {
$_SESSION['reply'] = $row['from'];
$username2 = $_SESSION['reply'];`
and then it echo'd correctly. Thanks for the help from everyone :)
I cant figure out how to display MySql information using Ruby on Rails.
This is the query i want to run
SELECT name, description FROM projects where status > 1
If someone can translate to Rails the code below it would be greatly apreciatted
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Proyects</title>
</head>
<?php
$dsn = "mysql:dbname=redmine_default";
$username = "root";
$password = "";
try {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Error: " . $e->getMessage();
}
function mostrarProyectos($conn) {
$sql = 'SELECT * FROM projects';
foreach ($conn->query($sql) as $row) {
print $row['name'] . "\t";
print $row['id'] . "\t";
print $row['description'] . "\n";
}
}
//creating a table with 4 columns
?>
<table border="1" cellspacing=1 cellpadding=2 style="font-size: 8pt"><tr>
<td><font face="verdana"><b>ID</b></font></td>
<td><font face="verdana"><b>Name</b></font></td>
<td><font face="verdana"><b>Status</b></font></td>
<td><font face="verdana"><b>Desc</b></font></td>
</tr>
<?php
//here comes the SQL query
$query = "SELECT name, description FROM projects where status > 1";
$resultado = $conn->query($query);
$numero = 0;
foreach($resultado as $row)
{
echo "<tr><td width=\"25%\"><font face=\"verdana\">" .
$row["id"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["name"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["status"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["description"]. "</font></td></tr>";
$numero++;
}
echo "<tr><td colspan=\"15\"><font face=\"verdana\"><b>Número: " . $numero .
"</b></font></td></tr>";
mysql_free_result($result);
mysql_close($link);
?>
</table
</body>
</html>
The RoR code would be something like this:
Project.where("status > ?", 10)
http://guides.rubyonrails.org/active_record_querying.html
The link will give you a large list of mysql queries and their RoR counterpart.
I have been struggling trying to find a way to split the records I have into two columns using a prepared statement and was wondering if any could provide assistance
Split Records
<?PHP
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';
try {
$dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db = new PDO($dsn, $DB_USER, $DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM `snippets` ORDER BY _ID ASC"; // Split in half here
$prep = $db->prepare($query);
$prep->execute();
echo ' <div class="row">' . "\n"; // Display 1st half here
echo ' <div class="large-12 columns">' . "\n";
echo ' <div class="panel">' . "\n";
while ($row = $prep->fetch()) {
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <b>' . $row['CODE_NAME'] . '</b><br/><br/>' . "\n";
$timestamp = strtotime($row['CODE_TIMESTAMP']);
echo ' Date: ' . date("M d, Y h:m:s A", $timestamp).'<br/><br/>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . '<br/>' . "\n";
}
$db = null;
}
catch (PDOException $e) {
echo '<div class="row">';
echo '<div class="panel">';
echo 'Connection failed: ' . $e->getMessage();
echo '</div>';
echo '</div>';
}
echo ' </div>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . "\n";
// Create a 2nd column here to display second half
?>
I would like to correctly calculate the total number of records and then split it in half and display on half in one column and the other in another which I have figured out I just can't seem to get the records split and then displayed
I managed to get it working by doing the following
<!DOCTYPE html>
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<html>
<?php $title = 'ALL'; $page = 'all';include 'includes/header.php';?>
<body>
<?php include 'includes/nav.php'; ?>
<div class="row">
<div class="small-6 small columns">
<?php
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';
try {
$dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db = new PDO($dsn, $DB_USER, $DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM `snippets` ORDER BY _ID ASC";
$prep = $db->prepare($query);
$prep->execute();
$count = $prep->rowCount();
$half = round($count/2);
$dsn2 = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db2 = new PDO($dsn, $DB_USER, $DB_PASS);
$db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query2 = "SELECT * FROM `snippets` ORDER BY _ID ASC Limit " . $half;
$prep2 = $db2->prepare($query2);
$prep2->execute();
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <div class="panel">' . "\n";
while ($row = $prep2->fetch()) {
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <b>' . $row['CODE_NAME'] . '</b><br/><br/>' . "\n";
$timestamp = strtotime($row['CODE_TIMESTAMP']);
echo ' Date: ' . date("M d, Y h:m:s A", $timestamp).'<br/><br/>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . '<br/>' . "\n";
}
$db = null;
}
catch (PDOException $e) {
echo '<div class="row">';
echo '<div class="panel">';
echo 'Connection failed: ' . $e->getMessage();
echo '</div>';
echo '</div>';
}
echo ' </div>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . "\n";
?>
</div>
<div class="row">
<div class="small-6 small columns">
<?php
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';
try {
$dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db = new PDO($dsn, $DB_USER, $DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM `snippets` ORDER BY _ID ASC";
$prep = $db->prepare($query);
$prep->execute();
$count = $prep->rowCount();
$half = round($count/2);
$dsn2 = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db2 = new PDO($dsn, $DB_USER, $DB_PASS);
$db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query2 = "SELECT * FROM `snippets` ORDER BY _ID ASC Limit " . $half . ',' . $half;
$prep2 = $db2->prepare($query2);
$prep2->execute();
/*
$dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
$db = new PDO($dsn, $DB_USER, $DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM `snippets` ORDER BY _ID ASC LIMIT 20,19";
$prep = $db->prepare($query);
$prep->execute();
*/
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <div class="panel">' . "\n";
while ($row = $prep2->fetch()) {
echo ' <div class="row">' . "\n";
echo ' <div class="large-12 columns">' . "\n";
echo ' <b>' . $row['CODE_NAME'] . '</b><br/><br/>' . "\n";
$timestamp = strtotime($row['CODE_TIMESTAMP']);
echo ' Date: ' . date("M d, Y h:m:s A", $timestamp).'<br/><br/>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . '<br/>' . "\n";
}
$db = null;
}
catch (PDOException $e) {
echo '<div class="row">';
echo '<div class="panel">';
echo 'Connection failed: ' . $e->getMessage();
echo '</div>';
echo '</div>';
}
echo ' </div>' . "\n";
echo ' </div>' . "\n";
echo ' </div>' . "\n";
?>
</div>
</div>
</div>
<?php include 'includes/footer.php';?>
</body>
This code is from O'Reilly Head First PHP&MySQL book. The script doesn't recognize the username and password somehow:
<?php
$username = 'rock';
$password = 'roll';
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)) {
// The user name/password are incorrect so send the authentication headers
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Guitar Wars"');
exit('<h2>Guitar Wars</h2>Sorry, you must enter a valid user name and password to access this page.');
}
?>
And this is the script, from which the authentication file is required:
<?php
require_once('authorize.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guitar Wars - High Scores Administration</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - High Scores Administration</h2>
<p>Below is a list of all Guitar Wars high scores. Use this page to remove scores as needed.</p>
<hr />
<?php
require_once('appvars.php');
require_once('connectvars.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the score data from MySQL
$query = "SELECT * FROM guitarwars ORDER BY score DESC, date ASC";
$data = mysqli_query($dbc, $query);
// Loop through the array of score data, formatting it as HTML
echo '<table>';
echo '<tr><th>Name</th><th>Date</th><th>Score</th><th>Action</th></tr>';
while ($row = mysqli_fetch_array($data)) {
// Display the score data
echo '<tr class="scorerow"><td><strong>' . $row['name'] . '</strong></td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['score'] . '</td>';
echo '<td><a href="removescore.php?id=' . $row['id'] . '&date=' . $row['date'] .
'&name=' . $row['name'] . '&score=' . $row['score'] .
'&screenshot=' . $row['screenshot'] . '">Remove</a>';
if ($row['approved'] == '0') {
echo ' / <a href="approvescore.php?id=' . $row['id'] . '&date=' . $row['date'] .
'&name=' . $row['name'] . '&score=' . $row['score'] . '&screenshot=' .
$row['screenshot'] . '">Approve</a>';
}
echo '</td></tr>';
}
echo '</table>';
mysqli_close($dbc);
?>
</body>
</html>
Can you see what's wrong here?
Thank you!
The authentication code is perfectly fine.
You might have extra spaces (before <?php) or something else there before the header() calls.. in that case you don't see the authentication popup.