2

In my app data is fetched from a database. For that a variable is passed to a php script and should be used in the query later. I tried it using $var = $_POST['name'] in the query, but it seems like the passed variable is empty. To test it, I inserted the variable into a table and used the isset() method as well. The first part works as it should, the variable is set and inserted, but in the second part with the query to fetch the data it doesn’t seem to be set since the variable returns the value „a“. Why is the value not set and not taken from above?

The php code:

<?php

include Connection.php;



$conn = new mysqli($servername, $username, $password, $dbname);


if(isset($_POST['name'])){

 $query = "INSERT INTO TestTable (name) VALUES ('$_POST[name]')";


 if(mysqli_query($conn,$query)){

    echo 'Data Submit Successfully';

 }
 else{

    echo 'Try Again';

 }
}



if(!isset($_POST['name'])){
    $var = "a";
} 

 $query = "SELECT * FROM TestTable WHERE name = '$var'";

    $result = mysqli_query($conn, $query);

    while($row = mysqli_fetch_assoc($result)) {
            $array[] = $row;
        }



    header('Content-Type:Application/json');
    echo json_encode($array);
    mysqli_close($conn);

?>
4
  • change "INSERT INTO TestTable (name) VALUES ('$_POST[name]')" to "INSERT INTO TestTable (name) VALUES ($_POST['name'])" Commented Apr 30, 2018 at 10:04
  • @UmerFarooq Sorry for unprecise questioning, I tried to improve it. The problem I have is in the fetching part, my bad! Commented Apr 30, 2018 at 10:06
  • have you tried query without variable i mean "SELECT * FROM TestTable WHERE name = 'a'" Commented Apr 30, 2018 at 10:15
  • @UmerFarooq yes I tried that and that works as it should do as well! Commented Apr 30, 2018 at 20:41

2 Answers 2

2

Your query string is incorrect, fix by this:

//...
$query = "INSERT INTO TestTable (name) VALUES ('".$_POST['name']."')";
//...

To fix SELECT query you should initialize $var in case isset($_POST['name']) returns true

//...
$var = "a";
if(isset($_POST['name'])){
    $var = $_POST['name'];
} 

$query = "SELECT * FROM TestTable WHERE name = '$var'";
Sign up to request clarification or add additional context in comments.

2 Comments

The inserting works this way, is it wrong or unconventional?
@MikeKng string can't parse array with key expression, you need to do it with a help of string concatenation or with a help of additional variable. ('".$_POST['name']."')"; or $var = $_POST['name']; $query = "INSERT INTO TestTable (name) VALUES ('$var')";
2

In the code

if(!isset($_POST['name'])){
    $var = "a";
} 

 $query = "SELECT * FROM TestTable WHERE name = '$var'";

change this to

$var = "";

if(!isset($_POST['name'])){
    $var = "a";
} 

 $query = "SELECT * FROM TestTable WHERE name = '$var'";

9 Comments

Isn‘t $var just empty then?
No..when !isset($_POST['name']) gets true, its value will set to 'a'. $var must be declared outside the scope of if block so that you can access it out side the scope of if block.
Since your select query is outside the if block therefore $var must be declared outside the scope of if block otherwise you can't access $var in your query
I understand, but $_POST['name'] is used above as well, why does the isset() return that it‘s empty?
have you tried debugging the script by calling var_dump($_POST) before calling isset(). ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.