Jump to content

Recommended Posts

[!--quoteo(post=360970:date=Apr 2 2006, 09:48 PM:name=desithugg)--][div class=\'quotetop\']QUOTE(desithugg @ Apr 2 2006, 09:48 PM) [snapback]360970[/snapback][/div][div class=\'quotemain\'][!--quotec--]
inserting data into db from a multiple option form like
<form action="trade1.php" method="post" multiple='multiple'>
<select name="ids1" multiple="multiple" size="20">
<option value='"1' selected>number 1</option>
<option value='2' selected>number 2</option>
</select>
</form>
[/quote]

Even though it has multiple options, only one name/value pair will be sent to trade1.php.

So simply you would do:

[code]/* Get value */
$ids1 = $_POST['ids1'];

/* Insert into DB */
$conn = mysql_connect($dbHost, $user, $password);
mysql_select_db($database) or die("Unable to select DB " . $database);
query("INSERT INTO myTable (isd1) VALUES ( " . $ids1 . ");";[/code]
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23251
Share on other sites

It's really difficult to help you without a more descriptive explanation of the problem.

In response to what Javizy posted, if you're using PHP you need to name your select field "ids[]" in order to get the data back out on the PHP end. For instance:

[code]<select name="ids[]" multiple="multiple" size=4>
<option value="1">Number 1</option>
<option value="2">Number 2</option>
<option value="3">Number 3</option>
<option value="4">Number 4</option>
</select>[/code]

Then in your PHP code:

[code]$id_array = $_POST['ids'];
foreach ($id_array as $id) {
   ...INSERT a record
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23259
Share on other sites

[!--quoteo(post=361022:date=Apr 2 2006, 07:04 PM:name=Javizy)--][div class=\'quotetop\']QUOTE(Javizy @ Apr 2 2006, 07:04 PM) [snapback]361022[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Sorry ignore my post, I was explaining how to use single select boxes.
[/quote]

kk thnx alot i think this really helps i was wondering why alot of the forms i looked at had that [] thing
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23311
Share on other sites

[!--quoteo(post=361022:date=Apr 2 2006, 07:04 PM:name=Javizy)--][div class=\'quotetop\']QUOTE(Javizy @ Apr 2 2006, 07:04 PM) [snapback]361022[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Sorry ignore my post, I was explaining how to use single select boxes.
[/quote]

umm back
it seems to insert the data but it inserts multiple rows like if i select 4 options it adds for rows in the db not 4 options in the same row and it inserts "array" as every value in the db

this is what it looks like
FORM
[code]<?PHP
$reqlevel = 1;
include("membersonly.inc.php");?>
<form action="trades2.php" method="POST">
<select name="ids[]" multiple="multiple" size=4>
<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('tpf');
if (!$db_selected) {
   die('Could not select database: ' . mysql_error());
}
$query = "SELECT pokemon,poke_id,level FROM pokemon WHERE trainer = 'desithugg'";
$result = mysql_query($query);
if (!$result) {
   die('Query failed: ' . mysql_error());
}
/* fetch rows in reverse order */
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
   if (!mysql_data_seek($result, $i)) {
       echo "Cannot seek to row $i: " . mysql_error() . "\n";
       continue;
   }

   if (!($row = mysql_fetch_assoc($result))) {
       continue;
   }

   echo " <option value='". $row['poke_id'] ."' selected>". $row['pokemon'] ."(Level:". $row['level'] .")</option>";
}

mysql_free_result($result);
?></select>
<tr><td colspan="2" align="center">
<input type="submit" name="submit" value="View Stats">
</td></tr>
</form>[/code]

[b]Action:[/b]
[code]<?PHP
$reqlevel = 1;
include("membersonly.inc.php");?>
<?php
$link = mysql_connect('localhost', 'username', 'pass');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
mysql_select_db('tpf');
$id_array = $_POST['ids'];
foreach ($id_array as $id) {
mysql_query("INSERT INTO trade (ids1) values ('".$_POST['ids']."')");
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23312
Share on other sites

I just wrote that as an example, I didn't mean for you to paste it in. It sounds like you want to do something like this:

[code]<?PHP
$reqlevel = 1;
include("membersonly.inc.php");?>
<?php
$link = mysql_connect('localhost', 'username', 'pass');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
mysql_select_db('tpf');
$id_array = $_POST['ids'];
$ids = implode(',',$id_array);
mysql_query("INSERT INTO trade (ids1) values ('".$ids."')");
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23516
Share on other sites

[!--quoteo(post=361298:date=Apr 3 2006, 01:40 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Apr 3 2006, 01:40 PM) [snapback]361298[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I always get explode and implode mixed up -- I much prefer Perl's join/split names.
[/quote]

Yeah, I wrote Perl for about 7 years, PHP for going on 1, I find myself typing join() instead of implode() all the time. It just makes more sense.
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23617
Share on other sites

[!--quoteo(post=361359:date=Apr 3 2006, 05:42 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Apr 3 2006, 05:42 PM) [snapback]361359[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Yeah, I wrote Perl for about 7 years, PHP for going on 1, I find myself typing join() instead of implode() all the time. It just makes more sense.
[/quote]
wow you guys are old im only 14 and have been around web languages for about 1 month
Link to comment
https://forums.phpfreaks.com/topic/6412-need-help-quick/#findComment-23619
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.