Jump to content

List as input to DB


adm83

Recommended Posts

Hello All, i am trying to figerout how can i create a list of options that the user can input the values to as many lines as he wants and then getting the value of each row and inserting it to my DB (mysql)

 

i havent found anyway to make this kind of list that is "open" for edit and is not limited...

Can anyone suggest anything ?

Link to comment
Share on other sites

Are you referring to something like this?

 

Notice the form input names. You can have a page that generates fields with a drop down list of numbers.

 

<?php

// database variables
$host = "localhost";
$user = "user";
$pass = "pass";

//database connection
$conn = mysql_connect($host, $user, $pass);
mysql_select_db("database", $conn);

//check form submitted
if (isset($_POST['submit']))
{
//check missing user input
foreach($_POST['formvalue'] as $value)
{
	if(!isset($value) || $value == "")
	{
	 	$emptyvalue = TRUE;
	}
}
if ($emptyvalue)
{
	//view if missing user input
	$form = "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">
	Value: <input type=\"text\" name=\"formvalue[]\" /><br />
	Value: <input type=\"text\" name=\"formvalue[]\" /><br />
	Value: <input type=\"text\" name=\"formvalue[]\" /><br />
	<input type=\"submit\" name=\"submit\" value=\"Submit\" />
	</form>";

	echo $form;
	exit();
}
}	
else 
{
//view if no submit
$form = "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">
Value: <input type=\"text\" name=\"formvalue[]\" /><br />
Value: <input type=\"text\" name=\"formvalue[]\" /><br />
Value: <input type=\"text\" name=\"formvalue[]\" /><br />
<input type=\"submit\" name=\"submit\" value=\"Submit\" />
</form>";

echo $form;
exit();
}

//step through post array and make safe for sql
foreach($_POST['formvalue'] as $value)
{
$sqlsafe[] = '("' . mysql_real_escape_string($value) . '")';
}

// build query
$query = "INSERT INTO userinput (formvalue) VALUES " . implode(",", $sqlsafe);

// insert to database or error
if(!mysql_query($query,$conn))
{
die('Error: ' . mysql_error());
}

// debugging
echo $query;

?>

 

Then your form generation page could be something like:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select style="width:225px" name="formfields">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select><br /><br />
<input type="submit" value="Submit" />
</form>

 

You will have to link the 2 pages together with the appropriate coding if this is what you're trying to achieve.

 

Link to comment
Share on other sites

Hi, thank you for your reply... :)

I tried getting it to work but i couldn't really figerout how it builds up the query , i got the connections working but i cant understand this line :

$query = "INSERT INTO tbl_clients (formvalue) Calias " . " . (the tbl_clients  is the table but and the Calias  is the column )

but anyways this is not what i am looking for actually , - i was hoping to find a way to create a list box object (of some sort) not just a textfield the reason i need a list is because i dont know hot many items the client will input , so if i have a list he can input ,say, 50 rows and then i want to submit each row as a new row to the DB..

Do you think this can be done ?

 

thank :)

Link to comment
Share on other sites

Let me rephrase my above post to suit your needs.

 

This line:

$query = "INSERT INTO tbl_clients (Calias) VALUES " . implode(",", $sqlsafe);

 

outputs this:

INSERT INTO tbl_clients (Calias) VALUES ("value1_from_field_1"),("value2_from_field_2"),("value3_from_field_3"),etc,etc,etc....  to how ever many items your user needs.

Link to comment
Share on other sites

Here is what I believe you are looking for.

 

Please note: The following code is a rough draft and meant as a guide. Not for any production site.

 

<?php

// database variables
$host = "localhost";
$user = "user";
$pass = "pass";

//database connection
$conn = mysql_connect($host, $user, $pass);
mysql_select_db("database", $conn);

// create form to get number of fields
if (!isset($_POST['submit']) && !isset($_POST['formfields']))
{
$form = '<h2>Choose Number Of Items</h2><form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<select style="width:225px" name="formfields">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select><br /><br />
<input type="submit" name="submit" value="Submit" />
</form>';
}

