The idea is simple. A key you press (a letter) appears in updated page: keyj.php: <input id='k'></input><scripttype="text/javascript"> document.getElementById('k').onkeypress=function() { <?php $db=new PDO('mysql:host=localhost;dbname=test;charset=utf8','$us','$ps'); $db->exec("UPDATE TXT SET texts=k.value"); $rl=$db->query("SELECT texts FROM TXT"); print_r($rl->fetchColumn());?> } </script> The intention is to have text on the screen via print_r. I successfully used it in another php to exchange information with MySQL and users screen (worked). Here - the more you type and the more k.value is the more text will be on screen (just from server). Unfortunately something went wrong.
PHP is run server side BEFORE the page is loaded -- that makes your above code outright gibberish... It will be run ONCE BEFORE it even gets to client side. To do what you are thinking you'd have to make that PHP a SEPARATE file from the form, (that input is in a FORM, right?) and use AJAX to call the php that does the UPDATE. Which is a very slow and painful handshake to the server every character, an asynchronous operation, and in general just asking for it to fail. There's a reason you don't see this **** being done that way. Also, there is no reason for that ::query since you just set the value, you know the value... you also don't perform a submit, so there is no such thing (at least not in the code you are showing) as k.value. Bottom line: you are completely failing to grasp what PHP is, what JavaScript is, the order they execute in, and the limitations of what is basically a pull technology.
What on earth are you trying to achieve here? There is so much wrong with your script it's hard to start from anywhere. Good luck.
You need to research AJAX calls. Fastest way would be to use jQuery (google it) and make a separate .php page. Then make an ajax call with javascript/jquery to that php page where you have the php code to update the database. As the members above have stated, that code is wrong from many points of view. The php code gets executed and the output is put between those brackets then that is server to the browser. So the php code does not get executed when the js function is called.