Jump to content

Write each textarea line as a new row in database


CaraRota

Recommended Posts

Hello there; i'd like to make it possible to write all the inputs i write with this text fields but make it in a single row (on a textarea), and make it possible to add multiple "events" in my website instead of one!

 

For example:

FIRST ROW IN TEXTAREA: Date - Local Team - Away Team - RatioLocal - Draw - RatioAway - SportID - League

SECOND ROW IN TEXTAREA: Date - Local Team - Away Team - RatioLocal - Draw - RatioAway - SportID - League

 

This would be a textarea example:

 

2013/04/03 21:00    Bolivar    The Strongest    2.20    3.20    2.88    1 LFP
2013/04/03 21:00    La Paz    Oriente Petrolero    4.33    3.50    1.73 1 LFP   
2013/04/03 21:00    Wilstermann    Universitario    1.91    3.40    3.60 1 LFP

 

Here are the files i use; you will get my idea as soon as you see them :)

Agregar.php: http://pastebin.com/xeTyjrX8

Config.php: http://pastebin.com/Tyb8SXuQ

 

Please if you need more information ask me about it!

Thanks a lot for your help!

Edited by CaraRota
Link to comment
Share on other sites

Hi Jessica,

 

I have looked at it, but i do not understand that guys coding and i certainly need something different..

 

It'd be:

 

Line1 Line2 Line3

Line4 Line5 Line6

..

LineX LineY LineZ

 

Altough i found this code much easier to me: http://forums.phpfreaks.com/topic/84395-solved-is-there-any-way-to-separate-lines-in-a-textarea-into-seperate-variables/

 

I have posted a question over there too.

 

Thanks for your reply!

Link to comment
Share on other sites

I've got this so far:

 

if($_POST['agendar'])
{
    // Verificamos que no haya ningun dato sin rellenar.
    if(!empty($_POST['local']))
    {
        // Pasamos los datos de los POST a Variables, y le ponemos seguridad.

$text="";

$a=explode(' ',$text);

foreach($a as $res){

        // Configuracion de la base de datos.
        $dbhost = ""; // Servidor
        $dbuser = ""; // Usuario
        $dbpass = ""; // Contraseña
        $dbname = ""; // Tabla

        // Creando conexion.
        $link = mysql_connect($dbhost,$dbuser,$dbpass);
            mysql_select_db($dbname,$link);

$res=mysql_real_escape_string($_POST['res']);

$sql="INSERT INTO events (hometeam, awayteam, ratiohome, ratioaway, ratiox, sportid, starttime, liga) VALUES ('$res')";
$sql_result=mysql_query($sql) or die(mysql_error());
}
    } else {
        // Si hay un dato sin rellenar mostramos el siguiente texto.
        $msg = "Falta rellenar algun dato.";
    }
    
}
 

But it doesn't seem to work; here's the error:

 

"Column count doesn't match value count at row 1"

 

Even though i write, for example: 1 2 3 4 5 6 7 8 in the textarea..

Link to comment
Share on other sites

Yeah, you are going to have a real problem with that input. You can easily explode() the text into individual lines. But then, I see no easy way to differentiate the data on a single line into the different sets of data. Well, not without more information.

 

You first state the information will be in this format

 

Date - Local Team - Away Team - RatioLocal - Draw - RatioAway - SportID - League

 

Then you show an example of this

 

2013/04/03 21:00    Bolivar    The Strongest    2.20    3.20    2.88    1 LFP
2013/04/03 21:00    La Paz    Oriente Petrolero    4.33    3.50    1.73 1 LFP   
2013/04/03 21:00    Wilstermann    Universitario    1.91    3.40    3.60 1 LFP

 

Those are not in the same format. If there are dashes between the different pieces of data, you can simply explode() the line using that character. But, your sample data example does not show dashes. But, it does show four spaces between each piece of data - except the last one. I don't know if that is correct or just how you decided to post the data. You need to determine a rule or rules(s) that you can rely upon to programmatically differentiate the different pieces of data.

 

