Jump to content

Adding Radio Buttons To A Mysql Database List


Ron916

Recommended Posts

Hello ya'll, first post. Please be gentle :o Learning by creating myphpAdmin type page functions, still have a long way to go. Lots of cut/paste and modifications so far.

 

Here I retrieve a list of databases, then add radio buttons, can anyone tell me where I'm going wrong? Am I doing it all wrong?

 

<?php

$con = mysql_connect($dbhost,$dbuser,$dbpass);

$set = mysql_query('SHOW DATABASES',$con);

$dbs = array();

while($db = mysql_fetch_row($set))

$dbs[] = $db[0];

 

echo '<form action="deletedb.php" method="post">';

echo '<input name=$db type=radio value=$db>';

echo implode('<tr><td><input type="radio" name="<?php echo $dbs; ?>" value="<?php echo $dbs; ?>"</td></tr>' , $dbs) . "<br>";

echo '<input type="submit">';

echo '</form>';

?>

 

It does retrive the DB list. And it does put the radio buttons next to each one, in a horizontal line, I'd prefer a verticle list but that's not so important right now.

 

I can't seem to get $_POST['xxx'] to work in deletedb.php (using echo to test with), I've tried name and value but it's not sending anything over, I copied the $set, $dbs, while and echo implode lines from the web and messed around til I had something resembling a table with radio buttons.

 

ANY comments on this code, what's wrong or what could be better are encouraged and greatly appreciated!

 

Thanks!

Link to comment
Share on other sites

echo '<input name=$db type=radio value=$db>';

Variables within single quotes are not parsed. You are literally creating an input with the name '$db' and a value of '$db'.

 

do a print_r($_POST) and you'll see. Or view the rendered source.

Edited by Jessica
Link to comment
Share on other sites

Not to mention the fact that you are putting code within code:

echo implode('<tr><td><input type="radio" name="<?php echo $dbs; ?>" value="<?php echo $dbs; ?>"</td></tr>' , $dbs) . "<br>";

 

That line of code makes no sense. Plus, there is no closing > for the input field.

 

If I think I understand what you are trying to accomplish, you need to do a foreach() loop

foreach($dbs as $db)
{
   echo "<tr><td><input type='radio' name='{$db}' value='{$db}'></td></tr>\n";
}

Edited by Psycho
Link to comment
Share on other sites

In addition to what Jessica and Psycho have said, when you do this $dbs[] = $db[0];, you're pushing the value in to a new array element of $dbds, which means when you fix the quoting, this name="<?php echo $dbs; ?>" value="<?php echo $dbs; ?>" will end up literally being name="Array" value="Array" in the html source.

Edited by Pikachu2000
Link to comment
Share on other sites

Hi again, I used the foreach() loop thusly:

 

 

foreach($dbs as $db)
{	  
echo "<br><tr><td><input type='radio' name='{$db}' value='{$db}'>'{$db}'</td></tr>\n";
}

 

Hitting submit takes me to the 2nd php page which then runs a print_r($_POST):

Array ( [asdf] => asdf [submit] => Submit Query )

asdf is the data I need, how to I cut out the rest O.O

 

 

 

edit: attempting to add code tags

Edited by Ron916
Link to comment
Share on other sites

Groovy. I got it working. Ty. If anyone would like to make comments on the final code please feel free, I'd love the input.

 

index.php (lots of random html stuff I left out)

<html>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'fakeuser';
$dbpass = 'fakepass';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
$set = mysql_query('SHOW DATABASES',$con);
$dbs = array();
while($db = mysql_fetch_row($set))
$dbs[] = $db[0];
echo "<form name=\"form1\"action=\"deletedb.php\" method=\"post\">";

foreach($dbs as $db)
{	
echo "<br><tr><td><input type='radio' name={$db} value={$db}>{$db}</td></tr>\n";
}

echo "<br><br><input type=submit value=DeleteDB>";
echo "</form>";
?>
</body>
</html>

deletedb.php

<html>
<body>
$stuff = array_values($_POST);
$dbname=$stuff[0];
$dbhost = 'localhost';
$dbuser = 'fakeuser';
$dbpass = 'fakepass';

$con = mysql_connect($dbhost,$dbuser,$dbpass);
echo "Deleting database: " . $dbname . "<br><br>";
if (mysql_query("DROP DATABASE $dbname",$con))
{
echo "Database dropped, ur data's gone!" . "<br>";
}
else
{
echo "Error deleting database: " . mysql_error() . "<br>";
}

?>
</body>
</html>

Edited by Ron916
Link to comment
Share on other sites

1. Separate your logic (PHP) from the output (HTML). Out all the core PHP logic at the top of the page and create variable of dynamic output. Then just echo those in the HTML.

 

2. You have invalid HTML code, the output of the radio buttons has surrounding table row and cell tags, but it is not within a table.

 

3. Your radio button options names don't make sense. All radio buttons in a group should have the same name. Yours all have unique names. So, they won't work correctly.

 

4. Put the DB connection code into a separate file and include in the pages where you need it rather than copying it into multiple pages

 

 

db_connect.php

<?php
$dbhost = 'localhost';
$dbuser = 'fakeuser';
$dbpass = 'fakepass';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
?>

 

index.php

<?php

//Connect to DB
include('db_connect.php');

$set = mysql_query('SHOW DATABASES',$con);
$dbs = array();
while($db = mysql_fetch_row($set))
{
$table = $db[0];
$tableButtons .= "<input type='radio' name='table' value={$table}>{$table}<br>\n";
}

?>
<html>
<body>
<form name="form1"action="deletedb.php" method="post">
Select a table:<br>
<?php echo $tableButtons; ?>
<br><br>
<input type="submit" value="DeleteDB">
</form>
</body>
</html>

 

deletedb.php

<?php

//Connect to DB
include('db_connect.php');

$dbName = trim($_POST['table']);
$query = "DROP DATABASE $dbname";
$result = mysql_query("DROP DATABASE $dbname",$con);
if($result)
{
$message = "Database dropped, ur data's gone!" . "<br>";
}
else
{
$message .= "Error deleting database: " . mysql_error() . "<br>";
}

?>
<html>
<body>
Deleting database: '<?php echo $dbName; ?>'
<br><br>
<?php echo $message; ?>
</body>
</html>

 

However, I've left off a lot of validation logic I would add to prevent errors.

Edited by Psycho
Link to comment
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.