// generate inputs if user chose number of items
if (isset($_POST['submit']) && $_POST['formfields'] != '')
{
$items = $_POST['formfields'];
$form = "<h2>Enter Your Items</h2>
	     <form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">";
	     for($in=1;$in<=$items;$in++){
		 if ($in >= 11){
			break;
		 }
		$form .= "<p>Item</p>"
	             . $in . ": <input style=\"width:200px\" type=\"text\" name=\"formvalue[]\" /><br /><br />";
		 } 
$form .= "<input type=\"hidden\" name=\"formfields\" value=\"" . $_POST['formfields'] . "\" />
		   <input type=\"submit\" name=\"submit\" value=\"Submit\" /></form>";
}

//check form items submitted
if (isset($_POST['submit']) && isset($_POST['formvalue']))
{
//check missing user input
foreach($_POST['formvalue'] as $value)
{
	if(!isset($value) || $value == "")
	{
	 	$emptyvalue = TRUE;
	}
}
if ($emptyvalue)
{
	//view if missing user input
	echo $form;
	exit();
}
}	
else 
{
//view if no submit
echo $form;
exit();
}

//step through post array and make safe for sql
foreach($_POST['formvalue'] as $value)
{
$sqlsafe[] = '("' . mysql_real_escape_string($value) . '")';
}

// build query
$query = "INSERT INTO tbl_clients (Calias) VALUES " . implode(",", $sqlsafe);

// insert to database or error
if(!mysql_query($query,$conn))
{
die('Error: ' . mysql_error());
}

// debugging
echo $query;

?>

 

Please note: Depending on how many items you are allowing users to choose, in the for loop "$in >= 11" must be adjusted.  The current value is for 10 items.

Link to comment
Share on other sites

Wow! THANK! that works grate ! :))

 

Just to make things a bit harder if possible :)

i can do with the allow user to set how many items but isnt there a way to create a listbox to do this (so he wont have to count and then set how many item) when thinking about java of something there is a listbox object that the user can input the data in as many rows and then i can loop though the number of rows..

just wounding if there is no way to do something like that in this environment ...

 

Thanks again !

Link to comment
Share on other sites

Noted, Tnx... :)

 

Just repeating the question a bit..

is there a way to create an "empty" list the the user can input as many rows of data into them and then looping though the size of the list and submitting them (the summiting them part is already in the code that was written here 2 posts ago so.. )

:confused:

Link to comment
Share on other sites

I mean creating a listbox (list option select) that the options are not defined, and the user can click on the first row and input sometext, and if he wishs click on the 2nd row and input another text and so on as many times as he wants , so the list size is not defined and not filed with any text at the beginning..

 

Link to comment
Share on other sites

Ji just want to get back to the original topic...

 

is there a way to create a list the a user can input data into it.. ? and for that matter is there a way of crating a grid table that the user can input informtion into (like excel sheet) ??? :confused:

Link to comment
Share on other sites

You can allow the user to input text into a textarea separated by a delimiter such as the | pipe character, but you'd have to trust that the user will actually put the delimiter in the text.

 

You can use a form that has text fields that the user can enter one value per field, and give them the option to specify how many more fields they'd like to add to the form.

 

You can let them input dimensions for a grid type form, then generate the form from that input.

 

If you allow the user to specify things, you definitely need to establish limits on the numbers they can input.

Link to comment
Share on other sites

Well, I am starting to realize there is no way to make the list object.. :-Z i just want it because i think its more UI friendly but since the field of the value is of known length i think i just might use the textbox idea...

is there any way of getting some code on how to do this? since i am new to php ...

so the question is now, how can i get the length of the text field and assuming the value lenght is 3 getting an array of values and submitting them to the db...

 

Thank you for all your help so far.. it is greatly appreciated.,.

Link to comment
Share on other sites

If the length of the input is variable, you're probably better off to use a <textarea> field. If all of the values are separated by spaces, you can explode() the string on the space, and then trim() the resultant array elements to end up with the data. You may also want to check that the length of the string in each element is actually 3 characters as well, if that's a requirement. Here's an example.

 

<?php
$_POST['textarea'] = implode(' ', range(95, 150)); // sample data
$_POST['textarea'] .= ' '  . implode(' ', range(995, 1050)); // sample data

if( !empty($_POST['textarea']) ) {
     $values = array_map('trim', explode(' ', $_POST['textarea']));
     foreach( $values as $key => $val ) {
     	if( strlen($val) !== 3 ) {
     		unset($values[$key]);
     	}
     }
}

echo '<pre>';
print_r($values);
echo '</pre>';
?>

 

The above returns an array containing values 100-150 and 995-999.

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.