I need help with this query, everything looks correct to me but it doesnt update the name, type or description field for the rows <? $page = basename($_SERVER['PHP_SELF']); $rootBase = $_SERVER["DOCUMENT_ROOT"]; include ($rootBase .'/pubcp/header.php'); include ($rootBase .'/db.php'); $game = $_GET['game']; $notice = $_GET['notice']; $sql = "SELECT * FROM trophies WHERE game_id='$game'"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <div class="grid_16"> <? include ($rootBase .'/pubcp/games/subnav.php'); ?> <!-- CONTENT START --> <div class="grid_16" id="content"> <!-- TITLE START --> <div class="grid_9"> <h1 class="staffinfo">Edit Game Info</h1> </div> <!--RIGHT TEXT/CALENDAR--> <div class="grid_6" id="eventbox"><a href="#" class="inline_tip">Click here for help</a> <div class='hidden'> <div id='inline_example2' title="Help" style='padding:10px; background:#fff;'> <p><strong>Use this page to edit a previous game you added</strong></p> </div></div> </div> <!--RIGHT TEXT/CALENDAR END--> <div class="clear"> </div> <!-- TITLE END --> <!-- #PORTLETS START --> <div id="portlets"> <!-- FIRST SORTABLE COLUMN START --> <div class="column" id="left"> </div> <div class="clear"></div> <!--THIS IS A WIDE PORTLET--> <div class="portlet"> <div class="portlet-header fixed"><img src="/images/user_48.png" width="16" height="16" alt="Latest Registered Users" /><? echo $count?> Current Info </div> <div class="portlet-content nopadding"> <? if ($notice == 'success') { ?> <p class="info" id="success"><span class="info_inner">Successful Update!</span></p><? } ?> <? if ($notice == 'failed') { ?> <p class="info" id="error"><span class="info_inner">Update Failed!</span></p><? } ?> <table width="100%" cellpadding="0" cellspacing="0" id="box-table-a" summary="Employee Pay Sheet"> <thead> <tr> <th scope="col">Trophy ID</th> <th scope="col">Trophy Name</th> <th scope="col">Trophy Tile</th> <th scope="col">Trophy Description</th> <th scope="col">Trophy Type</th> <th scope="col">Game ID</th> </tr> </thead> <tbody> <? while($rows=mysql_fetch_array($result)){ ?> <form name="form1" method="post" action="<? $_SERVER['PHP_SELF'] ?>"> <tr> <td><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td><input type="text" name="name[]" value="<? echo $rows['name']; ?>" id="name" size="30"></td> <td><img src="/<? echo $rows['image']; ?>" /></td> <td><input type="text" name="description[]" value="<? echo $rows['description']; ?>" id="description" size="30"></td> <td><select name="type[]" value="<? echo $rows['type']; ?>" id="type" ><OPTION SELECTED VALUE="">Select...</OPTION><option value="platinum">Platinum</option><option value="gold">Gold</option><option value="silver">Silver</option><option value="bronze">Bronze</option></select> <td><input type="text" name="game_id" size="10" value="<? echo $game ?>"></td> </td> </tr> <? } ?> <tr class="footer"> <td colspan="3"></td> </tr> </tbody> </table> <input type="submit" name="Submit" value="Submit" > </form> </div> </div> <!-- END #PORTLETS --> </div> <div class="clear"> </div> <!-- END CONTENT--> </div> <div class="clear"> </div> </div> <!-- WRAPPER END --> <? if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE trophies SET name='$name[$i]', description='$description[$i]', type='$type[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } if($result1){ ?> <script language="javascript"> location.replace("/pubcp/games/list.php?notice=Updated") </script> <? } include ($rootBase .'/pubcp/footer.php'); ?> PHP:
Cause is in following code: if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE trophies SET name='$name[$i]', description='$description[$i]', type='$type[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } Code (markup): $Submit is always false, so update query is never excuted. Please change $Submit to $_POST['Submit'].
Hello, First check what value your $submit is getting then accordingly write the if condition.From your code it seems that the possible value would be "Submit" so your if condition becomes if($_POST['Submit'] == 'Submit'){ for($i=0;$i<$count;$i++){ $sql1="UPDATE trophies SET name='$name[$i]', description='$description[$i]', type='$type[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } }
Sql query is not valid so database is not updated. Please print $sql1 to see it is valid or not. $name, $description, $type, $id is not assigned anywhere in script, so they cause sql query is not valid. if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE trophies SET name='$name[$i]', description='$description[$i]', type='$type[$i]' WHERE id='$id[$i]'"; [COLOR="#FF0000"]print($sql1);[/COLOR] $result1=mysql_query($sql1); } } Code (markup):
Hello, Ya the variables are not defined anywhere in the code , probably the variables should be declared as an array in the while loop and assigned the values , that way it should work.
I got it to work by defining my varibles like mentioned above after my while statement. Thanks for the help.