I have a text area that I want a list to be entered. I am trying to seperate each list item and put it into a seperate sql entry. Could some one tell me what I am dong wrong?

error_reporting(E_ALL);
ini_set('display_errors',1);


        $textarray = explode("\n",$textarea);if(isset($_POST['textarea'])){
    $textarea= $_POST['textarea'];
    $q = "INSERT INTO Owners(Names) VALUES(:textarea);";
    $query = $odb->prepare($q);
    $results = $query->execute(array(
    ":textarea" => $textarea
    ));

    }?>

Your exploding the textarea variable but not using your array anyway for one thing.
You'd want to get array after checking if the POST varibable exists and then, if each element is to be an insertion of it's own, do a foreach through the array, with your prepare and execute inside that.

    if(isset($_POST['textarea'])){
         $textarea= $_POST['textarea'];
         $textarray = explode("\n",$textarea);
        foreach($textarray as $t) {
           $q = "INSERT INTO Owners(Names) VALUES($t);";
           $query = $odb->prepare($q);
           $results = $query->execute();
    }

when I put in your code I get this error
[11-Feb-2016 15:55:15 America/New_York] PHP Parse error: syntax error, unexpected $end in /home/wrchtnoy/public_html/demos/breaksoft/reg.php on line 22
this is the full code. The connection information is correct.

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$host = "localhost";
$user ="user";
$db = "db_info";
$pass ="mydbpassword";

$odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass); 

error_reporting(E_ALL);
ini_set('display_errors',1);

                if(isset($_POST['textarea'])){
             $textarea= $_POST['textarea'];
             $textarray = explode("\n",$textarea);
            foreach($textarray as $t) {
               $q = "INSERT INTO Owners(Names) VALUES($t);";
               $query = $odb->prepare($q);
               $results = $query->execute();
        }
       ?>

It's missing a closing }, ad another one at line 22 (making the ?> on line 23).
Is the code in the comment above in the exact same format as in your IDE? I'm refering to where each line starts in relation to the others here.
If it is you would benefit greatly from correctly indenting your code. Mistakes like missing } show up much easier.
If that was just the result of pasting it into the editor here then ignore that last part :)

my ide is notepad++ so it must be the pasting.
no errors are being trown now however nothing is going into the database
as far as i know my form code is correct. thank you for your help this is driving me crazy

<body>
<form method="post" action ="reg.php">
Owners: <textarea name="textarea" style="width:100%; height: 70px;" maxlength="300"></textarea>
        <input type="submit" value="add" />
    </form>

Turns out that was my bad, put quotes around the input seeing as its a string.
$q = "INSERT INTO Owners(Names) VALUES('$t');";

Without the quotes MySql didn't recognise $t as a string of characters.

Member Avatar for diafol

There are many ways to do this.
mysqli has multi-query: http://php.net/manual/en/mysqli.multi-query.php
You can run multiple queries / statements with bindParam (PDO).
However - use a transaction - this may speed up the process by 15 fold - depending on the amount of data / number of queries you're running.

I wrote a code snippet here, discussing the timings: https://www.daniweb.com/programming/web-development/code/495371/insert-multiple-records-with-pdo-prepared-statement

Hope it helps.

BTW, splitting and inserting the raw POST data into a statement is leaving you wide open to SQL injection. Pass the var either in bindValue, bindParam or ar an array element in the execute() method.

See DW Tutorial: Common Issues with MySQL and PHP for more info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.