Jump to content

[SOLVED] How to insert multiple lines into db using a text field?


BadGoat

Recommended Posts

Hello!

 

Looking for pointers in how to enter comma separated lines of text into a db using a text field? The ideal would be to have a text box which I can paste large lumps of data into which would be inserted into a db line by line.. I don't know which concept I should be learning to do such a thing. Any guidance is much appreciated!

 

(example of data which could be pasted into  a text field for db insertion)

200005, Vegetable, Corn

200006, Vegetable, Peas

300001, Fruit, Apple

300002, Fruit, Banana

400004, Meat, Mutton

400005, Meat, Turkey

Link to comment
Share on other sites

here's how I did it in one of my scripts:

$input = $_POST['contacts'];
$array = explode("\n", $input);
foreach ($array as $value){
//input each line into your db
}

this is a slimmed down version of my code, but you get the idea, and if you don't, just ask!

 

Link to comment
Share on other sites

modified version:

<?php
$input = $_POST['contacts']; //your post data here.
$array = explode("\n", $input);

foreach ($array as $value){
if ($value != null && $value != "" && $value != " " && $value != "\n" && $value != "\t" && $value != "\r"){
	$array2 = explode(",", $value);
	$item1 = mysql_real_escape_string($array2[0]);
	$item2 = mysql_real_escape_string($array2[1]);
	$item3 = mysql_real_escape_string($array2[2]);
	//and so on, until you have each comma dillemidated (I can't spell) item in it's own variable
	$sql = "INSERT INTO `table_name`('$item1', '$item2', '$item3');";
	mysql_query($sql) or die("Query failed: ". mysql_error());
}
}
?>

Link to comment
Share on other sites

Thank you for the replies! I've been reading on the PHP site about EXPLODE and I see (in theory) how to use it.. And I see in your example code how it is done, so I think that I can play around with it. One question that comes to mind early on though.. As the field that I paste the data into is a textarea field, would I need to do something like $text = nl2br($text); to preserve the line breaks between each row of data I am entering?

Link to comment
Share on other sites

ok, I started small, to  make sure I had it figured out before expanding it, and I am getting an error.. On the page where I can paste in the data to the textfield I type 1,1 and submit it. The error I get:

 

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1', '1')' at line 1 Actual query: INSERT INTO `etest`('1', '1')

 

Here's the code from the second page:

<?PHP

include  ("../config.php");

echo'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title></title>
</head>
<body>';

$input = $_POST['t_data']; //your post data here.
$array = explode("\n", $input);

foreach ($array as $value){
if ($value != null && $value != "" && $value != " " && $value != "\n" && $value != "\t" && $value != "\r"){
	$array2 = explode(",", $value);
	$cola = mysql_real_escape_string($array2[0]);
	$colb = mysql_real_escape_string($array2[1]);

        $sql = "INSERT INTO `etest`('$cola', '$colb')";
        mysql_query($sql) or die("Query failed: " . mysql_error() . " Actual query: " . $sql);
}
}
echo $input;
?>

 

Is there anything glaring as to why it errors out?

Link to comment
Share on other sites

OK, been tinkering all day and night and still not having any luck.. I have tried:

 

$sql = "INSERT INTO `etest`('$cola', '$colb') VALUES ('$cola', '$colb')";   

 

and

 

$sql = "INSERT INTO `etest`('$cola', '$colb')";   

 

and

 

$sql = "INSERT INTO `etest`('$cola', '$colb') VALUES ('$array2[0]', '$array2[1]')";   

 

and various other attempts at getting the two numbers posted into the db.. The goal is to take the data from the first page, separate by comma and then insert into the data into the db. Help ???

Link to comment
Share on other sites

Yay! I fixed it.. Here's what works! (was a problem with the syntax of the MySQL query)

 

Thank you kindly for pointing me in the right direction :)

<?PHP

include  ("../config.php");

echo'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title></title>
<link rel=stylesheet href="../eve.css" type="text/css">
</head>
<body>';

$input = $_POST['t_data']; //your post data here.
$array = explode("\n", $input);

foreach ($array as $value){
    if ($value != null && $value != "" && $value != " " && $value != "\n" && $value != "\t" && $value != "\r"){
	$array2 = explode(",", $value);
	$cola = mysql_real_escape_string($array2[0]);
	$colb = mysql_real_escape_string($array2[1]);

        echo 'column a is '.$cola.'<br />';
        echo 'column b is '.$colb.'<br />';
        $sql = "INSERT INTO etest VALUES ('$cola', '$colb')";
        mysql_query($sql) or die("Query failed: " . mysql_error() . " Actual query: " . $sql);
    }
    }
?>

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.