Page 1 of 1

Add data from textarea in mysql

Posted: 30 Aug 2016, 16:20
by JanMolendijk
First great website i learning alot `thanks for this`

My Question:
I did not found an example how to add text into the SQLdatabase from a textarea.
where i hope to get a simple example how to do this ?

Add data from textarea in mysql

Posted: 30 Aug 2016, 17:36
by Admin
Hello
See this example and the comments in code:
- HTML

Code: Select all

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>

<form action="file.php" method="post">
<textarea name="some_name"></textarea><br>
<input type="submit" value="Send" />
</form>

</body>
</html>
- "file.php"

Code: Select all

<?php
//set header to can work with utf-8 charset
header('Content-type: text/html; charset=utf-8');

// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');

// check connection
if(mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}

//if it is received data from form element with name="some_name", and has data
if(isset($_POST['some_name']) && strlen($_POST['some_name']) >0){
  //get form element data
  $some_name = $_POST['some_name'];

  // Prepare an insert statement
  $sql ="INSERT INTO table_name (col_name) VALUES (?)";
  $stmt = $conn->prepare($sql);

  //bind parameters for markers to be added safelly in insert
  if($stmt->bind_param('s', $some_name)){
    // Execute the statement
    if($stmt->execute()) echo 'Data succesfully inserted';
    else echo 'Unable to insert data'. $stmt->error;
  }
  else 'Binding parameters failed: '. $stmt->error;
}

Add data from textarea in mysql

Posted: 31 Aug 2016, 07:53
by JanMolendijk
thanks for the quick answer you all do greatest work
i gonna try this code hope i`m able to complete