PHP - Update A Dynamic Array
How can I use php to update a dynamic amount of input fields and tell mysql which ones to update respectively? Essentially I have a list of occupations and if someone wants to update one of them I don't know how to tell php which one they've changed in a dynamic way.
Code: [Select] <?php if(!empty($occupations)) { ?> <h3 id="job_function">Job Functions</h3> <?php $i = 0; foreach($occupations as $occupation) { ?> <div class="formElement"> <div class="formFieldLabel "> <label for="occupation_<?php echo $i; ?>"><?php echo $occupation['companyname']; ?></label> </div> <div class="formField"> <input id="occupation_<?php echo $i; ?>" type="text" name="occupation_<?php echo $i; ?>" value="<?php echo $occupation['function']; ?>" /> </div> </div> <?php $i++; } } ?> Similar TutorialsFirst off thank you for opening my issue and taking the time out to try and help. I have created a php page that will display a list of people from a mysql database. What i would like to do is have a button that will update a field in the database when pressed. My question is if i make the fields into a form will it only update that one person? Please guys, I need your help on this one. How can call the data from mysql and make the values checked on the checkboxes Have got three tables. I am retrieving data and updating two tables at once... hi i am trying to make a payroll calculator script that takes employee info, calculates pay, displays submitted info in a table, stores info in an array, and updates the array when new info is submitted. i have most of these accomplished, i am having trouble with the "store into an array, and update the array when new info is submitted" parts of the project. i am still not very fluent in php so there may be easier ways to achieve what i have so far. any pointers would be a great help, this part has got me stumped. Hello all, I have yet again trouble finding a logical solution to my problem. I'm fetching an array which can hold 1 or more values. The problem is, I want these values to ouput in my json_encode function, but this also needs to happen dynamically depending on the amount of values. I can't explain it further, so here's the code so far: Code: (php) [Select] $files = mysql_fetch_array($get_files); $a = count($files); $i = 1; while ($files) { $variablename = 'fileName' . $i; $$variablename = $files['fileName']; $i++; } $output = array( OTHER VALUES , 'fileName1' => $fileName1, 'fileName2' => $fileName2, 'fileName3' => $fileName3, ............); // How do I add the fileNames dynamically depending on how many there are? This got me thinking, I also need to dynamically GET the values with jQuery. How would I do that, when the above eventually works? Thank you. I've used a lot of solutions from this site, however, this is my first post, so go easy on me :-) I've created a product form the successfully INSERTS data to a mysql table. I can successfully query the data so that product information can be updated. When I click submit to UPDATE the rows, everything updates except for 2 fields that are arrays. Here is my product form that populates data to be edited. Code: [Select] <?php session_start(); require("config.php"); require("db.php"); require("functions.php"); if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == FALSE) { header("Location: " . $config_basedir); } if(isset($_GET['func']) == TRUE) { if($_GET['func'] != "conf") { header("Location: " . $config_basedir); } $validid = pf_validate_number($_GET['id'], "redirect", $config_basedir); header("Location: " . $config_basedir . "adminedit.php"); } else { require("header.php"); $sql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";"; $query = mysql_query($sql); while ($prodrow = mysql_fetch_array($query)){ $id = $prodrow['id']; $active=$prodrow['active']; $cat_id=$prodrow['cat_id']; $name=$prodrow['name']; $description=$prodrow['description']; $details=$prodrow['details']; $price=$prodrow['price']; $price2=$prodrow['price2']; $price3=$prodrow['price3']; $price4=$prodrow['price4']; $price5=$prodrow['price5']; $price6=$prodrow['price6']; $minimum=$prodrow['minimum']; $price7=$prodrow['price7']; $price8=$prodrow['price8']; $price9=$prodrow['price9']; $image=$prodrow['image']; } ?> <form action="adminprodupdate.php" method="post"> <table width="500" border="1" cellpadding="10"> <tr> <td>ID:</td> <td><input type="hidden" value="<? echo $id; ?>" name="id" /></td> </tr> <tr> <td>Active:</td> <td><input name="active" type="radio" value="0" <?php echo ($active=='0')? 'checked':'';?> />Yes<br /> <input name="active" type="radio" value="1" <?php echo ($active=='1')? 'checked':'';?> />No</td> </tr> <tr> <td>Category:</td> <td><input name="cat_id" type="radio" value="1" <?php echo ($cat_id=='1')? 'checked':'';?> />Hats<br /> <input name="cat_id" type="radio" value="2" <?php echo ($cat_id=='2')? 'checked':'';?> />Shirts<br /> <input name="cat_id" type="radio" value="3" <?php echo ($cat_id=='3')? 'checked':'';?> />Promotional Items</td> </tr> <tr> <td>Product ID: </td> <td><input type="text" name="name" value="<? echo $name; ?>"></td> </tr> <tr> <td>Short Description: </td> <td><input type="text" name="description" value="<? echo $description; ?>"></td> </tr> <tr> <td>Details:</td> <td><textarea name="details" cols="50" rows="10" ><? echo $details; ?></textarea></td> </tr> <tr> <td>Price for S-XL:<br /> <br /><strong><div id="redfont">**Shirts**<br />&<br />**Hats**</div></strong></td> <td>1-23: <input type="text" name="price" value="<? echo $price; ?>"><br><br> 24-47: <input type="text" name="price2" value="<? echo $price2; ?>"><br><br> 48+: <input type="text" name="price3" value="<? echo $price3; ?>"><br><br> </td> </tr> <tr> <td>Price for 2XL - 5XL:<br /> <br /><strong><div id="redfont">**Shirts ONLY!**</div></strong></td> <td>1-23: <input type="text" name="price4" value="<? echo $price4; ?>"><br><br> 24-47: <input type="text" name="price5" value="<? echo $price5; ?>"><br><br> 48+: <input type="text" name="price6" value="<? echo $price6; ?>"><br><br> </td> </tr> <tr> <td>Minimum QTY: </td> <td><input type="text" name="minimum" value="<? echo $minimum; ?>"></td> </tr> <tr> <td>Sizes:</td> <td><? echo ''; $result = mysql_query("SELECT * FROM sizes"); while ($row = mysql_fetch_assoc($result)) { $selected = ''; $result2 = mysql_query("SELECT * FROM productoptions WHERE productid = '" . $_GET['id'] . "' AND sizeid = '" . $row['id'] . "'"); if ($row2 = mysql_fetch_assoc($result2)) { $selected = 'checked '; } echo '<input type="checkbox" name="size[]" value="' . $row['id'] . '" ' . $selected . '/> ' . $row['size'] . '<br />'; } ?> </td> </tr> <tr> <td>Colors:</td> <td><? $result = mysql_query("SELECT * FROM colors"); while ($row = mysql_fetch_assoc($result)) { $selected = ''; $result2 = mysql_query("SELECT * FROM productoptions WHERE productid = '" . $_GET['id'] . "' AND colorid = '" . $row['id'] . "'"); if ($row2 = mysql_fetch_assoc($result2)) { $selected = 'checked '; } echo '<input type="checkbox" name="color[]" value="' . $row['id'] . '" ' . $selected . '/> ' . $row['color'] . '<br />'; } ?> </td> </tr> <tr> <td> </td> <td><input type="Submit" value="Update this product"></td> </tr> </form> </table> <?php } require("footer.php"); ?> Here is my script: Code: [Select] <?php include("config.php"); include("db.php"); $id=$_POST['id']; $active=$_POST['active']; $cat_id=$_POST['cat_id']; $name=$_POST['name']; $description=$_POST['description']; $details=$_POST['details']; $price=$_POST['price']; $price2=$_POST['price2']; $price3=$_POST['price3']; $price4=$_POST['price4']; $price5=$_POST['price5']; $price6=$_POST['price6']; $minimum=$_POST['minimum']; $arrsizes = $_POST['size']; $arrcolors = $_POST['color']; $image=$_POST['image']; $sql="UPDATE products SET active = '$active', cat_id = '$cat_id', name = '$name', description = '$description', details = '$details', price = '$price', price2 = '$price2', price3 = '$price3', price4 = '$price4', price5 = '$price5', price6 = '$price6', minimum = '$minimum', image = '$image' WHERE id = '$id'"; $result = mysql_query("SELECT id FROM products ORDER BY id DESC LIMIT 0,1"); if ($row = mysql_fetch_assoc($result)) { $productid = $row['id']; } foreach ($arrsizes as $sizevalue) { foreach ($arrcolors as $colorvalue) { $sql2="UPDATE productoptions SET sizeid = '$sizevalue', colorid = '$colorvalue' WHERE productid='$id' "; } } mysql_query($sql) or die ("Error: ".mysql_error()); mysql_query($sql2) or die ("Error: ".mysql_error()); echo $sql; echo $sql2; header("Location: " . $config_basedir . "adminhome.php"); ?> And here is what is echo'd after submit: Code: [Select] UPDATE products SET active = '0', cat_id = '2', name = 'Test Edit Prod', description = 'just a simple test', details = 'just a simple testjust a simple testjust a simple testjust a simple testjust a simple testjust a simple test', price = '1', price2 = '2', price3 = '3', price4 = '4', price5 = '5', price6 = '6', minimum = '127', image = '' WHERE id = '41'UPDATE productoptions SET sizeid = '6', colorid = '5' WHERE productid='41' This part of the code: UPDATE productoptions SET sizeid = '6', colorid = '5' WHERE productid='41' , is supposed to be updating sizes and colors, however, it is only updating the last size/color selected in the check box. Thanks in advance for any support! Hi, Is there any way to pass dynamic arguments to array_multisort. Below is my idea of what the code should look like. First it has the data then its builds an array of array indexes with the sort direction. This array is then passed to the array_multisort function with the original data. Code: [Select] $data[] = array([0] => 67, [1] => 2, [3]' => 2001); $data[] = array([0] => 86, [1] => 4, [3]' => 2002); $data[] = array([0] => 98, [1] => 5, [3]' => 2002); $data[] = array([0] => 86, [1] => 6, [3]' => 2003); $data[] = array([0] => 67, [1] => 1, [3]' => 2004); $arg = array(1 => SORT_ASC, 3 => SORT_DESC); array_multisort($arg, $data); I have got this working with hardcoded values, for example... Code: [Select] array_multisort($sort1, SORT_ASC, $sort3, SORT_DESC, $data); but in my code instead of just 2 sorts there could be 'x' amounts. I hope ive made this clear. Any response would be much appreciated. Thanks in advance. Hi ! I'd like to count all occurences in "$thiscodestring" into a "dynamic?" array and print it out after the end of the loop, how do I assign the variablenames dynamically ? $query = "SELECT codes, status FROM table"; while ($row = mysql_fetch_assoc($result)) { $thiscodestring = $row[codes]; // looks like "a1 c2 d3 10 15 1 a1 a1"; $singlecodes = explode(" ", $thiscodestring); foreach($singlecodes as $thiscode) { $$thiscode[count] = $$thiscode[count] + 1; } } After all Mysql-Stuff is done I'd like to print out the counted codes, for example the code "a1" was found 100 times in all querys then $$a1[count] should be 100, c2 was found 15 times, then $$c2[count] should be 15 and so on. Question: How do I tell php to assign a "dynamic" variable with the name of the found string which was exploded (in this example a1, c2, d3... ) before ?! $$ does not seem to work. And how can I then print_r() the value if I dont know the name ? Thanks ! I'm scraping a housing website with html source: Code: [Select] <tr><td> </td><td> Bedrooms </td><td> </td><td >3</td><td> </td></tr> <tr><td> </td><td> Full Baths </td><td> </td><td >1</td><td> </td></tr> <tr><td> </td><td> Partial Baths </td><td> </td><td >1</td><td> </td></tr> <tr><td> </td><td> Interior Sq Ft </td><td> </td><td >1,356</td><td> </td></tr> <tr><td> </td><td> Acres </td><td> </td><td >0.16</td><td> </td></tr> What i'm trying to do is create an associative array with only the details I want from the above. I've already got a function that steps through the HTML and creates an array element for each row with the contents of each <td> statement as follows: Code: [Select] $repl = array(" ", "(", ")"); $repl_with = array("", "", ""); foreach ($rows_feat as $id2 => $row2){ $features_raw = $scrape->fetchAllBetween('<td','</td>',$row2,true); //$features_data[str_replace($repl,$repl_with,strtolower(trim($features_raw[1])))] = trim($features_raw[3]); $countid=$countid+1; } The array produced by this would look like: features_raw[1] = Bedrooms features_raw[3] = 1 Basically, I want to take data like this: Beds 1 Baths 1 Garage Yes and create an array on the fly: House[beds]=1 House[baths]=1 House[garage]="yes" The problem is that the order of Beds, Baths and Garage changes. So on some house pages the order would be Baths, Garage, Beds.. etc. Any pointers out there? Thanks, Brett Hi, Ok, so I'm making a registration form system, where I can create new registration forms for the users to pick a date etc. In my form I can add and remove text fields via Javascript - to add additional dates etc. so I don't know if I'll be submitting 3 fields or 15. The could look like this: <input type="hidden" name="session_id[]" value="<?php echo $session_id; ?>"> <input type="text" name="picked_date[]"> <input type="text" name="picked_room[]"> They are put in a big array: De bliver smidt i et stort array: $n=0; foreach ($session_id as $_session_id) { $bigar[$n][1] = $_session_id; $n++; } $n=0; foreach ($picked_date as $_picked_date) { $bigar[$n][2] = $_picked_date; $n++; } $n=0; foreach ($picked_room as $_picked_room) { $bigar[$n][3] = $_picked_room; $n++; } $n=0; which is then updated in the database using: foreach ($bigar as $part) { mysql_query("UPDATE... } Right - so far so good. The updating works fine with existing fields, but if I remove some of the fields (via JS) it obviously only updates the remaining in the array. The left out will be ignored. On the same note I would like to add fields as well, so - the bottom line - I want to: - UPDATE rows, where the ID is in the array - DELETE existing rows, WHERE the ID is NOT in the array - CREATE new rows for the new fields in the array (not having an ID) How can this be achieved? Hope someone can help... hey guys im really stuck and need help with this. im trying to update a mysql table from an array. basically i have 12 records what are pulled using a query. each record has its own ID to distinguish it from the rest. i then have 2 tick boxes for the user to select one for accept and one for deny. what i need is for the query to look at each record on the table and insert the selection boxes value if ticked. my code is below but i have altered it to the hilt and cannot get it to work so please help Code: [Select] <?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/Apollo/dbc.php"; include_once($path); $rs_results = mysql_query("SELECT * FROM off WHERE IsItAuthorised='0' and isitsick='0' ORDER BY DayOff"); ?> <html> <head> <title>Administration Main Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <?php if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $approve = $_POST['approve']; $deny = $_POST['deny']; $limit = count($id); $values = array(); // initialize an empty array to hold the values for($k=0;$k<$limit;$k++){ $msg[] = "$limit Holidays Updated"; $id[k] = $id; $approve[k] = $approve; $deny[k] = $deny; $id = implode($id); $approve = implode( $approve); $deny = implode( $deny); $query = "UPDATE off SET Authorised = $approve , Deny = $deny WHERE OffID = $id " ; } $Event = "INSERT INTO events (UserName, Event ) VALUES ('$_SESSION[user_name]', 'Entered New KPI' )"; echo $query; if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { mysql_query($Event); echo "<div class=\"msg\">" . $msg[0] . "</div>"; } } ?> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="14%" valign="top"><?php ?> </td> <td width="74%" valign="top" style="padding: 10px;"> <p><?php if(!empty($msg)) { echo $msg[0]; } ?></p> <p> <?php $cond = ''; $sql = "select * from off "; $rs_total = mysql_query($sql) or die(mysql_error()); $total = mysql_num_rows($rs_total); ?> <p> <form name "searchform" action="/Apollo/Admin/HolidayRequests.php" method="post"> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr class="mytables"> <td width="4%"><font color="white"><strong>ID</font></strong></td> <td width="4%"> <font color="white"><strong>Staff Member</font></strong></td> <td width="10%"><font color="white"><strong>Day Off</font></strong></div></td> <td width="10%"><font color="white"><strong>Is It Authorized</font></strong></div></td> <td width="15%"> </td> </tr> <tr> <td> </td> <td width="10%"> </td> <td width="17%"><div align="center"></div></td> <td> </td> </tr> <?php while ($rrows = mysql_fetch_array($rs_results)) {?> <tr> <td><input name="id[]" id="id[]" size="4" value="<?php echo $rrows['OffID'];?>" /></td> <td><?php echo $rrows['StaffMember']; ?></td> <td><?php echo date('d/m/Y', strtotime($rrows['DayOff']));?></div></td> <td> <span id="approve<?php echo $rrows['id']; ?>"> <?php if(!$rrows['IsItAuthorised']) { echo "Pending"; } else {echo "Authorized"; }?> </span> </td> <td> <input type="hidden" name="approve[]" id="approve[]" value="0"> <input type="checkbox" name="approve[]" id="approve[]" value="1"> Approve <input type="hidden" name="deny[]" id="deny[]" value="0"> <input type="checkbox" name="deny[]" id="deny[]" value="1"> Deny </td> </tr> <?php } ?> </table> <input name="submit" type="submit" id="submit" value="Submit"> </form> </p> <?php ?> <p> </p> <p> </p> <p> </p> <p> </p></td> <td width="12%"> </td> </tr> </table> </body> </html> Hi all, I'm trying to figure out how to update values I have in an array. I'm a bit stuck and hope someone can advise. I've read about all sorts of bits but I'm stuck, I dont really understand associative so thought I'd post an example of my code for someone to hopefully assist.. so, if I have a table $fees, which is filled from a select, for example.. select * from `cases_fees`. "contains fields for id, type, amount and others now I want to do is foreach through that table and update a field called `amount`. but I cant understand the foreach >= as key part. what is a key and what isn't ? (I know what the keys of the db table arem but that's not the same eh?) I'm fine with normal foreach, but I cant update back to the array so I need to find out how to be able to set valus back into $fees[`amount`] hope someone can point me in the right direction please... Ted Hi all, Im having some trouble trying to update my table using an array, It processes and doesnt throw up any errors so im at a loss as to whats happening as its not updating my table? my page is Code: [Select] mysql_select_db($database_saucy_connection, $saucy_connection); $query_allphotos = "SELECT * FROM model_login, model_pictures WHERE model_pictures.user_id=model_login.id"; $allphotos = mysql_query($query_allphotos, $saucy_connection) or die(mysql_error()); $row_allphotos = mysql_fetch_assoc($allphotos); $totalRows_allphotos = mysql_num_rows($allphotos); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center">Picture Name</td> <td align="center">Upload Date</td> <td align="center"><strong>No Downloads</strong></td> <td align="center"> Approved? </td> <td align="center"> Hunnies Gallery </td> <td align="center"> Hunks Gallery </td> <td align="center"> Default Pic </td> <td align="center"> Theme Pic </td> <td align="center"> Homepage Pic </td> </tr> <?php while($rows=mysql_fetch_array($allphotos)){ ?> <tr> <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><? $user_picture[]=$rows['user_picture']; ?><?php echo $rows['user_picture']; ?></td> <td align="center"><? $user_picture_date[]=$rows['user_picture_date']; ?><?php echo $rows['user_picture_date']; ?></td> <td align="center"><? $picture_downloads[]=$rows['picture_downloads']; ?><?php echo $rows['picture_downloads']; ?></td> <td align="center"><? $user_pic_approval[]=$rows['user_pic_approval']; ?><?php echo $rows['user_pic_approval']; ?> </td> <td align="center"><? $hotties_gallery[]=$rows['hotties_gallery']; ?><?php echo $rows['hotties_gallery']; ?> </td> <td align="center"><? $hunks_gallery[]=$rows['hunks_gallery']; ?><?php echo $rows['hunks_gallery']; ?> </td> <td align="center"><? $default_pic[]=$rows['default_pic']; ?><?php echo $rows['default_pic']; ?> </td> <td align="center"><? $theme_gallery[]=$rows['theme_gallery']; ?><?php echo $rows['theme_gallery']; ?> </td> <td align="center"><? $homepage=$rows['homepage_pic']; ?><?php echo $rows['homepage_pic']; ?> </td> </tr> <?php } ?> <tr> <td colspan="5" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $model_pictures SET user_pic_approval='$user_pic_approval[$i]', hotties_gallery='$hotties_gallery[$i]', hunks_gallery='$hunks_gallery[$i]', default_pic='$default_pic[$i]', theme_gallery='$theme_gallery[$i]', homepage_pic='$homepage_pic[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } Hi all. I am currently trying to build a site that will allow users to manage a self build house project. This is my first php MySQL project but I am getting to grips with it ok. I am currently building this for my own benefit at the moment as I am about to undertake a self build. I have a few questions to put to you guys. This is where I am at the moment I have a database table named materials which contains a list of materials , quantities, unit of measurement, id. My site has a user control panel that has separate forms for each stage of the build where the user enters the quantities of the materials, and the data is displayed as an array for each stage. So, heres question 1. I want to take the data and use it to generate a form which will be completed by several merchants.... so it will display similar to the array but will be a form with a text box for price where the merchant will insert a price. i.e <?php mysql_connect ("localhost", "test", "test") or die ('I cannot connect to the database becuase: ' . mysql_error()); mysql_select_db ("matquant"); // query $query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); // results while ($row = mysql_fetch_array($query)) { echo "<br />" .$row ['id']. " " .$row ['material']. " " .$row['quantity']. " " .$row['unit']. "<br />";} echo mysql_error(); ?> I want to add a text input box form which will allow the merchant to add a price to each item that is added to the array. so there the merchant will be able to give me a price for all the items in my list. Heres where I get stuck.... as I am constantly adding to the list I have no fixed amount of items so the form for the merchants needs to be built dynamically, ie each time I add a new material, I want the merchants form to get a new input. I am not very good at describing my problems but I am hoping that someone can help me out from the information given. Thats the first question. i like to get issues sorted in small bites. the second is the next big bite, I want to send the form above to several merchants, so I intend to have a user database with a user for each merchant, and link the tables so that each merchant can provide a price for each material, ideally the form would highlight the materials that have not had a price yet. I'm looking for advice on the best method of doing this basically but if I can get the first question sorted that would be great.. I have the test site local but can upload it it to the server if it will help you see what im trying to do. Any help that anyone can give will be greatly appreciated. I have the below code: Code: [Select] <div> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td colspan="7" valign="top"> <form enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <?php if (isset($submit)) { // UPDATE QUERY CODE WHEN SUBMIT IS ENTERED $str = implode(';',$_POST['checks']); $insert = "UPDATE members SET settings='$str' WHERE member_id='$_SESSION[SESS_MEMBER_ID]'"; if (@mysql_query($insert)) { ?> <script type="text/javascript"> document.location.replace('settings-tests.php'); </script> <?php } else { echo('Error in you submission:' . mysql_error() . "<br /><br />" . $sql); } } ?> <?php { $result=mysql_query("SELECT * FROM members WHERE member_id = '$_SESSION[SESS_MEMBER_ID]'"); $row = mysql_fetch_array($result); $arr = array(); $arr = explode(';',$row['settings']); $member_id = $row['member_id']; $firstname = $row['firstname']; $lastname = $row['lastname']; $email = $row['email']; $membership = $row['membership']; $roles = $row['roles']; $login = $row['login']; ?> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td colspan="2"><h3>Test Results Settings</h3></td> <td width="25%"> </td> <td width="25%"><?php echo $_SESSION['SESS_MEMBER_ID']; ?></td> </tr> <tr> <td colspan="4"><em>To turn off individual results gloablly across the site please use the following sliders (all results already added will also be made invisable)<br /> <br /> </em></td> </tr> <tr> <td width="25%"><div align='right'><strong><em>Salinity:</em></strong></div></td> <td width="25%"><input type="checkbox" value="test1" name="checks[]2" <?php echo (in_array("test1",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Phosphate:</em></strong></div></td> <td><input type="checkbox" value="test9" name="checks[]10" <?php echo (in_array("test9",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>PH:</em></strong></div></td> <td><input type="checkbox" value="test2" name="checks[]3" <?php echo (in_array("test2",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>potassium:</em></strong></div></td> <td><input type="checkbox" value="test10" name="checks[]11" <?php echo (in_array("test10",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Ammonia:</em></strong></div></td> <td><input type="checkbox" value="test3" name="checks[]4" <?php echo (in_array("test3",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Iodine:</em></strong></div></td> <td><input type="checkbox" value="test11" name="checks[]12" <?php echo (in_array("test11",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Nitrite:</em></strong></div></td> <td><input type="checkbox" value="test4" name="checks[]5" <?php echo (in_array("test4",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Strontium:</em></strong></div></td> <td><input type="checkbox" value="test12" name="checks[]13" <?php echo (in_array("test12",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Nitrate:</em></strong></div></td> <td><input type="checkbox" value="test5" name="checks[]6" <?php echo (in_array("test5",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Silica:</em></strong></div></td> <td><input type="checkbox" value="test13" name="checks[]14" <?php echo (in_array("test13",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Calcium:</em></strong></div></td> <td><input type="checkbox" value="test6" name="checks[]7" <?php echo (in_array("test6",$arr) ? "checked" : ''); ?>/></td> <td><div align='right'><strong><em>Temperatu </em></strong></div></td> <td><input type="checkbox" value="test14" name="checks[]" <?php echo (in_array("test14",$arr) ? "checked" : ''); ?>/></td> </tr> <tr> <td><div align='right'><strong><em>Magnesium:</em></strong></div></td> <td><input type="checkbox" value="test7" name="checks[]8" <?php echo (in_array("test7",$arr) ? "checked" : ''); ?>/></td> <td></td> <td></td> </tr> <tr> <td><div align='right'><strong><em>Alkalinity:</em></strong></div></td> <td><input type="checkbox" value="test8" name="checks[]9" <?php echo (in_array("test8",$arr) ? "checked" : ''); ?>/></td> <td colspan="2" align="right"></td> </tr> <tr> <td colspan="4" align="right"><table border="0" cellspacing="0" cellpadding="8"> <tr> <td><input class="Button" type="submit" name="submit" value="Submit" /></td> <td><input class="Button-alt" type="reset" name="reset" value="Reset" /></td> </tr> </table></td> </tr> </table> <br /> </form><?php } ?> </td> </tr> </table> </p> </div> This pulls data in array from database for example: test1;test9;test2;test3;test4;test5;test6;test14;test7;test8 The SELECT query works fine if i manually edit the array in the database to make the boxes checked or not. Problem seems to be when i click submit, its not updating the DATABASE?! i recently upgraded to PHP5 eventually and it seems it stopped working arond then. any help please. Code: [Select] //Update the ranks if (isset($_POST['update'])){ $ids = implode(",", array_map('intval', $_POST['m'])); $ranks = implode(",", array_map('intval', $_POST['ranks'])); if ($ids < 1) message("Incorrect Data"); if ($ranks < 0) message("Incorrect Data"); foreach ($_POST['ranks'] as $muffins){ $db->query('UPDATE friends set rank = '.$muffins.' WHERE friend_id = '.$_POST['m'].' AND user_id = '.$pun_user['id'].'') or error('Unable to remove users from online list', __FILE__, __LINE__, $db->error()); } redirect("s.php?section=Friends","Thanks, Ranks Updated"); } friend_id = becomes blank? the $muffins work and each rank is showed respectivaly, but how can I join in another array in the loop? I tried another foreach in the loop, but it just made me run double the queries. Any idea? $muffins is working fine and $_POST['m'] is giving no results i need to have 2 dynamic arrays in that forloop, the $ranks and the $ids Hi there. I have a page that lists the records of a table. On this page I want to be able to edit/update two attributes of these records by a checkbox - e.g., being able to update multiple records at once. I thought the smartest way to achieve this would be by using arrays. I've implemented one checkbox array and have two fields. Currently when I change both fields and hit submit, the height value changes and the name value is changed to the last record displayed in the table. E.g. Height - 170 -> 160 -> UPDATED TO 160 Name - Test1 -> Test2 -> UPDATED TO VICTORY (last record listed in table; displayed alphabetically) My code is as follows: <?php include("connection.php"); $conn = oci_connect($UName,$PWord,$DB) or die("Database Error - Contact Admin - sjrei5@student.monash.edu"); if (empty($_POST["check"])) { $query = "SELECT * FROM HORSE ORDER BY HORSE_NAME"; $stmt = oci_parse($conn,$query); oci_execute($stmt); $Horses = oci_fetch_array ($stmt); ?> <form method="post" action="horse_multi.php"> <table border="1" id="customers"> <tr> <th>ID</th> <th>EDIT</th> <th>NAME</th> <th>HEIGHT</th> </tr> <?php while ($Horses = oci_fetch_array ($stmt)) { ?> <tr> <td><?php echo $Horses["HORSE_ID"]; ?></td> <td align="center"><input type="checkbox" name="check[]" value="<?php echo $Horses["HORSE_ID"]; ?>"></td> <td align="center"><input type="text" size="5" name="<?php echo $Horses["HORSE_ID"]; ?>" value="<?php echo $Horses["HORSE_HEIGHT"]; ?>"></td> <td align="center"><input type="text" size="20" name="HORSE_NAME" value="<?php echo $Horses['HORSE_NAME']; ?>"></td> </tr> <?php } ?> </table><p /> <h4><input type="submit" value="Update Selected Horses"></h4> </form> <?php oci_free_statement($stmt); } else { foreach($_POST["check"] as $horse_id) { $query = "UPDATE HORSE SET HORSE_NAME= '$_POST[HORSE_NAME]', HORSE_HEIGHT = ".$_POST[$horse_id]." WHERE HORSE_ID ='".$horse_id."'"; $stmt = oci_parse($conn,$query); oci_execute($stmt); } } ?> </body> </html> Any ideas? Hi, I have the following code which basically checks an array of input textboxes and inserts them into a database table: public function insertAuthor($authArray, $PCorder=0) { $query = sprintf('SELECT Pid, Pname FROM People WHERE Pname IN(\'%s\') ORDER BY Pid ASC', implode('\',\'', $authArray)); $result = mysql_query($query); $maxquery = "SELECT MAX(CPid) as max FROM ConfPaper WHERE CPRid = ".$_GET['CPRid']; $maxresult = mysql_query($maxquery); $max = mysql_fetch_array($maxresult); $CPid = $max['max']; if($result && mysql_num_rows($result) > 0) { $sqlValues = array(); while(list($PId, $PName) = mysql_fetch_row($result)) { if(in_array($PName, $authArray)) { $sqlValues[] = sprintf("(%d, 1, ".$CPid.", %d, now(), 0)", $PId , $PCorder++ ); // Author already exists within the Pname table // remove user from $authArray $key = array_search($PName, $authArray); unset($authArray[$key]); } } $sql = "INSERT INTO PeopleCon(Person_id, PCHid, Pid, PCorder, PCdateadded, PCdeleted) VALUES \n"; $sql .= implode(",\n", $sqlValues); $result = mysql_query($sql); } // If there are Authors left within the $authArray // Add them to the Pname table if(count($authArray) > 0) { People::insertPersons($authArray); // call insertPersons method for remaining authors $this->insertAuthor($authArray, $PCorder); // insert the remaining auhtors into PeopleCon } } insertPersons method is as such: public function insertPersons($PName) { $query = "INSERT INTO People (Pname) VALUES ('" . implode("'), ('", $PName) . "')"; $result = mysql_query($query); } The logic works by firstly checking to see if an Author exists, if so they are inserted into PeopleCon, if not they are inserted into People AND PeopleCon. This all works perfectly.. But now what i need to do is build an update method where users can edit the authors also. The logic would be more or less the same, but i wouldn't neeed an insertpersons method, i would need to change it, and also the script would edit the existing authors, and if anymore are added apply the same method as above. Can anyone help me out as to how i should go about tackling this? Thanks Kind regards Billy Hello all, im new to this forum, im a noobie, just started coding about 3 weeks ago, don't be too hard on me.
My question is I wan't to check for challenges a user's team posted that have not been excepted after 1 day, then auto refund the user's team back there credits, and then also delete all the challenges. So far here is my code.
//Delete all matches not accepted after 1 day $arrayin = array(); $autorefund = mysql_query("SELECT * FROM `challenges` WHERE `a` = " . $team['id'] . " " . "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < " . ((int) time()) . ")"); if (mysql_num_rows($autorefund) > 0) { while ($autorefund = mysql_fetch_assoc($autorefund)) { $arrayin[] = $autorefund['id']; mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits'] . " " . "WHERE `id` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ")"); mysql_query("DELETE FROM `challenges` WHERE `a` IN " . "(" . mysql_real_escape_string(implode(',', $arrayin)) . ") " . "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 " . "AND (`expires` < " . ((int) time()) . ")"); } }FYI here is the code i had, it works fine, however it will only delete 1 challenge per team, not all of there challenges. $autorefund = mysql_query("SELECT * FROM `challenges` WHERE `a` = " . $team['id'] . " AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < ".((int)time()).")"); if (mysql_num_rows($autorefund) > 0) { while ($autorefund = mysql_fetch_assoc($autorefund)) { if ($autorefund['accepted'] == 0 and $autorefund['expires'] < time()) { mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits'] . " WHERE `id` = " . $team['id'] . ""); mysql_query("DELETE FROM `challenges` WHERE `a` = " . $team['id'] . " AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < ".((int)time()).")); } } }I inherited this script, its using deprecated statements, i know im not skilled to rewrite the entire site to use prepared statements. As you can see, this array is setting each colorvalue to each idvalue. Code: [Select] <?php include("config.php"); include("db.php"); $arrid=$_POST['id']; $arrcolor=$_POST['color']; foreach ($arrid as $idvalue) { foreach ($arrcolor as $colorvalue) { $sql = "UPDATE colors SET color='$colorvalue' WHERE id = '$idvalue'"; mysql_query($sql) or die("Error: ".mysql_error()); echo $sql; } } header("Location: " . $config_basedir . "adminhome.php"); ?> Now, here's what I'm trying to do:
First, I have a file filled with data like such:
title
tag
1
2
Title
Description
Etc
Next, I upload that file to my site which then proceeds to make an array with said data and then inserts it into my database. But this is not the intended behavior. Right now, if I upload the same file again, it will re-insert everything and duplicate all entries.
What I want to do is check if the data in the file has already been added, do nothing. If it's been modified, I want to update the database where changes have been made and not duplicate anything.
Currently, my code does all that except one thing whre I'm really stuck: it won't update the changes from the file to the database. I've tried echoing everything and it's to be working except for the query so I take it the error is in there but I can't find it... I'm still learning PHP and MySQL so I thought maybe somebody could help indicate where or what I'm doing wrong in the query. Thanks in advance !
Here's my attempt at doing so:
$register_ep_data = array( 'show' => $name, 'season' => $srNum, 'ep' => $epNum, 'app_name' => $epName, 'tag' => $tag, 'app_about' => $desc, 'app_website' => $imdb, 'app_release' => $release, 'type' => $type, 'app_code' => $Frame ); array_walk($register_ep_data, 'array_fu'); $fields = '`' . implode('`, `', array_keys($register_ep_data)) . '`'; $data = '\'' . implode('\', \'', $register_ep_data) . '\''; $epFound = false; $id = 0; while ($ep_list_data = mysql_fetch_array($turtle)) { $id = $ep_list_data['app_id']; $currentNAME = $ep_list_data['app_name']; $SERIES = $ep_list_data['show']; if ($SERIES == $name) { if ($currentNAME == $epName) { $epFound = true; break; } } } if ($epFound) { mysql_query("UPDATE `games` SET ($fields) VALUES ($data) WHERE `app_id` = '$id'"); } else { mysql_query("INSERT INTO `games` ($fields) VALUES ($data)"); }A few explanations: $fields would equal to something like: `show`, `season`, `ep`, etc... and $data to 'example', '1', '2', 'etc' |