Displaying table and data in database doesnt show - php

i have this problem that my table doesnt show and all info in my database doesnt show also can you help me? the output only shows "Student No. First Name Middle Name Last Name Subject Code Units Final
..." . my info in database doesnt show and the table I created didnt show also . Any answers how to fix this code? thankss
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM student_grades WHERE Student_ID='c2011-02529' ";
$result = $conn->query($sql);
echo "<table>";
echo "<tr><th>Student No.</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Subject Code</th><th>Units</th><th>Final Grade</th><th>Remarks</th><th>Professor</th></tr>";
while($row = mysqli_fetch_array($result)) {
$Student_ID = $row['Student_ID'];
$First_Name = $row['First_Name'];
$Middle_Name = $row['Middle_Name'];
$Last_Name = $row['Last_Name'];
$Subject_Code = $row['Subject_Code'];
$Units = $row['Units'];
$Final_Grade = $row['Final_Grade'];
$Remarks = $row['Remarks'];
$Professor = $row['Professor'];
echo "<tr><td>".$Student_ID."</td><td>".$First_Name."</td><td>".$Middle_Name."</td><td>".$Last_Name."</td><td>".$Subject_Code."</td><td>".$Units."</td><td>".$Final_Grade."</td><td>".$Remarks."</td><td>".$Professor."</td></tr>";
}
echo "</table>";
$conn->close();
?>
this is<table border='1' cellpadding='2' cellspacing ='2'><tr><td>Student No.</td><td>First Name</td><td>Middle Name</td><td>Last Name</td><td>Subject Code</td><td>Units</td><td>Final</td></tr></table>... </div>
thats the only code in that page only

Related

PHP checkbox problem (row not getting deleted)

I made a simple HTML web page with a list of emplyees (only two atm).
<form method="post" action = "del.php">
<table border = "1">
<tr>
<th>Employee Name</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lavoratori";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT nome, id FROM operai";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo"<tr>";
echo'<td><input type = "checkbox" name = checkbox[]" value = '.$row['id']."<td>".$row['nome']."</td>";
echo"</tr>";
}
echo"</table>";
}
$conn->close();
?>
<input type = "submit" name = "delete" id = "delete" value = "Delete Records">
</form>
This is del.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lavoratori";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['delete'])){
$chkarr = $_POST['checkbox'];
foreach($chkarr as $id){
$sql = "DELETE FROM operai WHERE id = .$id.";
$result = $conn->query($sql);
}
header("Location: /test_purpose/home.php");
}
$conn->close();
?>
Can you guys tell me what's going on? I'm new to PHP.
What i'm trying to do is to delete a specific a row from selecting with a checkbox. And it does not work, it redirects me back to the main page, without deleting anything obviously.
This line is wrong:
$sql = "DELETE FROM operai WHERE id = .$id.";
Replace with:
$sql = "DELETE FROM operai WHERE id = {$id}";
You also need to consider SQL Injection: https://stackoverflow.com/a/60496/1403785

Relate database and PHP through links

I'm doing a code where the main page has titles of some exercises: 'Exercises.php' (stored in a Mysql database) and depending on what title the user clicks (with links), I want the title and the question itself in another page: ‘question.php’. The questions will also be taken from the database. Im trying to use a GET parameter in the exercise link with the id of the exercise. Then in ‘question.php’, get the exercise with that id from the database.
This is some of the code that I've done so far but I'm stuck. Could you help me?
Thank you.
Exercises.php – In here I have all of the titles of the exercises displayed.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises";
$result = $conn->query($sql);
?>
<?php
while($row = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo $row["exercise_id"]; ?></td>
<td><a name="search" href="http://localhost/PHP%20Pages/2.php" target="_blank"><?php echo $row["title"]; ?></a></td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<?php
}
?>
question.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises"; /*Select from table name: exercises*/
$result = $conn->query($sql); /*Check connection*/
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["exercise_id"] . ". " . $row["title"] . $row["text"] . "<br>";
}
}
?>
If you want to get the excericise_id from excercise.php you can change your href like this
Add Topic</td>
And you can get that exercise id in your question.php page like this
$id=$_GET['id'];
I would like you to suggest you to keep your db connection in separate file and try to include and use. that is not the good way to write database connection in every page.

mysql query displays field data not matched to LIKE statement

I've been spending hours on this one.
So I got two tables, the "candidate_list" table and "election_title" table. I need to display all the candidates running in a certain election/club by matching the "ename" column on both tables. Also, it has an if while statement because it is highly dependent on admin input. Hope you can help me with this one.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voting_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ename FROM election_title ORDER BY `sdate` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$ename= $row['ename'];
?>
<p><font size= "6px" align = "center" color = "#efbf77"> <?php echo $row['ename']. "<br>";?></p>
<?php
$servername1 = "localhost";
$username1 = "root";
$password1 = "";
$dbname1 = "voting_system";
$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
$sql = "SELECT * FROM candidate_list WHERE ename IN (SELECT ename FROM election_title);";
$result = $conn1->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$ename= $row['ename'];
$pos= $row['pos'];
$fname= $row['fname'];
$mname= $row['mname'];
$lname= $row['lname'];
?>
<button type="submit" class = "ename"> <?php echo "<p class = 'bold'>" .$row['ename']. " </p>" .$row['pos']. "<br>" .$row['fname']. " " .$row['mname'] . " " .$row['lname'];?> </button>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<?php
}
} else {
echo "0 results";
}
$conn1->close();
?>
As of now, it only display one election title. I need it to loop and display all my elections together with its respective candidates.
UPDATE: The query has worked after some reading on INNER JOINS. Thanks for your help guys! :)

Count rows in a SQL group

I have a script that I want to put on a dashboard. The SQL works and groups the items in the while and it is displayed in the dashboard correctly. However I also want the QTY's to show.
So if the SQL is grouping how can I count the rows it returns so I can echo the QTY out in to my table for each row. This is my code.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
$custid = $row['CustomerID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT * FROM stock
JOIN hardware ON stock.HardwareID = hardware.HardwareID
WHERE stock.CustomerID = $custid GROUP BY stock.HardwareID";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row1['Hardware']."</td>";
echo "<td></td>";
echo "</tr>";
}
}
?>
So the aim is to have the row
echo "<td></td>";
Have a variable in it that shows the qty for each row that is grouped.
you can use this query, group by query has to select and provide aggregation, here i have provided count, if you require some of appropriate column you can change it accordingly...
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
$custid = $row['CustomerID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT stock.Hardwareid, count(*) as CountHardware FROM stock
JOIN hardware ON stock.HardwareID = hardware.HardwareID
WHERE stock.CustomerID = $custid GROUP BY stock.HardwareID";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row1['Hardware']."</td>";
echo "<td></td>";
echo "</tr>";
}
}
?>

How to retrieve images from database in php

I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.
And i have data in the database where multiple images can contained by single pid.
image contains database data
now i want to retrieve images from database according to given pid. there can be more than one image.
All i know it there should be an iteration to retrieve images.
I want to display images in HTML .
can you please show me the way to do it in php?
Thanks in advance guys
This may help you
<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>
in the inc/database.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>
To see how it works try visiting : file.php?propid=22
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
//create sql
$sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
$result = mysqli_query($con, $sql);
$row = mysqli_num_rows($result);
//retrive data print here
if($row > 0){
while($col = mysqli_fetch_assoc($result))
{
echo $col['location'];
}
} else {
echo 'no result found.';
}
?>
wish it helps

Categories