Why won't this query update correctly?

Discussion in 'PHP' started by lilgezuz, Nov 12, 2011.

  1. #1
    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:
     
    Solved! View solution.
    lilgezuz, Nov 12, 2011 IP
  2. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    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'].
     
    dthoai, Nov 12, 2011 IP
  3. lilgezuz

    lilgezuz Active Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Still does not update the database, but it does redirect as if there was a result.
     
    Last edited: Nov 12, 2011
    lilgezuz, Nov 12, 2011 IP
  4. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #4
    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);
    }
    }
     
    avinash gaud, Nov 12, 2011 IP
  5. #5
    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):
     
    dthoai, Nov 13, 2011 IP
  6. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #6
    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.
     
    avinash gaud, Nov 13, 2011 IP
  7. lilgezuz

    lilgezuz Active Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #7
    It's not doing or showing any thing when I put the print in. Where would I define the variables at?
     
    lilgezuz, Nov 14, 2011 IP
  8. lilgezuz

    lilgezuz Active Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #8
    I got it to work by defining my varibles like mentioned above after my while statement. Thanks for the help.
     
    lilgezuz, Nov 14, 2011 IP
  9. zhouhounan

    zhouhounan Greenhorn

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    nice post....
     
    zhouhounan, Nov 14, 2011 IP