Odd paragraphs added to html after PHP POST - php

When i click the submit button for my search from, for each result there is a paragraph with a ">" which is put at the top of the page and I have no idea where it comes from. Here's my code (using wordpress)
[insert_php]
$db_host = "localhost";
$username = "root";
$password = "";
$dbname = "Tickets";
// Create connection
$conn = new mysqli($db_host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if((isset($_POST['Search'])) && ($_POST['Search'] != '')){
$search = $_POST['Search'];
$sql = "SELECT * FROM `Problem_Solutions` WHERE (`Problem_Type` LIKE '%$search%') OR (`Solution_Title` LIKE '%$search%')";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
if ($count > 0){
echo "<table><th>Title</th><th>Solution</th><th>Count</th>";
while ($row = mysqli_fetch_array($query)){
echo "<tr><td>".$row['Solution_Title']."</td>";
echo "<td>".$row['Solution']."</td>";
echo "<td>".$row['Count']."</td>";
echo "<<td><form name='form' method='POST'><input style='background:#6B8E23;' type='submit' name='".$row['Solution_ID']. "' value='This Solution Solved My Problem' /></form></td></tr>";
} echo "</table>";
}else{
echo " 0 results found";
}
}
$conn->close();
[/insert_php]

Your problem is in this line:
echo "<<td><form name='f
Delete the first <

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";
}

Dynamically generate buttons with loop php

What I wan't to do is create buttons that are automatically generated from the database. So when I add a new record in the database the button is created Is this possible with a loop? So yes how do I create the button.
This is what I have so far:
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row["theme_name"]. "<br>";
}
} else {
echo "no results";
}
$conn->close();
?>
Yes it is possible. you need to echo html
<?php
$servername = "localhost";
$username = "root";
$password = "Iamthebest1009";
$dbname = "dktp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM theme";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$your_url ="https://www.google.com";
echo "". $row["theme_name"]. "<br>";
echo '<input type="button" name="' . $row["theme_name"]. '" value="'. $row["theme_name"].'">';
}
} else {
echo "no results";
}
$conn->close();
?>

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

music from PHPMyAdmin to play in a modal box

I am trying to to put a music from my table in phpmyadmin into a modal box. I have been able to have them in a modal box but it puts all of them in and I only want the specific music allocated to a specific button to play.
Here is my code:
audio.php
<?php
/*the variables*/
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "arashi_site";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
/*sql to extract the data*/
$sql = "SELECT * FROM music";
$result = mysqli_query($conn, $sql);
/*condition to see if there are any results*/
if (mysqli_num_rows($result) > 0) {
/*a loop function to output */
while($row = mysqli_fetch_assoc($result)) {
/*echos the data within each column*/
/*within the final <td>, audio controls will be echoed to play the song files. When the user clicks any of the buttons in the audio controls, they will remain in the second section the the site and not be sent back to the top.*/
echo "<button type='button' class='btn btn-info btn-lg' data-toggle='modal' data-target='#myModal'>" . $row["song_name"] . "</button>";
}
}
else {
echo "0 results";
}
mysqli_close($conn);
?>
audio2.php:
<?php
/*the variables*/
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "arashi_site";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
/*sql to extract the data*/
$sql = "SELECT song_name FROM music";
$result = mysqli_query($conn, $sql);
/*condition to see if there are any results*/
if (mysqli_num_rows($result) > 0) {
/*a loop function to output */
while($row = mysqli_fetch_assoc($result)) {
echo "<audio id='sound' controls>
<source src='audio/" .$row['song_name']."' type='audio/mpeg'>
Your browser does not support the audio element.
</audio>";
}
}
else {
echo "0 results";
}
mysqli_close($conn);
?>

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.

Categories