-2

Is it possible to show variable username before I am putting it into DB? Echo, alert, console or something? I want to check what is in $username before do INSERT

 <?php

    require_once 'connect.php';

    $data = json_decode(file_get_contents("php://input"));

    $username = mysqli_real_escape_string($connect, $data->username);


    $query = "INSERT into tablename (username) VALUES ('$username')";

    mysqli_query($connect, $query);
    echo true;
    ?>
Defus
  • 789
  • 2
  • 12
  • 22
  • 2
    `echo $username;` ? – David Feb 20 '17 at 13:29
  • 1
    Why would it _not_ be possible? You seem to know about PHP's `echo` and JavaScript's `alert()` and `console.log()` already, and `$username` is just a variable… – ChrisGPT was on strike Feb 20 '17 at 13:30
  • 3
    Please learn how to use prepared statements instead of substituting variables into the SQL. – Barmar Feb 20 '17 at 13:31
  • Do you want to see username first and then decide whether put into DB or not ? – Niklesh Raut Feb 20 '17 at 13:33
  • I am taking username from input field and I want to show what user inputed. – Defus Feb 20 '17 at 13:34
  • @David doesn't work... – Defus Feb 20 '17 at 13:35
  • 1
    @Defus: Define "doesn't work". `echo $username;` will, in fact, echo whatever is in the `$username` variable. If you're doing something else wrong, you're going to have to explain it. And whether this is done "before" or "after" the query really makes no difference here, since both of those things are in the same operation. – David Feb 20 '17 at 13:36
  • Everything works fine I can insert into database. I just want to show what is inside before INSERT – Defus Feb 20 '17 at 13:37
  • @Defus: It's not clear at all what you're asking or what isn't working here. Nor is it even clear why you need to return data to the client that you *just received from the client*, since clearly the client *already has* that data. At the very least, it sounds like you're confused about the difference between client-side and server-side code here. If that's the case, this would be a good place for you to start: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – David Feb 20 '17 at 13:53
  • @David ok from the beginning I am using angular where I am getting current Date. But when I inserting it into database I have mytime - 1 hrs. For example is 14:55 so I get 13:55. In my code I do {{myDate | date:'dd-MM-yyyy:HH:mm:ss'}} for check variable myDate and also works fine but in a while I tried insert it into database I am getting correct date and incorret time. So I want to show variable before Insert it just for test. There is $username just for example – Defus Feb 20 '17 at 13:58
  • 1
    @Defus: And what happens when you simply do `echo $username;`? I don't know what client-side functionality you expect to be performed, because the question contains nothing about that. But anything you `echo` will be in the HTTP response. Echo the username, echo the SQL query, echo anything you like. It will be in the response. If it's "not working" (whatever that means), then what are you doing with the response? Are you looking at it at all? – David Feb 20 '17 at 14:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136160/discussion-between-defus-and-david). – Defus Feb 20 '17 at 14:08
  • @David I moved discussion to chat – Defus Feb 20 '17 at 14:14
  • @Defus: Sorry, I don't offer private tutoring services. If you have information relevant to the question, it should be put in the question. – David Feb 20 '17 at 14:17

2 Answers2

0

Try this

$username = mysqli_real_escape_string($connect, $data->username);

 if($username)
 {
  echo $username;

$query = "INSERT into tablename (username) VALUES ('$username')";

mysqli_query($connect, $query);


}
Mudassar Saiyed
  • 1,146
  • 10
  • 20
  • 1
    Do or do not. There is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Feb 20 '17 at 14:00
0
<?php

    require_once 'connect.php';

    $data = json_decode(file_get_contents("php://input"));

    $username = mysqli_real_escape_string($connect, $data->username);


    $query = "INSERT into tablename (username) VALUES ('$username')";


    echo "<script>alert('".$username."')</script>";

    mysqli_query($connect, $query);

    echo true;
    ?>
Sazzad Hussain
  • 321
  • 1
  • 3
  • 11
  • echo $username ; if this prints username, then It must give alert box written username. – Sazzad Hussain Feb 20 '17 at 13:41
  • Check if your browser has JavaScript enabled. Still you can try this echo ""; – Sazzad Hussain Feb 20 '17 at 13:42
  • ofc is enabled but still nothing. hmm – Defus Feb 20 '17 at 13:51
  • 1
    Code only answers are considered "bad form". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Feb 20 '17 at 14:01
  • I can't show here even Hello World alert. There is my angular piece http://pastebin.com/V67KDLuS – Defus Feb 20 '17 at 14:15
  • @Defus: According to that "angular piece" you're not *trying* to show any kind of alert. The response from the server is in the `data` variable in the AJAX callback, a variable you're not using for anything. – David Feb 20 '17 at 14:18
  • @David I am using to insert it into database I don't want it for something else – Defus Feb 20 '17 at 14:19
  • @Defus: If you don't want to do *anything else* other than insert into the database, and if it is *currently* inserting into the database, then *what are you even asking here*? That last comment contradicts everything you've asked/said so far in this question. Nobody can help you if you can't even decide what you're trying to do. – David Feb 20 '17 at 14:20