PHP - How Do I Set A Default Variable From An Edit Form That Is Using Php Array List
I have a simple form that edits student information. I have several fields that I have a drop down list pulled as an array from a separate table. I would like the information that is already entered on the student to be the default but still have the drop down list available if the item was entered in error. For example: Drop down list of all the states of America, OH was entered for the student, when the edit form comes up for State in Address I would like OH to be the default but still have the list to choose if OH was entered in error. I have tried selected = $selected but then the entire drop down changed to OH. I am very, very much a beginner in this
<?php // list of possible modules comes from database $state_array=get_states(); //display as a drop down list for end user to choose if (!is_array($state_array)) { echo "<p>No States are currently available<p>"; return; } foreach ($state_array as $row) { echo "<option value=\"".($row['stateid'])."\""; // if existing state, put in current catgory echo "<strong> ".($row['stateid'])."</strong><br />"; echo "<option select=\"selected\">OH"; } ?> Similar TutorialsI trying to display data from a database for editing I have 2 table: 1. tbl_proizvodi_karakteristike (Translate: tbl_product_caracteristics) - In this table i have list of product caracteristic relatet to product 'id' in table 'tbl_product_list' (this 'tbl_product_list' is not important now) - field: id, opis, vrednsot, proizvod_id 2. tbl_karakteristike (Translate: tbl_caracteristic) - In this table i have list of all caracteristic - field: id, opis in field 'opis' in table 'tbl_proizvodi_karakteristike' i insert 'id' from 'tbl_karkateristike' and that works ok with combo box. But, when i try to edit value in 'tbl_proizvodi_karakteristike', i do not know how to show current value in combo box (i have list of value from 'tbl_karakteristike' in combox, starting from first) For examle, if i know that value in selected caracteristics is 'opis = 25', it means that i in combo box on load edit form mast have first value 25 and rest of posible value. For now i have value '1 and rest of value' Hiar is current code in edit form Code: [Select] <form action='product_carasteristic.php?product_id=<?php echo $p_id; ?>&product_name=<?php echo $p_name; ?>' method='POST'> <?php $p_id = $_GET['id']; $karakteristike = mysql_query("SELECT * FROM `tbl_proizvodi_karakteristike` WHERE `id`='$p_id'") or die(mysql_error()); while($row = mysql_fetch_array($karakteristike)){ $p_id = $row['id']; $p_opis = $row['opis']; $p_vrednost = $row['vrednost']; $p_proizvod_id = $row['proizvod_id']; ?> <table> <tr> <td > Karakteristika </td> <td> <select name="opis" style="font-family: verdana; font-size:15px; width:342px;"> <?php $tbl_karakteristike = mysql_query("SELECT * FROM `tbl_karakteristike`") or die(mysql_error()); while($row = mysql_fetch_array($tbl_karakteristike)){ $k_id = $row['id']; $k_opis = $row['opis']; ?> <option value='<?php echo $k_id; ?>'><?php echo $k_opis; ?></option> <?php } ?> </select> </td> </tr> <tr> <td> Vrednost karakteristike </td> <td> <input type="text" name="vrednost" value='<?php echo $p_vrednost; ?>'> </td> </tr> <tr> <td> <input type="submit" name="submit" value="Kreiraj"> </td> <td> Neophodno je popuniti sva polja </td> </tr> </table> <?php } ?> </form> thanks forward Hello, freaks. I need a little help with the best way to go about this. I have a field in mysql that contains a comma separated string of values. What I need to do is get those values out of the db and list them into editable text fields so the user can update the values in a html form. So far this is my code, I left out all the in betweens - validation, results, etc - for this post...but its there . SELECT servs FROM services WHERE id = $a_id $services = $row['servs']; $srvs = explode(',', $services); Then for the form: <?php foreach ($srvs as $service) { ?> <input type="text" name="servs[]" value="<?php echo $service ?>" /> <?php } ?> This gives me a text field for each value of servs, great! But the problem is, the user can enter up to 15 servs. Some currently have less than that saved in the db. I want to give them the ability to 1. edit any of the current servs value and 2. add more values if they have less than 15. My question is, how do I echo out 15 editable text fields for servs[] no matter the number of values they currently have? And when I go to implode the values, will blank text fields screw everything up? I can't find anything in the forums that relate to this. I believe it probably will involve $i, but I am not too familiar with counting in php yet, so I could really use some help. Thanks! Oh - and I am not stuck on using text fields, but I couldn't figure how to make this work using a select list or checkboxes. Any suggestions either way would help. This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=322008.0 I have a dropdown list which have some elements. I want to get all the elements of the dropdown list into a variable of type array. Any Idea??? Hi All, I've a loop which lists a series of options as checkboxes. What I'd like to do is set 1 as a default. But I've no idea how to do it!? Here's the code: <?php $i = 0; ?> <?php if ( bp_has_groups() ) : while ( bp_groups() ) : bp_the_group(); ?> <input type="checkbox" id="field_reg_groups_<?php echo $i; ?>" name="field_reg_groups[]" value="<?php bp_group_id(); ?>" /><?php bp_group_name(); ?> <?php } ?> <?php $i++; ?> Any help is much appreciated TIA Okay, so, here's my dilemma. I'm wanting to have it so someone can edit an item in the database, but it is a dropdown menu for the options to choose from. How do I make it so that the option that the item currently is is what the default showing is? For instance, if the item is for a "warrior", it will show warrior already in the dropdown menu. However, I could go to the dropdown menu and select a different option if I wanted. The {{class}} doesn't seem to give that default. Right now, it just defaults to "All" regardless of what the item truly goes to, such as "warrior". Thanks in advance!! Here's what I have for the coding: <tr><td width="20%">Class:</td><td><select name="class" value="{{class}}"> <option value="1" {{class1select}}>All</option> <option value="2" {{class2select}}>Alchemist</option> <option value="3" {{class3select}}>Assassin</option> <option value="4" {{class4select}}>Dark Arts</option> <option value="5" {{class5select}}>Dark Paladin</option> <option value="6" {{class6select}}>Entertainer</option> <option value="7" {{class7select}}>Hunter</option> <option value="8" {{class8select}}>Mage</option> <option value="9" {{class9select}}>Monk</option> <option value="10" {{class10select}}>Paladin</option> <option value="11" {{class11select}}>Pirate</option> <option value="12" {{class12select}}>Priest</option> <option value="13" {{class13select}}>Psion</option> <option value="14" {{class14select}}>Scholar</option> <option value="15" {{class15select}}>Thief</option> <option value="16" {{class16select}}>Warlock</option> <option value="17" {{class17select}}>Warrior</option> </select></td></tr> I am using this php code <?php echo $_GET['CiFrame']; ?> to check the url for the variable CiFrame, this allows me to link to a page through my page containing the iframe. Here is my iframe code. <iframe name="CiFrame" width="727" height="805" src="<?php echo $_GET['CiFrame']; ?>" scrolling="auto" frameborder="0"></iframe> The problem is that if the url does not contain a variable the iframe will not open a page. How can i set a default variable if one is not provided? Thank You Hello, i know this is a very silly question but it bothers me to know the reasoning behind it. My application has a design where the functions are stored in functions.php and variables are stored in variables.php. There are some functions that have variables that i might change often and the reason i don't directly use them in the argument itself is so that in future if i have to change the default argument's value , then i wouldn't have to scroll through all the functions and look for it. I have a slippery memory i know i will be able to remember it better if i store custom global variables separately and custom global functions separately. I tried searching these forums for this but its giving me a message like "search daemon not found" or something like that.. Anyways , coming to the point, i know that PHP doesn't accept variables as default arguments but may i know the reasoning behind this? If i understand the reasoning behind this i know it will stick better in my head.So..why does a function not allow a variable as a default value? Or if anyone has a link to explaining this behaviour in PHP, i would appreciate it if you could pass it on! Thank you I am working on a project that uses a drop down list to chose the category when inserting new data into the database. What I want to do now is make the drop down list default to the chosen category on the list records page and the update page. I have read several tutorials, but they all say that I have to list the options and then select the default. But since it is possible to add and remove categories, this approch won't work. I need the code to chose the correct category on the fly. There are two tables, one that has the category ID and category name. The second table has the data and the catid which is referenced to the category id in the first table. Code: [Select] -- -- Table structure for table `categories` -- DROP TABLE IF EXISTS `categories`; CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `categories` varchar(37) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ; -- -------------------------------------------------------- -- -- Table structure for table `links` -- DROP TABLE IF EXISTS `links`; CREATE TABLE IF NOT EXISTS `links` ( `id` int(4) NOT NULL AUTO_INCREMENT, `catid` int(11) DEFAULT NULL, `name` varchar(255) NOT NULL DEFAULT '', `url` varchar(255) NOT NULL DEFAULT '', `content` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `catid` (`catid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ; Then is the list records file, I have Code: [Select] <?php include ("db.php"); include ("menu.php"); $result = mysql_query("SELECT categories FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $categories=$row["categories"]; $options.= '<option value="'.$row['categories'].'">'.$row['categories'].'</option>'; }; $id = $_GET['id']; $query="SELECT * FROM links ORDER BY catid ASC"; $result=mysql_query($query); ?> <table width="65%" align="center" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="7"><strong>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>Category ID</strong></td> <td align="center"><strong>Category ID</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>URL</strong></td> <td align="center"><strong>Content</strong></td> <td align="center"><strong>Update</strong></td> <td align="center"><strong>Delete</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td> <SELECT NAME=catid> <OPTION>Categories</OPTION> <?php echo $options; ?> </SELECT> </td> <td><? echo $rows['catid']; ?></td> <td><? echo $rows['name']; ?></td> <td><a href="<? echo $rows['url']; ?>"><? echo $rows['url']; ?></a></td> <td><? echo $rows['content']; ?></td> <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td> <td align="center"><a href="delete.php?id=<? echo $rows['id']; ?>">delete</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> So, how do I get this code Code: [Select] $result = mysql_query("SELECT categories FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $categories=$row["categories"]; $options.= '<option value="'.$row['categories'].'">'.$row['categories'].'</option>'; }; <SELECT NAME=catid> <OPTION>Categories</OPTION> <?php echo $options; ?> </SELECT> to give me an output that will be something like if catid exactly matches categories.id echo categories.categorie ??? so far everything I have done produces either a default category of the last category, the catid (which is a number), all of the categories (logical since catid will always be = id, or nothing. How do I get just the category name? I will keep reading and try to figure this out, but any help would be greatly appreciated. Thanks in advance First page adds a new job number, then the order page loaded with the job number id as a get id. Basically there is a while loop in the order page which shows products/services client can order and he chooses what he requires 'one or eight services' (8 in total) and some other variables like date of order and client name etc. Here is the order item code The first sql statement executes fine, but in the second sql query nothing happens $sql="insert into job_order(order_num,order_date,order_customer_id, order_remarks) values(".$_GET['id'].",NOW(),".$_POST['companyBox'].",'".$_POST['remarkBox']."');"; $res=mysql_query($sql); $id=mysql_insert_id(); foreach($_POST as $key => $value) { if(!empty($value)) { $key.' => '.trim(strip_tags($value)); $order="INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('$_GET[id]','$value','$value','$value');"; $orderres=mysql_query($order) or die(mysql_error()); } }?>$sql="insert into job_order(order_num,order_date,order_customer_id, order_remarks) values(".$_GET['id'].",NOW(),".$_POST['companyBox'].",'".$_POST['remarkBox']."');"; $res=mysql_query($sql); $id=mysql_insert_id(); foreach($_POST as $key => $value) { if(!empty($value)) { $key.' => '.trim(strip_tags($value)); $order="INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('$_GET[id]','$value','$value','$value');"; $orderres=mysql_query($order) or die(mysql_error()); } } ?> ============= This is the formI have removed parts which are irrelevant. Please note that dateBox and companyBox are not required to be looping as they are only for first table, echo "<form action=".$config_basedir."./vieworder.php?id=".$_GET['id']." name=form1 method=post>";?><table><tr><td><h4>JOB ORDER</H4></TD><TD></TD></TR><TR><TD>ORDER NUMBER</TD><TD><?PHP ECHO $_GET['id'] ?></td></tr><tr><td>ORDER DATE</td><td><input type=text name=dateBox></td></tr> <tr><td>COMPANY NAME</td><td><?PHP$sql="select * from customers";$res=mysql_query($sql);echo "<select name=companyBox><option value=''>Please select</option>";WHILE($fetch=mysql_fetch_assoc($res)){ echo "<option value='".$fetch['id']."'>".$fetch['cust_name']."</option>";} echo "</select>"; echo "</td></tr>"; ?> </table> <table><tr><th>ITEM</th><th>QUANTITY</th><th>N0. of Teeths</th></tr><tr><?PHP$sql="select * from products";$res=mysql_query($sql); WHILE($fetch=mysql_fetch_assoc($res)){ echo "<td><input type=text name=desBox value='".$fetch['prod_id']."'>".$fetch['prod_name']."</td><td><input type=text name=quantBox></td><td><input type=text name=teethBox></td>";echo "</tr>";}echo "</table>";?>echo "<form action=".$config_basedir."./vieworder.php?id=".$_GET['id']." name=form1 method=post>"; ?> <table> <tr> <td><h4>JOB ORDER</H4></TD><TD></TD> </TR> <TR> <TD>ORDER NUMBER</TD><TD><?PHP ECHO $_GET['id'] ?></td> </tr> <tr> <td>ORDER DATE</td><td><input type=text name=dateBox></td> </tr> <tr> <td>COMPANY NAME</td><td> <?PHP $sql="select * from customers"; $res=mysql_query($sql); echo "<select name=companyBox><option value=''>Please select</option>"; WHILE($fetch=mysql_fetch_assoc($res)){ echo "<option value='".$fetch['id']."'>".$fetch['cust_name']."</option>";} echo "</select>"; echo "</td></tr>"; ?> </table> <table> <tr> <th>ITEM</th><th>QUANTITY</th><th>N0. of Teeths</th> </tr> <tr> <?PHP $sql="select * from products"; $res=mysql_query($sql); WHILE($fetch=mysql_fetch_assoc($res)){ echo "<td><input type=text name=desBox value='".$fetch['prod_id']."'>".$fetch['prod_name']."</td> <td><input type=text name=quantBox></td> <td><input type=text name=teethBox></td>"; echo "</tr>";} echo "</table>"; ?> HERE IS THE IMAGE showing the populated services. http://dubads.com/images/order.jpg Hello! We our creating an admin page to display a list of all the items within a mysql database, these items are within a table called "item". Each item is categorized, these categories are in a table called "category" each category has an ID. There is then a table called "links" which links the items to their category using ID's. We want to display each item by their category with an edit and delete button. We have it so all the items display with the two buttons in no specific order however we are not too sure how to make it so they all display via their category. Here is the code we are currently using: Code: [Select] <?php $itemlist = mysql_query("SELECT item.item_name, item.item_description, item.item_image, item.item_price, item.item_id FROM item ORDER BY item.item_id ASC"); $item_count = 0; ?> <?php while( $row = mysql_fetch_array($itemlist) ){?> <?php if(($row['item.item_name'] == 1) && (isset($item_count) && $item_count < 1)) { ?> <?php $item_count ++; } ?> <li class="title"> <h3><?php echo $row['item_name'];?></h3><br /> <form action="ourworkadmin.php" method="get"> <input type="hidden" name="item_id" value="<?php echo $row['item_id'];?>" /> <input name="edit" value="Edit" type="submit" /> <input name="remove" value="Remove" type="submit" /> </form> </li> <?php } ?> </ul> <!--Close-items_list--> </div> Any help would be appreciated! Thank you!!! Hi All, Need help. I'm strugling to figure out how to write the code to display default image if no image is available. I'm able to display image if data is available. Not able figure how to echo if/else statment. Below is the code Code: [Select] <?php function perDetails($var1, $result, $var2){ $dft_img = 'lib/images/name.png'; echo '<div class="desc">'.$var1.'</div>'; $color="1"; echo '<table align="center" cellpadding="2" cellspacing="1">'; while($row = mysql_fetch_array($result)){ $id = $row['pid']; $name = $row['pname']; $img = $row['pphoto_localurl']; $poster = str_replace("./", "lib/", $img); if($color==1){ echo '<tr bgcolor="#FFC600"><td><img src="'.$poster.'" width="32" height="42" alt="'.$name.'"</td><td><a href="persons.php?det='.$var2.'&perid='.$id.'"> '.$name.'" </a></td></tr>'; $color="2"; } else { echo '<tr bgcolor="#C6FF00"><td><img src="'.$poster.'" width="32" height="42" alt="'.$name.'"</td><td><a href="persons.php?det='.$var2.'&perid='.$id.'"> '.$name.'" </a></td></tr>'; $color="1"; } } echo '</table>'; } ?> hey guys i have an array but want to add "n." infront of each key value...example n.news_id, n.user_id etc...is there a simple way/function of doing it other than changing the values through a loop? Array ( [news] => Array ( => news_id [1] => user_id [2] => news [3] => date [4] => time ) ) Originally, I would get both, and unfortunately would inconsistently use both. Then, I wanted more consistently, so configured php.ini to only return objects as I felt accessing them was more concise. And then later, I found myself needing arrays more often, and initially would just typecast them to arrays but eventually my standard was to set the PDO's fetch style to an array. And now I find myself almost always explicitly requesting arrays and occasionally requesting columns or named values. Do you configure ATTR_DEFAULT_FETCH_MODE, and if so to what? PS. Anyone use FETCH_CLASS, FETCH_INTO or FETCH_NUM? If so, what would be a good use case? Hi, In my mysql database i have a text input option, in the registration form and edit my details form i have a multiple select dropdown list, which user selects options to populate the text input box, which ultimately populates the text field in the mysql database. All works perfectly. The dropdownlist consists of 3 parts <optgroups> first is current selection (what is the usesr current selection)works fine, The second <optgroup> is existing words, what words we(the site) have given as options, and the third <optgroup> is the words that others have used. This is where im having a small problem. Because its a text field when i call the data from the database, it calls the entire text box as a single option in my select list.. I want to break the words in the text field (at the comma) and have them listed each one as an option in the select list. Example what i need: Words in text box:(my input allows the "comma") word1, word2, word3, word4, word5, word6, How i want them called/displayed: <option value=\"word1\">word1</option> <option value=\"word2\">word2</option> <option value=\"word3\">word3</option> <option value=\"word4\">word4</option> <option value=\"word5\">word5</option> <option value=\"word6\">word6</option> here's my code: $query = "SELECT allwords FROM #__functions_experience WHERE profile_id = '".(int)$profileId."' LIMIT 1"; $original_functionsexperience =doSelectSql($query,1); $query = "SELECT allwords FROM #__functions_experience WHERE profile_id = '".(int)$profileId."' LIMIT 1"; $functionsexperiencelist=doSelectSql($query); $funcexpList ="<select multiple=\"multiple\" onchange=\"setFunctionsexperience(this.options)\">"; foreach ($functionsexperiencelist as $functionsexperienceal) { $selected=""; if ($functionsexperienceals->allwords == $original_functionsexperience) $selected=' selected="selected"'; $allwords=$functionsexperienceal->allwords; $funcexpList .= "<optgroup label=\"Current selection\"> <option value=\"".$allwords."\" ".$selected." >".$allwords."</option> </optgroup> <optgroup label=\"Existing Words\"> <option value=\"existing1,\">existing1</option> <option value=\"existing2,\">existing2</option> <option value=\"existing3,\">existing3</option> <option value=\"existing4,\">existing4</option> <option value=\"existing5,\">existing5</option> <option value=\"existing6,\">existing6</option> </optgroup> <optgroup label=\"Others added\"> //heres problem <option value=\"".$allwordsgeneral."\">".$allwordsgeneral."</option> </optgroup>"; } $funcexpList.="</select>"; $output['FUNCEXPLIST']=$funcexpList; The result im getting for optgroup others added: word1, word2, word3, word4, word5, how can i get it like this: <option value=\"word1\">word1</option> <option value=\"word2\">word2</option> <option value=\"word3\">word3</option> <option value=\"word4\">word4</option> <option value=\"word5\">word5</option> <option value=\"word6\">word6</option> Hi, I use the following code to create a select menu from an array of options stored in LISTS.php: include 'LISTS.php'; print('<select id="from" name="from">'); foreach ($langList as $lang) {printf('<option %s>%s</option>', ($from1 == $lang ? 'selected="selected"' : ''), $lang); } echo '</select>'; where LISTS.php includes the following: $langList = array(' ','English', 'French', 'German', 'Dutch', 'Spanish'); This works great, but now I want to do something similar with a checkbox list, where each checkbox has an associated 'onchange' javascript function and I'm getting pretty stuck. My checkbox list is of the following form: Code: [Select] <html> <ul style="height: 95px; overflow: auto; width: 200px; border: 1px solid #480091; list-style-type: none; margin: 0; padding: 0;"> <li id="li1b"><label for="chk1b"><input name="chk1b" id="chk1b" type="checkbox" onchange="function1('chk1b','li1b')">Option1</label></li> <li id="li2b"><label for="chk2b"><input name="chk2b" id="chk2b" type="checkbox" onchange="function1('chk2b','li2b')">Option2</label></li> //etc. </ul> </html> What I want to do is have 'Option1', 'Option2', etc. stored in an array in LISTS.php and have a PHP script that populates the checkbox list accordingly, in a similar manner to my select menu above. I can't work out how to get the ID of the next <li> and the next <input> in the list to go up by one each time, e.g. 'li1b' then 'li2b', 'li3b', etc. Could someone pls help me out? Thanks! This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=316203.0 I HAVE TRIED looking at it but i am new to php so i can't figure out what i did wrong help please !!!!!!!!!!!! Fatal error: Call to undefined method modernCMS::update_form() in C:\wamp\www\test\inc\update-content.php on line 4 ---- Page that's accessing code ----- <?php include '../test/inc/admin/nav.php'; echo $obj->update_form($_GET['id']); ?> ------ function ------ <?php class modernCMS { var $host; var $username; var $password; var $db; function connect() { $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error()); mysql_select_db($this->db, $con) or die(mysql_error()); } //end connect function get_content($id = '') { if($id != ""): $id = mysql_real_escape_string($id); $sql = "SELECT * FROM cms_content WHERE id = '$id'"; // $return = "<p><a href=../test/inc/admin/nav.php>Go Back To Content</a></p>"; else: $sql = "SELECT * FROM cms_content ORDER BY id DESC"; endif; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) != 0): while($row = mysql_fetch_assoc($res)) { echo '<table bgcolor="#975627" width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <td height="25" background="images/menu.gif"><center><strong>' . $row['title'] . '</strong></center></td> </tr> <tr bgcolor="975627"><td height="0"></td></tr> <tr> <td bgcolor="#181818"> '; echo '<br> <div align="center">' . $row['body'] . ' <br><br> </td> </tr> <tr> <td height="25" background="images/menu.gif"> </td> </tr> </table><br><br> '; } else: echo '<p>Uh Oh!!! </p>'; endif; // echo $return; } //end get_content function add_content($p) { $title = mysql_real_escape_string($p['title']); $body = mysql_real_escape_string($p['body']); if(!$title || !$body): if(!$title): echo "<p>The title is required!</p>"; endif; if(!$body): echo "<p>The body is required!</p>"; endif; echo '<p><a href="add-content.php">Try Again!!!</a></p>'; else: $sql = "INSERT INTO cms_content VALUES (null, '$title', '$body')"; $res = mysql_query($sql) or die(mysql_error()); echo "Added Successfully!"; endif; } //end add_content function manage_content() { $sql = "SELECT * FROM cms_content ORDER BY id DESC"; $res = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($res)): ?> <div> <h2 class="title"><?php echo $row['title']; ?></h2> <span class="actions"<a href="../test/update-content.php?id=<?php echo $row['id']?>">Edit</a> | <a href="?delete= <?php echo $row['id'] ?> ">Delete</a></span> </div> <?php endwhile; echo '</div>'; //Class Manage Div } //end manage_content function delete_content($id) { if(!$id) { return false; }else { $id = mysql_real_escape_string($id); $sql = "DELETE FROM cms_content WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); echo "Content Deleted Successfully!"; } } } //end delete_content function update_form($id) { $id = mysql_real_escape_string($id); $sql = "SELECT * FROM cms_content WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res); ?> <form method="post" action="./post-added.php"> <input type="hidden" name="update" value="true" /> <input type="hidden" name="id" value="<?php $row['id'] ?>" /> <div> <label for="title">Title:</label> <input type="text" name="title" id="title" value="<?php echo $row['title'] ?>" /> </div> <div> <label for="body">Body:</label> <textarea name="body" id="body" rows="8" cols="40" /><?php echo $row['body'] ?></textarea> </div> <input type="submit" name="submit" value="Update Content" /> </form> <?php } //end update_content_form function update_content($p) { $title = mysql_real_escape_string($p['title']); $body = mysql_real_escape_string($p['body']); $id = mysql_real_escape_string($p['id']); if(!$title || !$body): if(!$title): echo "<p>The title is required!</p>"; endif; if(!$body): echo "<p>The body is required!</p>"; endif; echo '<p><a href="update-content.php?id=' . $id . '">Try Again!!!</a></p>'; else: $sql = "UPDATE cms_content SET title = '$title', body = '$body' WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); echo "Updated Successfully!"; endif; } //end update_content($p) ?> Gang, I'm trying to create a form that allows me to edit table data within a MySQL database. I've been able to display the data, no problem. I want to be able to edit the fields in the database tables but have had no luck. I'm using session variables to connect and gather the information I need. Here's the code for collecting the table data: <?php mysql_select_db($database); if(empty($database)) { echo "<p>You must be connected to a database in order to view any table data.</p>"; }else{ // Show table data start $sql = "SHOW TABLES FROM $database"; $result = mysql_query($sql); $table = array(); while ($row = mysql_fetch_row($result)) { $table[] = $row[0]; } if (count($table) == 0) { echo "<p>The database '" . $database . "' contains no tables.</p>\n"; } else { foreach($table AS $aTable) { echo "<p style='text-align:left;float:left;width:100%;margin-top:15px;'>Table: <font color='green'>$aTable</font>"; if (!mysql_connect($hostname, $user, $passwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$aTable}"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<div style='text-align:left;width:100%;'><table border='0'><tr><td></td>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td style='text-align:left;padding:3px;font-size:11px;color:green;'>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr><td style='text-align:center;padding:3px;font-size:10px;border:1px solid #888;background:#fff;'><a href='edit_data.php?=$row[0]' style='color:red;text-decoration:none;'>edit</a></td>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td style='text-align:left;padding:3px;font-size:10px;border:1px solid #888;background:#fff;clear:both;float:left;'>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); echo "</table></div>"; } } } //end show table data include("includes/footer.php"); ?> I'm not sure how to create a form using the session variables in order to be able to edit the correct information since I want to edit numerous databases. Any help would be great! Thanks Hi guys im in the middle of optimizing code.. Code: [Select] $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { $enemy['current_health'] = $row['current_health']; $enemy['current_skill'] = $row['current_skill']; $enemy['level'] = $row['level']; $enemy['damage'] = $row['damage']; $enemy['evade'] = $row['evade']; $enemy['accuracy'] = $row['accuracy']; $enemy['speed'] = $row['speed']; $enemy['luck'] = $row['luck']; echo 'debug: variables synced with db table'; }I know that the setting of these variables are messy and can be done in a better way... but how? I have tried foreach and copying other's code for an array copy but it lead no where. Then again my syntax could be wrong... I used something like Code: [Select] foreach ( $enemy[$value] as $row => $value) { $enemy[$value] = $row[$value]; } |