Also, you shouldn't just explode the data and assume that the right number of fields are present. You need to verify each record before you execute the query.

 

 

Lastly, your error is due to this:

 

$a=explode(' ',$text);

foreach($a as $res){

 

You explode the string based upon spaces and then execute a loop for each "word". Then in that loop you try to run a query

 

$res=mysql_real_escape_string($_POST['res']);

$sql="INSERT INTO events (hometeam, awayteam, ratiohome, ratioaway, ratiox, sportid, starttime, liga) VALUES ('$res')";

 

That query is trying to insert a record for 8 different fields, but you are only supplying ONE value.

Edited by Psycho
Link to comment
Share on other sites

Hi psycho,

 

Thanks for your answer, but honestely i know very little php and im trying to understand what you've said.

First of all, i really don't care if the input is divided by a - or a space, or 4 spaces; i can sort that out as soon as my explode() works :)

 

Second, i though that inserting the value $res would give me as many records as i type in the textarea (for example, 1 2 3 4 5 6 7 8 should be working on that code.. im guessing).

 

So, to state a ground base, let's say i want to post this into the textarea:

 

2013/04/03 21:00-Bolivar-The Strongest-2.20-3.20-2.88-1-LFP
2013/04/03 21:00-La Paz-Oriente Petrolero-4.33-3.50-1.73 1-LFP   
2013/04/03 21:00-Wilstermann-Universitario-1.91-3.40-3.60-1-LFP

 

That'd be an 8 input data with a - in the expode();

 

$a=explode('-',$text);

foreach($a as $res){
 

 

and then execute the query normally (without the $res=mysql_real_escape_string($_POST['res']);)

 

 

$sql="INSERT INTO events (hometeam, awayteam, ratiohome, ratioaway, ratiox, sportid, starttime, liga) VALUES ('$res')";
$sql_result=mysql_query($sql,$link) or die(mysql_error());
 

 

Would this be right then? Because it continues giving me the same error... :(

 

Thanks a lot for your help!

Link to comment
Share on other sites

First of all, i really don't care if the input is divided by a - or a space, or 4 spaces; i can sort that out as soon as my explode() works :)

 

You don't care? You have to write code to programatically understand how to interpret the user input. As humans we can read an address, for example, and intuitively know the street, city, country, etc. A computer would just see a string of random characters. We have to write logic to break apart a string and determine what values are what. If the values are separated by a dash, then we can explode on the dash. If not, then we have to come up with some other rule or rules on how the string will be broken apart.

 

As for your error, you are exploding a line of text based upon the dash and then taking ONE value at a time and trying to insert a record which requires eight values.

 

If you are going to receive multiple lines, then follow the logic from the other post linked above to process the results into a single line. THEN use explode on each line to break it into the eight different values and use ALL EIGHT of those values in the insert query.

Link to comment
Share on other sites

You don't care? You have to write code to programatically understand how to interpret the user input. As humans we can read an address, for example, and intuitively know the street, city, country, etc. A computer would just see a string of random characters. We have to write logic to break apart a string and determine what values are what. If the values are separated by a dash, then we can explode on the dash. If not, then we have to come up with some other rule or rules on how the string will be broken apart.

 

As for your error, you are exploding a line of text based upon the dash and then taking ONE value at a time and trying to insert a record which requires eight values.

 

If you are going to receive multiple lines, then follow the logic from the other post linked above to process the results into a single line. THEN use explode on each line to break it into the eight different values and use ALL EIGHT of those values in the insert query.

 

Let me see if we understand each others; what i mean is, i dont care because I AM the one who is going to "upload" the matches, and i, as human, can use a - or a / or whatever y want; that won't bother me at all!

So basically, i can make it explode as a dash (-), like i posted in the other reply, and i will upload the matches like that :)

 

The other code i just don't understand it.. im not that expert on php (not even close!).

 

Im trying to get some help from a friend, see what we can do.

 

Thanks for all your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.