mysql query displays field data not matched to LIKE statement - php

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! :)

Related

Echo only current user details

please can someone help me with this code? It shows all the users’ info but i need it to show only the info of the logged user.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "username";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT AEG FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 3) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["AEG"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Upon login, store the email/username/id in a $_SESSION variable;
$_SESSION['email'] = $email; // in this example I used email
Then on your file, you can access session variables using $_SESSION['variable'] and use it on your sql statement;
My modifications are the ones with comments.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "username";
/*Store session data in a variable*/
$email = $_SESSION['email'];
/**********************************/
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/*Add where clause to your sql statement*/
$sql = "SELECT AEG FROM users WHERE email ='".$email."'";
/****************************************/
$result = $conn->query($sql);
if ($result->num_rows > 3) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["AEG"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try this one if you are having 4 user then it is showing only one
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "id: " . $row["AEG"]. "<br>";
}
}
else
{
echo "0 results";
}

Trying to get propperty of non-object Mysqli result

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "job_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Get']))
{
$jtitle = $_POST['jobtitle'];
$location = $_POST['location'];
$category = $_POST['categories'];
//Query specified database for value
$sql = "SELECT * FROM addjob where jtitle ='$jtitle' ∧ location ='$location' ∧ category ='$category' " ;
$result = $conn->query($sql);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "jtitle: " . $row["jtitle"]. "location:" . $row["location"]. "category" . $row["category"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Change your sql query as follows,
$sql = "SELECT * FROM addjob WHERE jtitle =".$jtitle." AND location =".$location." AND category =".$category." ;
since ∧ is wrong, what it does is, it ends the SQL query because there is ; in the end. So change ∧ into and and check.
And read more about form validation and SQL injection from these links.
Good luck! =)

How to return the results of my query in rows

This is my query, and it is working fine, but how do I have the results returned in an HTML TABLE. for example I want the first 5 results on row 1 the next 5 on row 2 and so on. Any help would be great. Thank you!
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM manufacturer LIMIT 10 OFFSET 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<td><a href="index.php?route=product/manufacturer/info&
manufacturer_id='. $row["manufacturer_id"] .'"><img src="image/' .
$row["image"] .'"></a></td>';
}
} else {
echo "0 results";
}
$conn->close();
<?php
$servername = "XXX";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn- >connect_error);
}
$sql = "SELECT * FROM manufacturer LIMIT 10 OFFSET 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i=0;
while($row = $result->fetch_assoc()) {
If($i==5){
echo "</tr>";
echo "<tr>";
$i=0;
}
echo '<td><a href="index.php? route=product/manufacturer/info&
manufacturer_id='. $row["manufacturer_id"] .'">. <img src="image/' .
$row["image"] .'"></a></td>';
$i++;
}
} else {
echo "0 results";
}
$conn->close();

PHP Loop through results

I am trying to loop through my database and check to see if the user already exists in another table. If they do then I want to increment a value, if they don't then I want to add the user.
When I run the code below it happily loops through all the results:
<?php
$servername = "p:10*********";
$username = "*******";
$password = "*******";
$dbname = "******";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM payroll WHERE user != ' ' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $result->num_rows;
while($row = $result->fetch_assoc()) {
$user = $row['user'];
$time = $row['time'];
$id = $row['id'];
echo $id;
echo $user;
}
} else {
echo "0 results";
}
$conn->close();
?>
However when I add in the SQL to check to see if they exist in the other table the loop no longer functions correctly and echos the same user each time.
<?php
$servername = "*******";
$username = "******";
$password = "********";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM payroll WHERE user != ' ' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo $result->num_rows;
while($row = $result->fetch_assoc()) {
$user = $row['user'];
$time = $row['time'];
$id = $row['id'];
echo $id;
echo $user;
// Added existing user check:
$sql = "SELECT * FROM smsreport WHERE user = '$user'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "found";
} else {
echo "USER NOT FOUND";
}
}
} else {
echo "0 results";
}
$conn->close();
?>
In the open eye:
Rename the inside $result variable. It is over writting the first $result.
It could be the problem. Not tested though.

Displaying table and data in database doesnt show

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

Categories