6 Replies - 677 Views - Last Post: 05 October 2012 - 02:21 AM Rate Topic: *---- 2 Votes

#1 Pbpixels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Giving a variable to a <select> inside a while loop

Posted 04 October 2012 - 02:06 PM

Hi all, I have troubles declaring values inside a <select> option.
I want it to select an email from a list created from a cmslogin database and then delete it after a push on the button.
I feel like trying everything but I somehow can't get it to work.
My code goes like this:

<?php
  
  require_once('connectvars.php');
  
  //Connects to your Database 
 require_once('connectvars2.php');
 $data = mysql_query("SELECT * FROM cmslogin") 
 or die(mysql_error());
 
 $select = $_POST['select'];

  if (isset($_POST['execute'])) {
  
 $id = $_POST['id'];
 $email = $_POST['email'];
 
 $data = "DELETE FROM cmslogin WHERE id = $id";
 $result = mysql_query($data);
 
  }
   //Connects to your Database
 require_once('connectvars2.php');
 $data = mysql_query("SELECT * FROM cmslogin")
 or die(mysql_error());
 
  echo '<form method="post">';
  echo '<select name="select">';
  
 while($row = mysql_fetch_array( $data )) {
 
 echo "<p><option value=" . $row['id'] . ">" . $row['email'] . "</option></p>";

 }
 
  echo '</select>';
  ?>
<input type="submit" name="execute" />
 </form>
<?php
}
?>


The connectvars contains my database login.
Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Giving a variable to a <select> inside a while loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6109
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: Giving a variable to a <select> inside a while loop

Posted 04 October 2012 - 02:21 PM

Quote

I somehow can't get it to work.


Does your computer melt down or explode?
Was This Post Helpful? 0
  • +
  • -

#3 Pbpixels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Re: Giving a variable to a <select> inside a while loop

Posted 04 October 2012 - 02:28 PM

View PostJackOfAllTrades, on 04 October 2012 - 02:21 PM, said:

Does your computer melt down or explode?


Relevant posts please?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6109
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: Giving a variable to a <select> inside a while loop

Posted 04 October 2012 - 02:30 PM

Something more than "I can't get it to work", please?

Read this

For example:

1. Is it giving you errors?
2. Is it giving you a blank screen?
3. If neither of these, what does the generated HTML look like?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: Giving a variable to a <select> inside a while loop

Posted 04 October 2012 - 07:37 PM

View PostPbpixels, on 04 October 2012 - 05:28 PM, said:

View PostJackOfAllTrades, on 04 October 2012 - 02:21 PM, said:

Does your computer melt down or explode?


Relevant posts please?

Pot, meet kettle.

Read the text that shows in the background when posting! It's there for a reason.
Was This Post Helpful? 0
  • +
  • -

#6 Pbpixels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Re: Giving a variable to a <select> inside a while loop

Posted 05 October 2012 - 01:43 AM

well, I thought I would make a short post since this problem should be rather simple to solve.
My problem is that as said, I can't declare a variable in my <option> html.
The reason for this script is that it should delete a user inside a database when a button is pressed, the values contains the id and the option contains the emails, but the $id doesn't get recognized once the button is pressed.

You don't get an error for this, $id just doesn't get a value when the button is pressed. Nothing more than that, nothing happens when you press the button.

"I can't get it to work" If I could make it work, I wouldn't be sitting here writing to you guys. It is hard to tell what my problem is when it doesn't give me any errors, hence I'm writing here.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 Pbpixels  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Re: Giving a variable to a <select> inside a while loop

Posted 05 October 2012 - 02:21 AM

Oh yeh got it working.
The only thing that had to be changed was:

 $data = "DELETE FROM pbcmslogin WHERE id = $id";

to
 $data = "DELETE FROM pbcmslogin WHERE id = $select";


Thanks for your help. lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1