2

I have tried the code below but it just wrote '1' in the database instead of the text i wrote. i tried the same with input type="text" and it worked. Why does it not work with textarea? Ofc i have the if(isset) function too.. PHP:

$connectionText = mysql_connect("localhost", "root");
if(!$connectionText){
die("Noget gik galt?". mysql_connect_error());
}
mysql_select_db("textbox", $connectionText);

$t = isset($_POST['tekst']);

$sqlScript = "INSERT INTO forms (tekst) VALUES ('$t')";

mysql_query($sqlScript, $connectionText);
mysql_close($connectionText);

?>

HTML:

<form action="post.php" method="POST">
<textarea name="tekst" autocapitalize="on" autocomplete="off" 
autocorrect="off" 
class="textBox" maxlength="140"></textarea>
<input type="submit" value="Offentligør" name="submit">
</form>
R. A
  • 23
  • 2
  • 7
  • 1
    `$t= isset($_POST['tekst']);` thats the problem –  Jun 26 '17 at 21:06
  • http://php.net/manual/en/function.isset.php, `Returns TRUE if var exists and has value other than NULL. FALSE otherwise.`. You also are open to SQL injections and using an out of date driver. Upgrade your driver (mysqli or pdo) and parameterize. – chris85 Jun 26 '17 at 21:09
  • 1
    The code you are writing is open to the most prevalent attack on the web, SQL Injection. Read here: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Scopey Jun 26 '17 at 21:13

4 Answers4

4
$t = isset($_POST['tekst']) ? $_POST['tekst'] : '';

Otherwise, $t is a boolean

EvgenyKolyakov
  • 3,310
  • 2
  • 21
  • 31
3

Fucntion isset only check if given input is defined or not. So it returns true or false as output and when you save it in database you will see only 1 (if $t is true) or 0 (if $t is false).

You should edit this line $t = isset($_POST['tekst']); as one of following codes if you want save value of $_POST['tekst'] in the database:

  • if-else version:
if(isset($_POST['tekst']))
    $t = $_POST['tekst'];
else
    $t = '';
  • short if version:
$t = isset($_POST['tekst']) ? $_POST['tekst'] : '';
hadi.mansouri
  • 828
  • 11
  • 25
2

Function isset($your_variable) return true if $your_variable is defined, otherwise return false. So $t is TRUE, and it is translated into 1.

TIP: Your code is vulnerable to SQL injection (you should escape $t when you build your SQL statement) TIP 2: Validate the user input first, if all OK then connect to DB (save resources)

Fulldump
  • 783
  • 5
  • 10
-2

You only need to add an id value to the <form> and then add the form attribute to the textarea with the form id value

<form id="sample".....>
  <textarea name="aba" form="sample".....></textarea>
</form>
Ôrel
  • 7,044
  • 3
  • 27
  • 46
Tacho
  • 11
  • 3