Ron916 Posted October 12, 2012 Share Posted October 12, 2012 Hello ya'll, first post. Please be gentle 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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Pikachu2000 Quote Link to comment Share on other sites More sharing options...
Ron916 Posted October 12, 2012 Author Share Posted October 12, 2012 <3 ! Thanks folks, sorry for my frankenstein code! I'm giving the foreach() loop a try. Quote Link to comment Share on other sites More sharing options...
Ron916 Posted October 12, 2012 Author Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Ron916 Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 12, 2012 Share Posted October 12, 2012 Please use code tags. You already know how to access data in an array, you've done it before. $_POST is an array. Quote Link to comment Share on other sites More sharing options...
Ron916 Posted October 12, 2012 Author Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Ron916 Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 12, 2012 Share Posted October 12, 2012 (edited) 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 October 12, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
Ron916 Posted October 12, 2012 Author Share Posted October 12, 2012 amg awesome, I knew there were better ways, thank you all I got a lot of good info from comparing my code, and even ended up with a lesson in concatenating(??)... ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.