Jump to content


Photo

need help quick


  • Please log in to reply
13 replies to this topic

#1 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 02 April 2006 - 08:48 PM

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>

#2 Javizy

Javizy
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 02 April 2006 - 09:58 PM

[!--quoteo(post=360970:date=Apr 2 2006, 09:48 PM:name=desithugg)--][div class=\'quotetop\']QUOTE(desithugg @ Apr 2 2006, 09:48 PM) View Post[/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:

/* 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 . ");";


#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 02 April 2006 - 10:07 PM

Really? Why wouldn't PHP put this into an array?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 02 April 2006 - 10:08 PM

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:

<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>

Then in your PHP code:

$id_array = $_POST['ids'];
foreach ($id_array as $id) {
   ...INSERT a record
}


#5 Javizy

Javizy
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 02 April 2006 - 11:04 PM

Sorry ignore my post, I was explaining how to use single select boxes.

#6 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 03 April 2006 - 12:28 AM

[!--quoteo(post=361022:date=Apr 2 2006, 07:04 PM:name=Javizy)--][div class=\'quotetop\']QUOTE(Javizy @ Apr 2 2006, 07:04 PM) View Post[/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

#7 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 03 April 2006 - 12:43 AM

[!--quoteo(post=361022:date=Apr 2 2006, 07:04 PM:name=Javizy)--][div class=\'quotetop\']QUOTE(Javizy @ Apr 2 2006, 07:04 PM) View Post[/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
<?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>

Action:
<?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']."')");
}
?>


#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 April 2006 - 09:34 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#9 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 03 April 2006 - 04:43 PM

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:

<?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."')");
?>


#10 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 April 2006 - 06:40 PM

I always get explode and implode mixed up -- I much prefer Perl's join/split names.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#11 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 03 April 2006 - 07:50 PM

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



#12 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 03 April 2006 - 09:42 PM

[!--quoteo(post=361298:date=Apr 3 2006, 01:40 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Apr 3 2006, 01:40 PM) View Post[/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.

#13 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 03 April 2006 - 09:44 PM

[!--quoteo(post=361359:date=Apr 3 2006, 05:42 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Apr 3 2006, 05:42 PM) View Post[/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

#14 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 April 2006 - 03:48 AM

Not old, just experienced. BTW, I think they've aliased the join() function in PHP as well, finally.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users