Home > Enterprise >  How to save long text from textarea-input line per line
How to save long text from textarea-input line per line

Time:09-28

How to save long text from textarea-input line per line i have a form with a text area, i wanna save a long text line per line in mysql i have no idea

$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
        }

    fclose($handle);
}

CodePudding user response:

You need to learn how mysql interacts with the DB. You will likely need to use VARCHAR datatype depending on how big you text area is. So if the field from the from is up to 250 characters then the text area column's datatype would be VARCHAR(250).

You would do a POST request to a file with something like this:

$post = $_POST;
//set other fields here, I recommend sanitizing your inputs.
...
$textarea = $_POST['text_area'];
$servername = "HOST";   
$username = "username"; 
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (...other columns you have, textarea)
       VALUES (..., $textarea)";

if ($conn->query($sql) === TRUE) {
   echo "New record created successfully";
} else {
   echo "Error: " . $sql . "<br>" . $conn->error;
}

Two links I highly suggested looking at:
How to filter inputs via php(used before sql execution)
PHP MySQL Insert Data

CodePudding user response:

follow these steps to save any type of data in mysql using php

Step one

open xampp and run tomcat and mysql 

Step two 

now go to phpmyadmin and create a database mydatabase 
and create table named  posts and add fields id , title , description;

Datatypes for you columns

id = int  make it auto_increment and primary key !important
title = varchar(256)
description  = longtext

remember to select these datatypes in phpmyadmin

Step three

open your code editor make a file called posts.html and past 
the below code in your body section 

    <form action='post.php' method='post'>

    <div >
    <label >Post title </label>
    <input type="text" name='title' >
    </div>

    <div >
    <label >Post Description</label>
    <textarea name='descp'  rows="3"></textarea>
    </div>

    <button type="submit" >Submit</button>
    </form>

Step Four

now to handle the action you have to create a file post.php and past the blow code 



        <?php 
    $con = mysqli_connect('localhost' , 'root' , '' , 'mydatabase'); 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
        $title =   $_POST['title']; $descp =   $_POST['descp'];
         $query = "insert into posts(title , description) value($title , $descp)";
          $result = mysqli_query($con , $query);
           if($result){ echo 'data saved successfully'; }
           else{ echo "sorry their is an error please try again"; 
           } }
           ?>


Step five 

now open you phpmyadmin and go the database named mydatabase and open the table posts you fill find the record their which you have submitted now .

CodePudding user response:

It would have been easier if I had a look at your form data. I'll assume my own form data to try and answer your question.
form.php

            <form action="processing.php" method="POST">
                  <textarea required name="records"  rows="8" cols="4" placeholder="Enter records separated by new line"></textarea>
              <button type="submit" name="addRecords" >Add Records</button>
            </form>

Then processing.php

if (isset($_POST['addRecords'])) {
    $record = $_POST['records'];
    //explode records based on new line \n
    $records = explode("\n", $record);

    foreach ($records as $new) {
      $data = $new;
      //Here you'll write your sql code to insert records in the database
      
    }
  }
  • Related