PHP - Loop Thru Multiple Update
I have a code that I am using to update multiple products at one time instead of one at a time. It works great except that my processing page is not updating the very last product. My process page is receiving all of my products as I know how many products that I am updating and I have my process page tell me how many products are updated as well as listing the name of each product updated. It is always the last product that is not updated.
Here is my process update code. Code: [Select] <?php $size = count($_POST['casketid']); //$size = 306; $i = 1; while ($i < $size) { $product=$_POST['productid'][$i];//getting case # $price=$_POST['price'][$i]; $model=$_POST['model'][$i]; $active=$_POST['active'][$i]; //Getting pricerange if(($price > "0") && ($price <= "1000.00")) $pricerange = "1"; elseif(($price >= "1001.00") && ($price <= "1500.00")) $pricerange = "2"; elseif(($price >= "1501.00") && ($price <= "2000.00")) $pricerange = "3"; elseif(($price <= "2001.00") && ($price <= "2500.00")) $pricerange = "4"; elseif(($price <= "2501.00") && ($price <= "3000.00")) $pricerange = "5"; elseif(($price >= "3001.00") && ($price <= "3500.00")) $pricerange = "6"; elseif(($price >= "3501.00") && ($price <= "4000.00")) $pricerange = "7"; elseif(($price >= "4001.00") && ($price <= "4500.00")) $pricerange = "8"; elseif(($price >= "4501.00") && ($price <= "4500.00")) $pricerange = "9"; elseif($price >= "5001.00") $pricerange = "10"; $query = "UPDATE products SET price='$price', pricerange='$pricerange', active='$active' WHERE productid='$product'"; mysql_query($query) or die ("Error in query: $query"); echo "$model = $price & active = $active</em><br />$size<br />"; ++$i; } ?> Similar TutorialsAs the title says I need help with multiple update system.. Can't really get it to work at all >.< Here's some code: $selectt = mysql_query("SELECT * FROM exercises WHERE user='" . $_SESSION["user"] . "' AND exname='" . $_GET["edit"] . "'"); echo '<form method="POST" action="exercises.php?edited">'; echo '<div id="newexx">'; while($raw = mysql_fetch_array($selectt)) { echo '<input type="text" name="up[' . $raw["id"] . ']" value="' . $raw["swe"] . '">'; echo '<input type="text" name="up1[' . $raw["id"] . ']" value="' . $raw["eng"] . '"><br>'; echo '<input type="hidden" name="id[' . $raw["id"] . ']">'; } echo '</div>'; echo '<input type="submit" value="Update Exercise">'; echo '</form>'; if(isset($_GET["edited"])) { $swe = $_POST["up"]; $eng = $_POST["up1"]; $id = $_POST["id"]; foreach($id as $value => $index) { $update = "UPDATE exercises SET swe='".$swe."', eng='".$eng."' WHERE id='".$index."'"; mysql_query($update); } Would be thankful for reply! Ok I'm trying to insert multiple rows by using a while loop but having problems. At the same time, need to open a new mysql connection while running the insert query, close it then open the previous mysql connection. I managed to insert multiple queries before using a loop, but for this time, the loop does not work? I think it is because I am opening another connection... yh that would make sense actually? Here is the code: $users = safe_query("SELECT * FROM ".PREFIX."user"); while($dp=mysql_fetch_array($users)) { $username = $dp['username']; $nickname = $dp['nickname']; $pwd1 = $dp['password']; $mail = $dp['email']; $ip_add = $dp['ip']; $wsID = $dp['userID']; $registerdate = $dp['registerdate']; $birthday = $dp['birthday']; $avatar = $dp['avatar']; $icq = $dp['icq']; $hp = $dp['homepage']; echo $username." = 1 username only? :("; // ----- Forum Bridge user insert ----- $result = safe_query("SELECT * FROM `".PREFIX."forum`"); $ds=mysql_fetch_array($result); $forum_prefix = $ds['prefix']; define(PREFIX_FORUM, $forum_prefix); define(FORUMREG_DEBUG, 0); $con = mysql_connect($ds['host'], $ds['user'], $ds['password']) or system_error('ERROR: Can not connect to MySQL-Server'); $condb = mysql_select_db($ds['db'], $con) or system_error('ERROR: Can not connect to database "'.$ds['db'].'"'); include('../_phpbb_func.php'); $phpbbpass = phpbb_hash($pwd1); $phpbbmailhash = phpbb_email_hash($mail); $phpbbsalt = unique_id(); safe_query("INSERT INTO `".PREFIX_FORUM."users` (`username`, `username_clean`, `user_password`, `user_pass_convert`, `user_email`, `user_email_hash`, `group_id`, `user_type`, `user_regdate`, `user_passchg`, `user_lastvisit`, `user_lastmark`, `user_new`, `user_options`, `user_form_salt`, `user_ip`, `wsID`, `user_birthday`, `user_avatar`, `user_icq`, `user_website`) VALUES ('$username', '$username', '$phpbbpass', '0', '$mail', '$phpbbmailhash', '2', '0', '$registerdate', '$registerdate', '$registerdate', '$registerdate', '1', '230271', '$phpbbsalt', '$ip_add', '$wsID', '$birthday', '$avatar', '$icq', '$hp')"); if (FORUMREG_DEBUG == '1') { echo "<p><b>-- DEBUG -- : User added: ".mysql_affected_rows($con)."<br />"; echo "<br />-- DEBUG -- : Query used: ".end($_mysql_querys)."</b></p><br />"; $result = safe_query("SELECT user_id from ".PREFIX_FORUM."users WHERE username = '$username'"); $phpbbid = mysql_fetch_row($result); safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('2', '$phpbbid[0]', '0', '0')"); safe_query("INSERT INTO `".PREFIX_FORUM."user_group` (`group_id`, `user_id`, `group_leader`, `user_pending`) VALUES ('7', '$phpbbid[0]', '0', '0')"); mysql_close($con); } include('../_mysql.php'); mysql_connect($host, $user, $pwd) or system_error('ERROR: Can not connect to MySQL-Server'); mysql_select_db($db) or system_error('ERROR: Can not connect to database "'.$db.'"'); } So I need to be able to insert these rows using the while loop.. how can I do this? I really appreciate any help. Hello Still pretty new to php, so I need a little help with the below code (It's not pretty, but for now I just need it to work correct ) Code: [Select] ... while($row = mysql_fetch_array($result)){ if($row['user_group_id'] <= 2){ $multiplier = 1; } if($row['user_group_id'] == 6){ $multiplier = 2; } if($row['user_group_id'] == 7){ $multiplier = 3; } if($row['user_group_id'] == 8){ $multiplier = 4; } $totalshares = ((($row['activity_blog']*$multiplier)/2)+(($row['activity_comment']*$multiplier)/10)); $set_shares=mysql_query("UPDATE phpfox_user SET bod_shares=$totalshares WHERE profile_page_id=0"); echo " User id: ".$row['user_id']." multiplier:".$multiplier." User group:".$row['user_group_id']." - blog:".$row['activity_blog']." - comment:".$row['activity_comment']." totalshares= ".$totalshares.""; echo "<br />"; } ?> Problem is the UPDATE part "bod_shares=$totalshares", which just inserts "0". How do I get it to update the row with the correct value for each row? The echo shows the right values and calculation for $totalshares. I created a form that would display all the information that was in my database within a textbox using a while loop. The infomation is able to be displayed but when trying to update only the information in the last textbox gets updated and the other textboxes will just stay the same. <?php include ('connection.php'); ?> <form method="post" action="update.php"> <?php $display = mysql_query("SELECT * FROM book ORDER BY id asc") or die("Table Error"); $ID = $_POST['id']; $Title = $_POST['title']; $Cost = $_POST['cost']; $Stock = $_POST['stock']; while ($row = mysql_fetch_assoc($display)) { echo "ID :<input type=\"text\" name=\"".id."\" value = \"". $row['id'] ."\"/readonly>"; echo "Title :<input type=\"text\" name=\"".title."\" value = \"" . $row['title'] . "\"/>"; echo "Cost :<input type=\"text\" name=\"".cost."\" value = \"" .$row['cost'] . "\"/>"; echo "Stock :<input type=\"text\" name=\"".stock."\" value = \"" . $row['stock'] . "\"/>"; mysql_query("UPDATE book SET title = '" .$Title. "', cost = '" .$Cost. "', stock = '" .$Stock. "' WHERE id = '" .$ID. "'"); } ?> <input type="submit" name="update" value="Update" /> Im not sure why it is doing this if someone could tell me it would be a big help Thanks Hi folks... I really didn't know how to title this thread so I hope it makes sense Here is what I am trying to do: Create an editable list of items, price and stock using the 'while' loop - this I can easily do like this... Quote echo "<table width=\"100%\" cellspacing=0>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td width=40% class=item_list>$row[mbp_name]</td>"; echo "<td align=right width=20% class=item_list>"; echo "<input value=\"$row[mbp_price]\" size=\"2\" maxlength=\"5\" type=\"text\" name=\"mbp_price\" /></td>"; echo "<td align=right width=20% class=item_list>"; echo "<input value=\"$row[mbp_stock]\" size=\"2\" maxlength=\"5\" type=\"text\" name=\"mbp_stock\" /></td>"; echo "<td align=right width=20% class=item_list>" . $row['mbp_stock']*$row['mbp_price'] . "</td>"; echo "</tr>"; } echo "</tr></table>"; If I have the update outside the loop then there are only 3 fields that contain a value after the loop is completed ie: mbp_item = the last record name of the table, mbp_price=the last record price and mbp=the last record stock. If I have the update inside the loop the table will update each record but the user may decide to cancel - so then the data in the table is wrong. As an extra feature, I would also be inetersted to know the best way to check that an entered value is valid before the user moves the cursor to a new field? Does anyone have a clue what the best way to do this please? i have the following problem: im trying to update the region_id in citylist2233 according to the region_id in regionlist2233. i can compare regionlist2233.region_name = worldregionlist22.regionname and get the new and old id im messing the loop somewhe Code: [Select] <?php //Insert Unique region id and update $query1 = 'SELECT country_id from worldcountrylist where enable =1 LIMIT 1 '; $results1 = mysql_query($query1); if (mysql_num_rows($results1) > 0) { while($row1 = mysql_fetch_array($results1)) { $query2 = 'SELECT * from citylist2233 where country_id ="'.$row1['country_id'].'" '; $results2 = mysql_query($query2); if (mysql_num_rows($results2) > 0) { while($row2 = mysql_fetch_array($results2)) { //echo $row2['city_id'].''.$row2['city_name'].'-'.$row2['region_id'].'-'; echo $row2['city_name'].'-'.$row2['city_id'].'-'.$row2['region_id'].'<BR>'; $query3='select regionlist2233.region_name ,regionlist2233.region_id from worldregionlist22 LEFT JOIN regionlist2233 on worldregionlist22.regionname = regionlist2233.region_name AND worldregionlist22.region_id <> regionlist2233.region_id AND worldregionlist22.country_id = regionlist2233.country_id WHERE regionlist2233.region_id="'.$row2['region_id'].'" AND regionlist2233.country_id="'.$row1['country_id'].'" '; $results3 = mysql_query($query3); if (mysql_num_rows($results3) > 0) { while($row3 = mysql_fetch_array($results3)) { echo $updatequery='UPDATE citylist2233 SET region_id='.$row3['region_id'].' WHERE region_id='.$row2['region_id'].' AND country_id='.$row1['country_id'].'';}} $update = mysql_query($updatequery) } } } } ?> I am trying to get info from a table, add rows in that table, and put the added value back into the table in another field...I want to add field1 and field2 of every row and put the total in the total row. Heres what the echo gives me Susan 14 Mary 18 Bob 13 Sam 21 heres the database mysql> SELECT total_val FROM table; +-----------+ | total_val | +-----------+ | 21 | | 21 | | 21 | | 21 | +-----------+ heres the code.... <?php require("connection.php"); mysql_select_db("database", $connection); echo "<br />"; $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { $values = ($row['field1'] + $row['field2']); $sql=mysql_query("UPDATE table SET total = '$values'"); echo $row['user_name'] . " " . $values; echo "<br />"; } ?> also note that if I put a INT in place of $values - the echo changes to match...why?? PLEASE help...I have been working on this for 2 entire days.... Hi there, hope have some help. I have a form where a user select time and days of the week (monday, tuesday..) that he is available. The insert script works fine, it insert and the days of week he checked in the table, the only problme is when i try to update each of them.
I used this code above, but is not working, instead of updating/inserting days that is selected is inserting multi-records with the same day of the week value.
here it is:
foreach($weekDay as $DaysofWeek){ $sql_days = "UPDATE available SET day_time = '$day_time', week_day = '$DaysofWeek' WHERE user_id = '$id_user'"; $update_availability = mysql_query($sql_days); } This way seems not good. <?php $username ="MANCENT"; $user_input_x = _xmouse.movement ;//FLASH $user_input_y = _ymouse.movement ;//FLASH while(true) { $updatepos = mysql_query("UPDATE users_float SET user_input_x = '$user_input_x' , user_input_y = '$user_input_y' WHERE username = '$username'"); } This way works but whats a better way? ?> I am trying to create a quick script to update all my products in my product table, I want to set a prodOrder starting at 1 and incrementing by 1 for each product in the catalogID group. eg if I have 12 product with a catalogID of 1 then I want to number the prodOrder for each product in my table 1-12 and so on for each catalogID group whats the best way to do this here is my starter code but no where near performing this function $sql=("SELECT * FROM product ORDER BY productID ASC GROUP BY catalogID"); $result=mysql_query($sql); while($row=mysql_fetch_array($result)) { $catID=$row['catalogID']; $id=$row['productID']; $value='1'; foreach ($catID AS $id => $value) { $qty=("UPDATE product SET prodOrder='$value' WHERE productID='$id'"); $change=mysql_query($qty); echo $row['name']." from $catID - order = $value<br/>"; $value++ } } Hi coders,
im trying to update multiple rows of record using check-box and have a default value "RCV" and now im getting difficulties since im new in CI and any body can help and solve or can improve this code below.
view
<tr> <td><input type = "hidden" value = "<?php echo $row->pull_out_id ?>" name = "po_id"></td> <td><input type="checkbox" class="case" name="case" value="rcv"/></td> </tr>controller public function update_po_receive() { $po_id = $this->input->post('po_id'); $this->public_base_model->update_po_receive($po_id); redirect('public_base/search_stocks', 'refresh'); }model public function update_po_receive($po_id) { $data = array('temp' => $this->input->post('case')); $this->db->where('pull_out_id', $po_id); $this->db->update('pull_out', $data); } This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=321230.0 I have ran this code before and it worked but not it won't update. Can anyone see what I am doing wrong? Code: [Select] <? if(isset($_POST[sort_invest])) { // if form was submitted $id = $_POST['sel_record']; $sort = $_POST['sort']; for($i=0;$i<$count;$i++){ $sql = "UPDATE invest SET id='$id[$i]', sort='$sort[$i]' WHERE id='$id[$i]'"; $sql_result = mysql_query($sql,$con) or die (mysql_error()); } } // if form was submitted or if it came from another page if (isset($_POST['order_invest']) || isset($_POST['sort_invest'])) { ?> <p><strong>Order Invest</strong></p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <? $sql = "SELECT id, sort, title FROM invest ORDER BY sort ASC"; $sql_result = mysql_query($sql,$con) or die(mysql_error()); while ($row = mysql_fetch_array($sql_result)) { $id = $row["id"]; $title = $row["title"]; $sort = $row["sort"]; ?> <input type="hidden" name="sel_record[]" value="<? echo "$row[id]"; ?>"> <table width="545" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="69"><input type="text" name="sort[]" value="<? echo "$row[sort]"; ?>" size="3"></td> <td width="476"><? echo "$row[title]"; ?></td> </tr> </table> <br> <? } } ?> <input name="sort_invest" type="submit" value="Sort"> </form> hey can someone advice me on how to make my update script update multiple rows. here's what i've got so far: update.php <?php require('connect.php'); $update = mysql_query("SELECT DISTINCT rideName FROM ride ORDER BY rideName DESC" ); $numrows = mysql_num_rows($update); echo "<form action='mysql_update.php' method='POST'> <select name='updateRide' >"; while ($row=mysql_fetch_assoc($update)) { #$rideID = $row ['rideID']; $rideName = $row ['rideName']; #$seatNumber = $row ['seatNumber']; #$time = $row ['time']; #$price = $row ['price']; echo "<option value='$id'> $rideName </option>"; } echo "</select> <input type='text' name='tochange'> <input type='submit' name='submit' value='change'> </form>"; ?> and now the handler script: mysql_update.php <?php require ('connect.php'); $updateRide=$_POST['updateRide']; $tochange=$_POST['tochange']; if ($updateRide&&$tochange) { $change = mysql_query("UPDATE ride SET rideName='$tochange' WHERE $id='updateRide'"); } ?> When I try this I get nothing, no changes. In the table there is 3 distinct rideNames each have more than 20 rows. Is there a way using the drop down box and ext field like i Have to update all of the distinct rideNames in the table? Thanks. Ok, so I need to know how I can get these codes to interact and work to update multiple rows/columns in a database. this code is what pulls the query and displays info(it displays fine): Code: [Select] <?php if (isset($_POST['submitted'])) { include('connect1.php'); $category = $_POST['category']; $criteria = $_POST['criteria'] ; $query = ("SELECT name, badges, rank, userid FROM members WHERE $category LIKE '%".$criteria."%'"); $result = mysqli_query($dbcon, $query) or die('error getting data'); $num_rows = mysqli_num_rows($result); echo "$num_rows results found"; echo "<table width=\"896\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"; echo "<tr bgcolor=\"#F7E496\"><td bgcolor=\"#F7E496\"><strong>name</strong></td><td bgcolor=\"#F7E496\" ><strong>Merit Badges</strong></td><td bgcolor=\"#F7E496\"><strong>Rank</strong></td><td bgclor=\"#F7E496\"></td></tr>"; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {$color = ($color == 'white')?'#fffccc':'white'; echo "<tr bgcolor='$color'><td> "; echo $row['name']; echo " </td><td> <form action=\"scout.php\" method=\"post\"> <input type=\"text\" name=\"userid\" id=\"userid\" value='".$row['userid']."'> <textarea name=\"badges[]\" id=\"badges\" cols=\"40\" rows=\"3\" type=\"textarea\">".$row['badges']."</textarea></td><td> <span class=\"adfa\"> </span> <select name=\"rank[]\" id=\"rank\"> <option value=\"Scout\">Scout</option> <option value=\"Tenderfoot\">Tenderfoot</option> <option value=\"Second Class Scout\">Second Class Scout</option> <option value=\"First Class Scout\">First Class Scout</option> <option value=\"Star Scout\">Star Scout</option> <option value=\"Life Scout\">Life Scout</option> <option value=\"Eagle Scout\">Eagle Scout</option> <option value=\"\" selected=\"selected\">".$row['rank']."</option> </td><td>"; } echo "</td></tr>"; echo "</table>"; echo "<input type=\"submit\" name='submit[]' id=\"submit\" value=\"Save\" /> </form>"; } ?> and this code is what is supposed to update the database: Code: [Select] if ($_POST[submit]){ foreach ($_POST[rank] as $key => $value) { $badges = $_POST['email1'][$key]; $userid1 = $_POST['userid'][$key]; $q = "UPDATE members SET badges='$badges', rank='$rank' where userid='$userid1'"; $sql = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); printf("Records updated: %d\n", mysql_affected_rows());} exit(); } // close if post again, I am really new to arrays and don't know If i am using them right here......... Need help in updating the tables I have array of data coming from a form which I'm inserting in my DB. I have 5 tables product filter product_filter heater product_heater Im able to put the data in the "product", "filter" & "heater" tables but i dont know how to put data inside the "product_filter" & "product_heater" table. Any help or direction to any tutorails is appreciated. My Tables structu product id int(5) product text cost text details text filter id int(5) filter text imgpath text product_filter id int(5) id_product int(5) id_filter int(5) heater id int(5) heater text imgpath text product_heater id int(5) id_product int(5) id_heater int(5) Code: [Select] // Product data Update $name = mysql_real_escape_string($_POST['product']); $cost = mysql_real_escape_string($_POST['cost']); $details = mysql_real_escape_string($_POST['details']); $sql_title = "INSERT INTO product ( id , product , cost , details , ) VALUES ( NULL , '$name' , '$cost' , '$details')"; if (!mysql_query($sql_title,$con)) { die('Error: ' . mysql_error()); } echo "records for product added<br />"; // Filter update // This is the array which is coming from the form /* Array ( [0] => ehiem [1] => Hagan [2] => Rena [3] => jobo ) Array ( [0] => img1.jpg [1] => img2.jpg [2] => img3.jpg [3] => img4.jpg ) */ $filtername = mysql_real_escape_string($filtername); $filterimgpath = mysql_real_escape_string($filterimg); $combined_array = array_combine($filtername, $filterimgpath); $values = array(); foreach ($combined_array as $filtername => $filterimgpath) { $values[] = "('$filtername', '$filterimgpath')"; } $sql = "INSERT INTO filter (filter , imgpath) VALUES " . implode(', ', $values); //echo $lastid = mysql_insert_id()."<br />"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "records added<br />"; //Product Filter Update table // This is where Im stuck. Not able to even think of anything.... // heater update // This is the array which is coming from the form /* Array ( [0] => ehiem [1] => Dolphin [2] => Rena [3] => jobo ) Array ( [0] => img1.jpg [1] => img2.jpg [2] => img3.jpg [3] => img4.jpg ) */ $heatername = mysql_real_escape_string($heatername); $heaterimgpath = mysql_real_escape_string($heaterimg); $combined_array = array_combine($heatername, $heaterimgpath); $values = array(); foreach ($combined_array as $heatername => $heaterimgpath) { $values[] = "('$heatername', '$heaterimgpath')"; } $sql = "INSERT INTO heater (heater , imgpath) VALUES " . implode(', ', $values); //echo $lastid = mysql_insert_id()."<br />"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "records added<br />"; //Product heater Update table // This is where Im stuck. Not able to even think of anything.... I have a php script that I've been running that seems to have been working but now I'm wondering if some of my logic is potentially off. I select records from a db table within a date range which I put into an array called ```$validCount``` If that array is not empty, that means I have valid records to update with my values, and if it's empty I just insert. The trick with the insert is that if the ```STORES``` is less than the ```Quantity``` then it only inserts as many as the ```STORES``` otherwise it inserts as many as ```Quantity```. So if a record being inserted with had Stores: 14 Quantity:12
Then it would only insert 12 records but if it had It would only insert 1 record. In short, for each customer I should only ever have as many valid records (within a valid date range) as they have stores. If they have 20 stores, I can have 1 or 2 records but should never have 30. It seems like updating works fine but I'm not sure if it's updating the proper records, though it seems like in some instances it's just inserting too many and not accounting for past updated records. This is the logic I have been working with:
if(!empty($validCount)){ for($i=0; $i<$row2['QUANTITY']; $i++){ try{ $updateRslt = $update->execute($updateParams); }catch(PDOException $ex){ $out[] = $failedUpdate; } } }else{ if($row2["QUANTITY"] >= $row2["STORES"]){ for($i=0; $i<$row2["STORES"]; $i++){ try{ $insertRslt = $insert->execute($insertParams); }catch(PDOException $ex){ $out[] = $failedInsertStore; } } }elseif($row2["QUANTITY"] < $row2["STORES"]){ for($i=0; $i<$row2["QUANTITY"]; $i++){ try{ $insertRslt = $insert->execute($insertParams); }catch(PDOException $ex){ $out[] = $failedInsertQuantity; } } } }
Let's say customer 123 bought 4 of product A and they have 10 locations
customerNumber | product | category | startDate | expireDate | stores Because they purchased less than their store count, I insert 4 records. Now if my ```$validCheck``` query selects all 4 of those records (since they fall in a valid date range) and my loop sees that the array isn't empty, it knows it needs to update those or insert. Let's say they bought 15 this time. Then I would need to insert 6 records, and then update the expiration date of the other 9 records.
customerNumber | product | category | startDate | expireDate | stores There can only ever be a maximum of 10 (store count) records for that customer and product within the valid date range. As soon as the row count for that customer/product reaches the equivalent of stores, it needs to now go through and update equal to the quantity so now I'm running this but it's not running and no errors, but it just returns back to the command line $total = $row2['QUANTITY'] + $validCheck; if ($total < $row2['STORES']) { $insert_count = $row2['QUANTITY']; $update_count = 0; } else { $insert_count = $row2['STORES'] - $validCheck; // insert enough to fill all stores $update_count = ($total - $insert_count); // update remainder } for($i=0; $i<$row2['QUANTITY']; $i++){ try{ $updateRslt = $update->execute($updateParams); }catch(PDOException $ex){ $failedUpdate = "UPDATE_FAILED"; print_r($failedUpdate); $out[] = $failedUpdate; } } for($i=0; $i<$insert_count; $i++){ try{ $insertRslt = $insert->execute($insertParams); }catch(PDOException $ex){ $failedInsertStore = "INSERT_STORE_FAILED!!!: " . $ex->getMessage(); print_r($failedInsertStore); $out[] = $failedInsertStore; } }```
hi i was working on a code where i need to use multiple categories for a post, i get help before to add post in multi categories, now i need help on what would be my query to do update for a multiple categories post, to insert a new post with multiple categories i used this $values = array(); foreach($_POST['categories'] as $catID) { $values[] = "($postID, $catID)"; } $sql = "INSERT INTO `relation` (`postid`, `catid`) VALUES " . implode(', ', $values); $query = $db->query($sql); so if a post had 3 categories it will add in relation table, now i wants to update that post and modify the categories, all other part is done only problem is to update this relational category table which have two columns only associated with post. i tried like this but did not work $sql = "UPDATE relation SET `catid` = " . implode(', ', $values) . " WHERE postid ='$id' "; please any help ok, so I have this code: Code: [Select] <?php if (isset($_POST['submitted'])) { include('connect1.php'); $category = $_POST['category']; $criteria = $_POST['criteria'] ; $query = ("SELECT name, badges, rank, userid FROM members WHERE $category LIKE '%".$criteria."%'"); $result = mysqli_query($dbcon, $query) or die('error getting data'); $num_rows = mysqli_num_rows($result); echo "$num_rows results found"; echo "<table width=\"896\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"; echo "<tr bgcolor=\"#F7E496\"><td bgcolor=\"#F7E496\"><strong>name</strong></td><td bgcolor=\"#F7E496\" ><strong>Merit Badges</strong></td><td bgcolor=\"#F7E496\"><strong>Rank</strong></td><td bgclor=\"#F7E496\"></td></tr>"; while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {$color = ($color == 'white')?'#fffccc':'white'; echo "<tr bgcolor='$color'><td> <input type=\"text\" name=\"userid\" id=\"userid\" value='".$row['userid']."'>"; echo $row['name']; echo " </td><td> <form action=\"scout.php\" method=\"post\"> <textarea name=\"badges\" id=\"badges\" cols=\"40\" rows=\"3\" type=\"textarea\">".$row['badges']."</textarea></td><td> <span class=\"adfa\"> </span> <select name=\"rank\" id=\"rank\"> <option value=\"Scout\">Scout</option> <option value=\"Tenderfoot\">Tenderfoot</option> <option value=\"Second Class Scout\">Second Class Scout</option> <option value=\"First Class Scout\">First Class Scout</option> <option value=\"Star Scout\">Star Scout</option> <option value=\"Life Scout\">Life Scout</option> <option value=\"Eagle Scout\">Eagle Scout</option> <option value=\"\" selected=\"selected\">".$row['rank']."</option> </td><td>"; echo "<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Save\" /> "; echo "</form>"; } echo "</table>"; echo "</td></tr>"; } ?> and this is the code that does the posting: Code: [Select] <?php session_start(); // Must start session first thing /* Created By Adam Khoury @ [URL=http://'http://www.flashbuilding.com/']www.flashbuilding.com[/URL] -----------------------June 20, 2008----------------------- */ // Here we run a login check if (!isset($_SESSION['id'])) { echo 'Please <a href="login.php">log in</a> to access your account'; exit(); } //Connect to the database through our include include_once "connect_to_mysql.php"; // Place Session variable 'id' into local variable $userid = $_SESSION['id']; // Query member data from the database and ready it for display // Process the form if it is submitted if ($_POST['submit']) { $badges = $_POST['badges']; $userid = $_POST['userid']; $rank = $_POST['rank']; $sql = mysql_query("UPDATE members SET badges='$badges', rank='$rank' WHERE userid='".$_GET['userid']."'"); printf("Records updated: %d\n", mysql_affected_rows()) ; exit(); } // close if post ?> So what I need to know is how I can make it so that each result has it's user id associated with it......and I can update multiple rows at once according to each userid that is associated..........right now I can only get it to do one if I add on ?userid=".$row['userid']." in the first piece of code.......how can I make it update lets say 78 as soon as I click submit...... PS I know there will be a submit button with each row right now.......I will move it out of the loop later..... This has been bugging me for quite a while and I've tried so many different things and just run into all kinds of problems... What I'm doing or trying at least is pulling data from a table displaying it into a multiple select box allowing the user the oppertunity to remove one or more options. As the code stands now I'm getting no value at all in the post variable, by taking the [] brackets out of the inputs name i was able to get the first selected option but the php code was then unable to remove that value from the database. I hope I'm explaining this well enough but the code follows I'd really appreciate some help or direction on this one. The FORM Code: [Select] <FORM action='' method='post'> <select size='8' multiple name='remove_player[]'> <?php foreach ($members as $key => $mem) { if ($mem != "") { $mem = str_replace("-", " ", $mem); $mem = ucwords($mem); echo ("<option value='" . $mem . "'>" . $mem . "</option>"); } } ?> </select> and then the handler code: Code: [Select] $to_remove = $_POST['remove_player']; $as_team = str_replace(" ", "-", $teamname); $as_team = strtolower($as_team); $for_remove = "SELECT * FROM teams WHERE name = '$as_team'"; $for_query = (mysql_query($for_remove)); foreach ($to_remove as $y => $z) { $z = str_replace(" ", "-", $z); $z = strtolower($z); } while($arr_remove = mysql_fetch_array($for_query)) { foreach ($arr_remove as $a => $b) { foreach ($to_remove as $c => $d) { if ($b == $d) { unset($arr_remove[$a]); } } } foreach ($arr_remove as $e => $f) { $add_replace = (mysql_result($for_query, 0, 'members')); $replace = ($add_replace . "\r\n" . $f . "\r\n"); $replace_mem = "UPDATE teams SET members = '$replace'"; $replace_q = (mysql_query($replace_mem)); } } echo "Member List successfully updated.<br>"; foreach ($to_remove as $h => $g) { echo ($g . " removed from the team.<br>"); } Also any time i try to print any part of the array/s I get no values but the database is being updated with 2 line breaks and a "3" at the end of the table which has me a bit confused as well. Thanks! |