Click here to Skip to main content
15,564,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i trying to get values the above error is displaying

PHP




<?php 
session_start();
include("connectdb.php");
//include("statuselectricity.php");
 if( isset( $_POST['id'])) {
    $id = $_POST['id']; 
} 

          $query = "SELECT * from approvedelectricity  where id` = '$id';";
     $result= mysqli_query($conn, $query);
 if(mysqli_num_rows($result)>0){
                   while($row = mysqli_fetch_array($result))
    $rows[] = $row;
foreach($rows as $i){
            if($i['message']=='approved'){
                echo " liked your post";
}else{
                echo "rejected";
}
}
}
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?><br/><a href="index.php">BACK</a>


What I have tried:

i need to display message apporved if message is approved
Posted
Updated 5-Nov-20 10:13am
Comments
Mohibur Rashid 27-Oct-19 16:02pm    
what would happen if $_POST['id'] does not exists

That is because your $id variable goes out scope outside the if block.
if( isset( $_POST['id'])) {
    $id = $_POST['id']; 
} 
So, you need to either create the variable outside this scope and update its value here, or you should bring the code that accesses this variable. I believe second option is much suitable for this, as in case the $id does not exist, there is no point of executing the code.
PHP
if(isset($_POST['id'])) {
    $id = $_POST['id']; 

    $query = "SELECT * from approvedelectricity  where id = '$id';";
    $result = mysqli_query($conn, $query);

    // .. so on
} 
One more thing, this code is being exposed to SQL Injection, and that is a very bad thing—read more here[^]—so you should always sanitize the inputs as you receive them.

mysql - How can I prevent SQL injection in PHP? - Stack Overflow[^]
 
Share this answer
 
PHP
$query = "SELECT * from approvedelectricity  where id` = '$id';";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
-----
Advice: Learn to indent properly your code, it highlight its structure and it helps reading and understanding. It also helps spotting structures mistakes.
PHP
<?php
session_start();
include("connectdb.php");
//include("statuselectricity.php");
if( isset( $_POST['id'])) {
    $id = $_POST['id'];
}

$query = "SELECT * from approvedelectricity  where id` = '$id';";
$result= mysqli_query($conn, $query);
if(mysqli_num_rows($result)>0){
    while($row = mysqli_fetch_array($result))
    $rows[] = $row;
    foreach($rows as $i){
        if($i['message']=='approved'){
            echo " liked your post";
        }else{
            echo "rejected";
        }
    }
}
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?><br/><a href="index.php">BACK</a>

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
v2
$query = "SELECT * FROM `approvedelectricity` WHERE `id` = $id";
$result= $conn->query($query);
 
Share this answer
 
v2
Comments
Richard Deeming 6-Nov-20 8:39am    
An unchanged copy of part of the question is NOT a "solution" to the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900