newbtophp Posted July 26, 2009 Share Posted July 26, 2009 I've used Crayon Violents tutorial, and got to a point where I need help, im trying to remove the id column and add another column where I can select an option from a dropdown menu. <?php /**** Dealing with the database ****/ // connect to db $conn = mysql_connect('localhost','dbuserwebsite','dbpassword') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('dbwebsite',$conn) or trigger_error("SQL", E_USER_ERROR); // INSERT: if we have a website to add... if($_POST['website']) { // little bit of cleaning... $website = mysql_real_escape_string($_POST['website']); // insert new website into table $sql = "INSERT INTO info (id, website) VALUES ('','$website')"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end if // UPDATE: if we have website(s) to change... if($_POST['cwebsite']) { // for each website to change... foreach($_POST['cwebsite'] as $cid => $cwebsite) { // little bit of cleaning... $id = mysql_real_escape_string($cid); $website = mysql_real_escape_string($cwebsite); // update website in the table $sql = "UPDATE info SET website = '$website' WHERE id = '$id'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end foreach } // end if // DELETE: if we have a website to delete... if($_GET['website']) { // little bit of cleaning... $website = mysql_real_escape_string($_GET['website']); // delete website from table $sql = "DELETE FROM info WHERE website = '$website'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); } // end if // ORDERBY: if one of the links was clicked.. if ($_GET['orderby']) { // make an aray of allowed websites $allowed = array('id','website'); // bit of cleaning... $order = mysql_real_escape_string($_GET['orderby']); // is it a valid column website? yes: use it. no: default to 'id' $order = (in_array($order, $allowed))? $order : "id"; // if no link clicked, default to 'id' } else { $order = "id"; } // end else // SELECT: get the list of websites from database $sql = "SELECT id, website FROM info ORDER BY $order"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); /**** end deal with the database ****/ /**** list everything out ****/ // list columns echo <<<LISTCOLS <form action = '{$_SERVER['PHP_SELF']}' method = 'post'> <table border = '1'> <tr> <td><a href = '{$_SERVER['PHP_SELF']}?orderby=id'>ID</td> <td><a href = '{$_SERVER['PHP_SELF']}?orderby=website'>Website</td> <td>Delete</td> </tr> LISTCOLS; // loop through list of websites while ($list = mysql_fetch_assoc($result)) { echo <<<LISTINFO <tr> <td>{$list['id']}</td> <td><input type = 'text' website = 'cwebsite[{$list['id']}]' value = '{$list['website']}'> <td><a href = '{$_SERVER['PHP_SELF']}?website={$list['website']}'>delete</a></td> </tr> LISTINFO; } // end while // list input box for adding new entry echo <<<NEWENTRY <tr> <td bgcolor = 'gray'></td> <td><input type = 'text' website = 'website'></td> <td bgcolor = 'gray'></td> </tr><tr> <td></td> <td align = 'center'><input type = 'submit' value = 'submit'></td> <td></td> </tr> </table> </form> NEWENTRY; /**** end list everything out ****/ ?> Heres how it currently looks: Heres how im trying to get it too look: Can anyone help. Thanks Link to comment https://forums.phpfreaks.com/topic/167533-sql-query-help/ Share on other sites More sharing options...
GingerRobot Posted July 26, 2009 Share Posted July 26, 2009 Well why don't you have a go first? I'm sure you can work out how to prevent the ID from being displayed. And i'm sure you can attempt to output a select box in the loop. There's not much point in following a tutorial and not attempting to alter it to your needs. If you get stuck with something or you get errors you don't understand, then post back here and i'm sure people will help. You might have some troubles getting the form to work properly, but once you've got it displaying correctly, we can deal with that. Link to comment https://forums.phpfreaks.com/topic/167533-sql-query-help/#findComment-883467 Share on other sites More sharing options...
newbtophp Posted July 26, 2009 Author Share Posted July 26, 2009 Well why don't you have a go first? I'm sure you can work out how to prevent the ID from being displayed. And i'm sure you can attempt to output a select box in the loop. There's not much point in following a tutorial and not attempting to alter it to your needs. If you get stuck with something or you get errors you don't understand, then post back here and i'm sure people will help. You might have some troubles getting the form to work properly, but once you've got it displaying correctly, we can deal with that. Every post I make here, I always try before I ask, I do never abuse the knowledge of the gurus/recommend's. I started one from scratch at: http://www.phpfreaks.com/forums/index.php/topic,259286.msg1225881.html#msg1225881 But then I got stuck, so Crayon Violent suggested me his tutorial so I literally restarted and used his code, but Im unfamilar with the code, so theirfore I asked for help. I tried modifying this code, but Im back to the same point as I was when I originally started my own code. I cant get the drop down options to work with the submit. Link to comment https://forums.phpfreaks.com/topic/167533-sql-query-help/#findComment-883475 Share on other sites More sharing options...
abazoskib Posted July 26, 2009 Share Posted July 26, 2009 newbtophp, i didnt look through the code above, but basically you want to alter the display of your table and add a combo box for a column? check out a tutorial on input types for html. Link to comment https://forums.phpfreaks.com/topic/167533-sql-query-help/#findComment-883493 Share on other sites More sharing options...
GingerRobot Posted July 26, 2009 Share Posted July 26, 2009 I cant get the drop down options to work with the submit. So the issue isn't with the display of the above? It's more with using multiple select boxes? May we see the code that you have problems in this area with? I suspect what you need to do is name the select boxes as an array and loop through the results when you process the form (a process similar to which, albeit using checkboxes, can be found here). I might be wrong, however, which is why we're going to need to see some code. Link to comment https://forums.phpfreaks.com/topic/167533-sql-query-help/#findComment-883527 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.