Jump to content

Archived

This topic is now archived and is closed to further replies.

desithugg

need help quick

Recommended Posts

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>

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
Really? Why wouldn't PHP put this into an array?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sorry ignore my post, I was explaining how to use single select boxes.

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
That's because your running a foreach loop -- if I remember correctly, you should be probably be using explode() to join your array elements together and simply INSERT a single record instead.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
I always get explode and implode mixed up -- I much prefer Perl's join/split names.

Share this post


Link to post
Share on other sites
k thnx guys if figured it out and seems to have worked

it inserts the data correctly just need a lil help vieing it than im done

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Not old, just experienced. BTW, I think they've aliased the join() function in PHP as well, finally.

Share this post


Link to post
Share on other sites

×

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.