gevo12321 Posted July 8, 2007 Share Posted July 8, 2007 ok so i have a page with an input on it: echo "<input type=\"text\" name=\"button"; echo $id; echo "\" value=\"\"/> ok and the page also has a submit button when u press it its supposed to put the text that u typed in in the input into a database in the row corresponding with the id number $id now in the other php page, the one that does the action, how would i post it since its a variable: $name=$_POST[what goes in here?]; mysql_query("UPDATE `layout` SET `name` = '". $name . "', WHERE id = '" . $id . "'"); would i write button$id or something? can someone plz help me with this? thank you Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 8, 2007 Share Posted July 8, 2007 Edit : Whoops, i didn't read your post properly. If, as i would guess, you have a number of these form elements that you wanting to put into a database, you can use arrays. An example might be: <input type="text" name="name[0]" /> <input type="text" name="name[1]" /> <input type="text" name="name[2]" /> You can then use a loop on the array: <?php $name= $_POST['name']; foreach($name as $value){ //insert the name into the database etc } ?> Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 8, 2007 Author Share Posted July 8, 2007 would there be a way of doing it if the $id is not a number? and also the input is in a while loop so that as soon as it does all the id's it stops how can i incorporate the array into that because i only specify name once. thx Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 8, 2007 Author Share Posted July 8, 2007 bump Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 8, 2007 Share Posted July 8, 2007 For your first question, yes its fine to use non-numeric indexes. As for your second question, im not entirely sure what you mean. Could you post the code you have? Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 8, 2007 Author Share Posted July 8, 2007 i think u didnt understand my first question i was wondering if there is any way of doing it the way i suggested in my first post and not using arrays and as for my code, here it is: this is the form <?php require_once('../connect.php'); $query = mysql_query("SELECT * FROM layout") or die(mysql_error()); echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <title>E Layout</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"> </head> <body> <a href=\"eenglish.htm\">English<a> > <a href=\"eeditlayout.php\">E Layout<a> <hr> <form action=\"esubmiteditlayout.php\" method=\"post\"> <table width=\"100%\" border=\"0\">"; while($row=mysql_fetch_object($query)) { $id=$row->id; $name=$row->name; $source=$row->source; echo " <tr> <td valign=\"top\" width=\"2%\"><input type=\"checkbox\" name=\"checkbox"; echo $id; echo "\"></td> <td valign=\"top\" width=\"5%\">Button "; echo $id; echo ":</td> <td valign=\"top\" width=\"10%\"><input type=\"text\" name=\"button"; echo $id; echo "\" value=\""; echo $name; echo "\"/></td> <td valign=\"top\" width=\"8%\">Button "; echo $id; echo " Source:</td> <td valign=\"top\" align=\"left\"><input type=\"text\" name=\"buttonsrc"; echo $id; echo "\" value=\""; echo $source; echo "\"/><input type=\"hidden\" name=\""; echo $id echo "\"></td> </tr>"; } echo " </table> <input type=\"submit\" name=\"edit\" value=\"Edit\"> <input type=\"submit\" name=\"insert\" value=\"Insert\"> <input type=\"submit\" name=\"del\" value=\"Delete\" onclick=\"return deldel()\"> <script language=\"javascript\"> function deldel() { var agree=confirm(\"Are you sure you want to delete the checked buttons from the Side Bar?\"); if(agree) return true; else return false; } </script> </form> </body> </html>"; mysql_close($link); ?> here is the submit <?php require_once('../connect.php'); $edit=$_POST[edit]; $insert=$_POST[insert]; $delete=$_POST[del]; $name=$_POST[]; $source=$_POST[]; if (isset($delete)) { } else if (isset($insert)) { mysql_query("INSERT INTO layout (id, name, source) VALUES ('', '', '')"); echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> <html> <head> <meta http-equiv=\"Refresh\" content=\"1; url=eeditlayout.php\"/> </head> <body> New Button has been Inserted.<br> Thank You </body> </html> "; } else if (isset($edit)) { while($row=mysql_fetch_object($query)) { $id=$row->id; mysql_query("UPDATE `layout` SET `name` = '". $name . "', `source` = '".$source."', WHERE id = '" . $id . "'"); } } mysql_close($link) ?> i really appreciate ur help thank you Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 8, 2007 Share Posted July 8, 2007 Why do you not want to use arrays? It would be far easier. You could go down the route of hidden fields etc, but arrays are a much better idea. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 8, 2007 Author Share Posted July 8, 2007 well, the reason i don't want to use arrays is because i'm new to php and i don't understand arrays very well but sure ill use it but i have no idea where to start. i would really appreciate your help thank you Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 8, 2007 Share Posted July 8, 2007 Well, first off, i suggest you try and learn a little about arrays. There's an introduction to them here: http://www.phpfreaks.com/tutorials/42/0.php Edit: plus there's a section from a free online php book about arrays here: http://hudzilla.org/phpwiki/index.php?title=Arrays Try reading through those first and see if you can understand the way this might work. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 8, 2007 Author Share Posted July 8, 2007 ok i understand what it is now but i just dont see the difference between just defining a variable and defining an array. its just as hard. i mean i can define 2 variables as $variable1="HI"; $variable2="BYE"; but to define the same thing as an array, i would have to do $variable=array("HI","BYE"); i mean i guess its a little easier to write since i don't have to repeat the same thing all over again but to echo the array i would have to go through the same process as with a variable instead of echo $variable1; i would have to echo $variable[1]; so I'm still faced with the problem i was faced with earlier with the variables. how would i define the array because the number of elements is not constant and how would i be able to implement this into the submit script. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 8, 2007 Share Posted July 8, 2007 I felt the easiest way to answer your questions was to create a working example. If you set up a table(called users in my example) with two fields , id and username. Populate it with a few bits of data. Or you can import this using phpMyAdmin if its easier: CREATE TABLE `users` ( `id` int(20) NOT NULL auto_increment, `username` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; -- -- Dumping data for table `users` -- INSERT INTO `users` (`id`, `username`) VALUES (1, 'tom'), (2, 'dick'), (3, 'harry'), (4, 'jim'), (5, 'bob'), (6, 'bill'); Then, the script: <html> <head> </head> <body> <?php include("test_db_connection.php"); if(isset($_POST['submit'])){//form has been submitted, so process it if(count($_POST['edituser']) > 0){//first check to see if we are trying to edit anyone. otherwise we get an error foreach($_POST['edituser'] as $key => $value){ //as the index of each element of the array, $key will contain the user ID $new_username = $_POST[username][$key]; mysql_query("UPDATE `users` SET `username`='$new_username' WHERE `id`=$key") or die(mysql_error()); echo "User $key# has changed their name to $new_username <br />"; } } if(count($_POST['deleteuser']) > 0){//first check to see if we are trying to delete anyone. otherwise we get an error foreach($_POST['deleteuser'] as $key => $value){ mysql_query("DELETE FROM `users` WHERE `id` ='$key'") or die(mysql_error()); echo "User $key# has been deleted<br />"; } } } ?> <form action="phpfreaks.php" method="post"> <table border="1"> <tr><td>User ID# </td><td>Username</td><td>Edit?</td><td>Delete?</td></tr> <?php $sql = mysql_query("SELECT * FROM `users`") or die(mysql_error());//grab all our users from the table while($row = mysql_fetch_assoc($sql)){//cycle through each user echo "<tr><td>$row[id]</td><td><input type='text' name='username[$row[id]]' value = '$row[username]' /></td><td><input type='checkbox' name='edituser[$row[id]]' /></td><td><input type='checkbox' name='deleteuser[$row[id]]' /></td></tr>"; //notice how i use the id as the index for all the arrays. This is because this is the the one thing that uniquely identifies each person } ?> <tr><td colspan="4"><input type="submit" name="submit" value="Edit/Delete" /></td></tr> </table> </form> </form> </body> </html> Its just a quick piece of code to show you how it all works. But its tested and does work. Play around with it , i hope it will answer your questions. If there's any of the code you dont understand, feel free to ask. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 8, 2007 Author Share Posted July 8, 2007 k thx 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.