Jump to content

Retrieving mySQL row information THEN using it in a query


andrewgarn

Recommended Posts

Hi

 

I'm trying to create a mySQL admin type page. The page needs to retrieve the values from a column in mySQL and place them into a drop down box.

 

The user then selects one of the values and clicks a button to run a query, which will then submit / delete etc

 

I have this so far:

 

<?
$result = mysql_query("select username from officials");

if (mysql_num_rows($result) > 0) {
  print "<select name=\"select_name\">";
  while ($row = mysql_fetch_array($result)) {
    print "<option value=\"" . $row['username'] . "\">" . $row['username'] . "</option>\n";
  }
print "</select>";
}
?>

 

This loads the usernames from the table and puts them into a drop down box.

 

But what I dont know how to do is then run a query?

 

Help would be appreciated :)

I'm not sure if what you're asking is this simple:

 

print "<form action=mysql.php method=post>";
$result = mysql_query("select username from officials");

if (mysql_num_rows($result) > 0) {
  print "<select name=\"select_name\">";
  while ($row = mysql_fetch_array($result)) {
    print "<option value=\"" . $row['username'] . "\">" . $row['username'] . "</option>\n";
  }
print "</select>";
}

print "<input type=submit>";
print "</form>";

Thank you for the quick reply

 

That added a submit query button (how does it know its submit query?) which would then presumably feed the selected variable to the next page?

 

How would i get the page to feed the variable back into itself and run the query without loading a page, ie if it was a select query return the results onto the screen into boxes which could be edited and saved back to the database?

 

I also want to have multiple drop down boxes leading to multiple queries so that the user can add delete / add entries as well as searching and modifying all on one page.

 

I was thinking of something like using divs, and a javascript hide/block feature to keep it simple?

The only way to get dynamic content without reloading the page is by using javascript. Otherwise, you could set the form action to the pagename.php and make it refresh using the new variable.

 

For multiple buttons, you can name them btnAdd, btnUpdate, btnDelete, then set the form action to an all-SQL php page that runs SQL based on the button pressed.

 

FormPage.php > SQLPage.php > FormPage.php(results)

 

and use header() to redirect them from the SQLPage.php.

So, the mysql.php the form was submitting to would have something like this on it?

 

$username = $_POST['username'];

 

then:

 

<?php

if (isset($_POST['submit'])) {

do something?

}

 

then how would it feed the information back to the original page?

 

The only current link I have between pages is a session check to see if the user has gone through the login page and not directly to the php

Actually, you can use AJAX for dynamic content too.

$username = $_POST['username'];

 

then:

 

<?php

if (isset($_POST['submit'])) {

do something?

}

You would want to check if the form is submitted before assigning a variable a value from the form.

Also how would I change the button name?

 

//print "<form action=\"$self\" method=\"post\">";
print "<form action=mysql.php method=post>";
$result = mysql_query("select username from officials");

if (mysql_num_rows($result) > 0) {
  print "<select name=\"select_name\">";
  while ($row = mysql_fetch_array($result)) {
    print "<option value=\"" . $row['username'] . "\">" . $row['username'] . "</option>\n";
  }
print "</select>";
}
print "<input type=submit>";
print "</form>";

Archived

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

×
×
  • 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.