-2

I want to insert data/array to database with a line.
my html look :

<form action="file.php" method="POST">
    <textarea name="data">
          DATA1
          Data2
    </textarea>
    <input type="submit">
</form>

and file.php is :

<?php
$data = $_POST['data'];
require_once("config.php");
mysql_query("INSERT INTO database VALUES('$data')");
?>

But it not inserted to my database :(
Any help?

mymiracl
  • 583
  • 1
  • 16
  • 24
Japar S
  • 77
  • 2
  • 10
  • 1
    Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Dec 28 '15 at 16:04
  • 1
    Plus, if `database` is indeed the name of your table, it's MySQL reserved word https://dev.mysql.com/doc/refman/5.5/en/keywords.html and requires special attention. We also don't know if you successfully connected and using the same API as your query function. – Funk Forty Niner Dec 28 '15 at 16:05
  • possible duplicate of http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql – Funk Forty Niner Dec 28 '15 at 16:06
  • The tittle has nothing to do with the code... – FirstOne Dec 28 '15 at 16:08
  • sorry,i want to add to database multiple data.Example : in line 1 is data 1 in line 2 is data 2 – Japar S Dec 28 '15 at 16:10
  • but it insert in different table. – Japar S Dec 28 '15 at 16:10
  • 5
    there is no way of providing you with a solution with what you've posted. Check for errors as I've already outlined above. You're **not** doing that and expect us to debug your code. That's your job, not ours. Come back and tell us what those are, only then can we help you. – Funk Forty Niner Dec 28 '15 at 16:11
  • You [**shouldn't use mysql_* functions**](http://stackoverflow.com/q/12859942/4577762), they are deprecated since PHP 5.5 and removed on PHP 7.0. And, this code is insecure, take a look at [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174/4577762) – FirstOne Dec 28 '15 at 16:12
  • Thank you very much i've found error on mysql – Japar S Dec 28 '15 at 16:16
  • ...and what would that be exactly? – Funk Forty Niner Dec 28 '15 at 16:18
  • 1
    @JaparS Use `explode` to split the data into an array. Then use a `foreach` loop to perform an `INSERT` for each array element. – Barmar Dec 28 '15 at 16:18

1 Answers1

1

As @Fred-ii- says, with the information you provide there is no way to provide a solution. However you can help yourself by changing your code to add some error reporting and also some status checking of the database accesses

<?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);

    require_once("config.php");

    $data = isset( $_POST['data'] ) ? $_POST['data'] : '';
    if ( isset( $_POST['data'] ) ) {
        $data = $_POST['data'];

        $res = mysql_query("INSERT INTO `database` VALUES('$data')");
        if ( ! $res ) {
            echo mysql_error();
            exit;
        }
    } else {
        echo 'The data field was empty';
    }
?>

Run this and you should receive at least one error to help you move forward.

Tell us what the error is and we will help if it is not obvious to you what is wrong.

Additional Note:

Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

It is also odd to call a table database! A database is a collection of many tables. It will help you, and/or others later if you give your datbase tables sensible names that describe what data is being held in that table.

Also there is no reason why 2 lines inside a <textarea> should not be stored in one column of your table.

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149