how to save multi-line input...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vijayragvan
Forum Newbie
Posts: 2
Joined: Sat Aug 26, 2006 4:59 am
Location: Chennai, India

how to save multi-line input...

Post by vijayragvan »

Hi Friends,

I am trying to develop a site for my company's field people wherein they can enter the product wise sales made each month. I am using php and mysql. A form will be displayed which will accept the salesman code and a table of product names (from the product master table). The user has to enter only the sales quantities against each product name. I want to save this data into a seperate table. I want to know how to load the user entries into variables so that I can call another php file and save them in the table. I have designed the form for input. I have the problem in saving them in to the table. I am unable to do this since the input is multi-line and can grow based on the no. of records in the product master.

Pl. help.

With regards,
R.Vijay
INDIA.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Your post is very abstract. Please post:
  • Your existing PHP code
  • Your existing MySQL table structure
  • Explaination of what you are trying to achieve which refers to existing table structure and PHP code.
I recon the solution will be very simple but I'm having difficulty understanding what the actual problem is.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The easiest would be to generate a form with input as following: (assuming you have an array with product name/id pairs)

Code: Select all

foreach($products as $product_name => $product_id) {
<label>$product_name:</label><input type='text' name='product_$product_id'/>
}
So when the form is posted back you can easily iterate over the $_POST keys that start with 'product_'...

Code: Select all

foreach(array_keys($_POST) as $key) {
  if (substr(0, strlen('product_'), $key) == 'product_') {
     $product_id = substr(strlen('product_') -1);
     $quantity = (int) $_POST[$key];

     $sql = 'INSERT INTO sales (product_id, qty) VALUES (' . mysql_real_escape_string($product_id) . ', ' . mysql_real_escape_string($quantity) . ');';
  }
}
vijayragvan
Forum Newbie
Posts: 2
Joined: Sat Aug 26, 2006 4:59 am
Location: Chennai, India

Post by vijayragvan »

JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="ole"]Your post is very abstract. Please post:[list][*]Your existing PHP code
[*]Your existing MySQL table structure
[*]Explaination of what you are trying to achieve which refers to existing table structure and PHP code.[/list]

I recon the solution will be very simple but I'm having difficulty understanding what the actual problem is.[/quote]

Thanks for trying to help me.

I have posted the complete of the two php forms.

=======================

salesform.php

Code: Select all

<html>
<head>
<title>Untitled</title>
</head>
<body>

<?


$username="root";
$password="admin";
$database="secsales"


?>

<br>
<br>
<br>
<br>
<br>

<form method="post" action="salesadd.php" target="_blank"> 

<?
@mysql_connect("localhost",$username,$password);

if (!@mysql_select_db('secsales'))
{
die('<p>Unable to locate the database at this time.</p>');
}

$result = @mysql_query('SELECT * FROM prodmast');


if (!$result)
{
die('<p>Error performing query:'.mysql_error().'</p>');
}

echo('<Table>');

?>

<tr>
<td>FSE Employee Code :</td>
<td><input type="text" name="fseempcode" size=7></td>
</tr>
<tr>
<td>REP Employee Code :</td>
<td><input type="text" name="repempcode" size=7></td>
</tr>
<tr>
<td>Month / Year :</td>
<td><input type="text" name="mmyy" size=7></td>
</tr>

<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>


<?

while ($row = mysql_fetch_array($result)){

echo('<tr>');

echo('<td>' . '<input type="text" name="pcode" size=8 value="' . $row['prodcode'] . '">' . '</td>');
echo('<td>' . '<input disabled type="text" name="pname" size=35 value="' . $row['descr'] . '">' . '</td>');
echo('<td>' . '<input type="text" name="qty" size=8>' . '</td>');
echo('</tr>');

}
mysql_close();

?>

<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<td></td>
<td align="center"><INPUT type="submit" value="Save"></td></tr>

</Table>

</body>
</html>
salesadd.php

Code: Select all

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

<html>
<head>
<title>Untitled</title>
</head>
<body>

<?php 

$username="root";
$password="admin";
$database="secsales";

@mysql_connect("localhost",$username,$password);

if (!@mysql_select_db('secsales')){
die('<p>Unable to locate the employee database at this time.</p>');
}

$fseempcode= $_POST['fseempcode'];
$repempcode= $_POST['repempcode'];
$mmyy= $_POST['mmyy'];
$pcode = $_POST['pcode']; 
$pqty = $_POST['qty'];


$result = @mysql_query( "INSERT INTO SALETRAN VALUES('$fseempcode','$repempcode','$mmyy','$pcode','$pqty')" );

if (!$result){
die('<p>Error performing query:'.mysql_error().'</p>');
}

mysql_close();

?>
<br>
<br>
<p>Entry Saved ...</p>
<br>
<br>
<a href="http://localhost/salesform.php">Back to Form</a>

</body>
</html>

JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

while ($row = mysql_fetch_array($result)){

echo('<tr>');

echo('<td>' . '<input type="text" name="pcode" size=8 value="' . $row['prodcode'] . '">' . '</td>');
echo('<td>' . '<input disabled type="text" name="pname" size=35 value="' . $row['descr'] . '">' . '</td>');
echo('<td>' . '<input type="text" name="qty" size=8>' . '</td>');
echo('</tr>');

}
mysql_close();
That code is generating three fields with the same name="" over and over. The names need to be unique. You can do this:

Code: Select all

for ($i=0; $row = mysql_fetch_array($result); $i++) {
echo '<tr>
<td><input type="text" name="pcode[' . $i . ']" size=8 value="' . $row['prodcode'] . '"></td>
<td><input disabled type="text" name="pname[' . $i . ']" size=35 value="' . $row['descr'] . '"></td>
<td><input type="text" name="qty[' $i . ']" size=8></td>
</tr>';
}
Stick a print_r($_POST) at the top of your file if you don't understand what this does.
Post Reply