Add data from textarea in mysql

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Add data from textarea in mysql

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 ?

Admin Posts: 805
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;
}

JanMolendijk Posts: 282
thanks for the quick answer you all do greatest work
i gonna try this code hope i`m able to complete