Not updating the mysql

Discussion in 'PHP' started by killaklown, Aug 19, 2006.

  1. #1
    I have this code:

    
    if($_GET['action'] == "edit") {
       $id = $_GET["id"];
       $ntitle = $_POST['title'];
       $nurl = $_POST['url'];
       $ncategory = $_POST['category'];
       $nimage = $_POST['image'];
       $nvalidate = $_POST['validate'];
       $ndescription = $_POST['description'];
       $ncount = $_POST['count'];
    
    
    
    $sql = mysql_query("UPDATE tutorials SET
    	   			    id='NULL',
    					title='$ntitle',
    					url='$nurl',
    					category='$ncategory',
    					validate='$nvalidate',
    					image='$nimage',
    					count='$ncount',
    					description='$ndescription' 
    			  WHERE id='$id'") or die (mysql_error()); 
          
      echo "Tutorial Edited, Redirecting...";
             echo "<meta http-equiv=Refresh content=3;url=?x=check>";
    exit();
    
    } else {
    
    ?>
    <form name="message" method='post' action='?x=edittutorial&action=edit'>
    	 <b>ID:</b>
         &nbsp;&nbsp;<? echo''.$id.''; ?><br><BR>
         <b>Title:</b><BR> 
         &nbsp;&nbsp;<input name='title' value='<? echo''.$title.''; ?>' size='36' maxlength='255'><br><BR>
         <b>URL:<br></b>
         &nbsp;&nbsp;<input name='url' value='<? echo''.$url.''; ?>' size='36' maxlength='255'><br><BR>
         <b>Category:</b><Br> 
         &nbsp;&nbsp;
    	 <select size="1" name='category' size='36' maxlength='255'>
         <? echo''.$cat.''; ?>
    	 </select>
         <br><br>
    	 <b>Hits:<br></b>
         &nbsp;&nbsp;<input name='count' value='<? echo''.$count.''; ?>' size='36' maxlength='255'><br><BR>
         <b>Image:<br></b>
         &nbsp;&nbsp;<input name='image' size='36' maxlength='255' value='<? echo''.$image.''; ?>'><br><br>
    	 <b>Description:</b><br>
         &nbsp;&nbsp;<input name='description' size='36' maxlength='255' value='<? echo''.$description.''; ?>'><br><br>
         <b>Validate:</b> 0 - Not Validated; 1 - Validated<br>
         &nbsp;&nbsp;<select size='1' name='validate' size='36' maxlength='255'>
         <option>1</option>
         <option>0</option>
         </select><br><br>
         &nbsp;&nbsp;<input type="submit" name="submit" value="Edit Tutorial">&nbsp;&nbsp;
         <input type="reset" name="Reset" value="Reset"></form>
    <?php
    }
    }
    ?>
    
    PHP:
    But it isnt updating the row when i click on submit. Whats wrong with it?
     
    killaklown, Aug 19, 2006 IP
  2. knine143

    knine143 Peon

    Messages:
    186
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    knine143, Aug 19, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    i am, i just posted the part that i think is going wrong.
     
    killaklown, Aug 19, 2006 IP
  4. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does it get as far as saying "Tutorial Edited, Redirecting..."? If so, try doing a SELECT using the ID you're passing in to see if any records match.

    What type is the id column? If it's an integer, take off the '' around $id in the SQL query.
     
    void, Aug 19, 2006 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    The first part of it is:

    
    <? include('admin_header.php'); ?>
    <b>Edit a Tutorial</b><br>
    <hr noshade size="1">
    <?php
    
    include '../database.php';
    
        $sql_query_cat = "SELECT * FROM tutorials WHERE id='$id' ORDER BY category ASC";
        //store the SQL query in the result variable
        $result_cat = mysql_query($sql_query_cat);
    
        if(mysql_num_rows($result_cat))
        {
        //output as long as there are still available fields
        while($row = mysql_fetch_array($result_cat))
        {
    	$category = stripslashes($row["category"]);
    		
            $cat.='<OPTION VALUE="___" selected>'.$category.'</OPTION>';
            $categ = $row[category];
            $categ = stripslashes($categ);
    		$cat.='<option>'.$categ.'</option>';
    		
    		$title = $row["title"];
            $url = $row["url"];
            $id = $row["id"];
    		$image = $row["image"];
    		$description = $row["description"];
    		$count = $row["count"];
       }
    	
    PHP:
     
    killaklown, Aug 19, 2006 IP
  6. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #6
    No i dont get up to Tutorial Edited, and yes the ID is an integer. (also tried taking off the ", didnt do anything)
     
    killaklown, Aug 19, 2006 IP
  7. petronel

    petronel Peon

    Messages:
    113
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    do you get any error mesg ?
    can you PM the link of the site ?
     
    petronel, Aug 19, 2006 IP
  8. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    In which case, it's a problem with
    if($_GET['action'] == "edit")
    and
    action='?x=edittutorial&action=edit'>

    I've nveer tried passing parameters into action, but I'd always try to avoid mixing GETs and POSTs. Try sticking this in instead of action='?x=edittutorial&action=edit'>
    <input type="hidden" name="x" value="edittutorial" />
    <input type="hidden" name="action" value="edit" />
    then change $_GETs to $_POST

    Let me know if that helps.
     
    void, Aug 19, 2006 IP
  9. knine143

    knine143 Peon

    Messages:
    186
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Change

    if(mysql_num_rows($result_cat))
    to
    if(mysql_num_rows($result_cat) != 0)

    You shouldn't have to, but that has fixed things like that before in my code.
     
    knine143, Aug 19, 2006 IP
  10. petronel

    petronel Peon

    Messages:
    113
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    if you put :
    
    echo "<pre>";
    print_r($_GET);
    print_r($_POST);
    
    //before
    if($_GET['action'] == "edit")
    
    PHP:
    what do you get?
     
    petronel, Aug 19, 2006 IP
  11. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #11
    I get a bunch of array stuff...

    I have
    
    <? include('admin_header.php'); ?>
    <b>Edit a Tutorial</b><br>
    <hr noshade size="1">
    <?php
    
    include '../database.php';
    
        $sql_query_cat = "SELECT * FROM tutorials WHERE id='$id' ORDER BY category ASC";
        //store the SQL query in the result variable
        $result_cat = mysql_query($sql_query_cat);
    
        if(mysql_num_rows($result_cat))
        {
        //output as long as there are still available fields
        while($row = mysql_fetch_array($result_cat))
        {
    	$category = stripslashes($row["category"]);
    		
            $cat.='<OPTION VALUE="___" selected>'.$category.'</OPTION>';
            $categ = $row[category];
            $categ = stripslashes($categ);
    		$cat.='<option>'.$categ.'</option>';
    		
    		$title = $row["title"];
            $url = $row["url"];
            $id = $row["id"];
    		$image = $row["image"];
    		$description = $row["description"];
    		$count = $row["count"];
       }
    	
    
    
    
    if($_POST['action'] == "edit") {
       $id = $_POST['id'];
       $ntitle = $_POST['title'];
       $nurl = $_POST['url'];
       $ncategory = $_POST['category'];
       $nimage = $_POST['image'];
       $nvalidate = $_POST['validate'];
       $ndescription = $_POST['description'];
       $ncount = $_POST['count'];
    
    
    
    $sql = mysql_query("UPDATE tutorials SET id='$id',title='$ntitle',url='$nurl',category='$ncategory',validate='$nvalidate',image='$nimage',count='$ncount',description='$ndescription' WHERE id='$id'") or die (mysql_error()); 
          
      echo "Tutorial Edited, Redirecting...";
             echo "<meta http-equiv=Refresh content=3;url=?x=check>";
    exit();
    
    } else {
    
    ?>
    
    <form name="message" method='post'>
    <input type="hidden" name="x" value="edittutorial" />
    <input type="hidden" name="action" value="edit" />
    	 <b>ID:</b>
         &nbsp;&nbsp;<? echo''.$id.''; ?><br><BR>
         <b>Title:</b><BR> 
         &nbsp;&nbsp;<input name='title' value='<? echo''.$title.''; ?>' size='36' maxlength='255'><br><BR>
         <b>URL:<br></b>
         &nbsp;&nbsp;<input name='url' value='<? echo''.$url.''; ?>' size='36' maxlength='255'><br><BR>
         <b>Category:</b><Br> 
         &nbsp;&nbsp;
    	 <select size="1" name='category' size='36' maxlength='255'>
         <? echo''.$cat.''; ?>
    	 </select>
         <br><br>
    	 <b>Hits:<br></b>
         &nbsp;&nbsp;<input name='count' value='<? echo''.$count.''; ?>' size='36' maxlength='255'><br><BR>
         <b>Image:<br></b>
         &nbsp;&nbsp;<input name='image' size='36' maxlength='255' value='<? echo''.$image.''; ?>'><br><br>
    	 <b>Description:</b><br>
         &nbsp;&nbsp;<input name='description' size='36' maxlength='255' value='<? echo''.$description.''; ?>'><br><br>
         <b>Validate:</b> 0 - Not Validated; 1 - Validated<br>
         &nbsp;&nbsp;<select size='1' name='validate' size='36' maxlength='255'>
         <option>1</option>
         <option>0</option>
         </select><br><br>
         &nbsp;&nbsp;<input type="submit" name="submit" value="Edit Tutorial">&nbsp;&nbsp;
         <input type="reset" name="Reset" value="Reset"></form>
    <?php
    }
    }
    ?>
    </div></div>
    <? include('admin_footer.php'); ?>
    PHP:
    and now it will show the Tutorial Edited, Redirecting... but it doesnt update the mysql row.



    Edit nvm, i removed:
    <input type='hidden' name='id' value='<? echo''.$id.''; ?>' />
    PHP:
    When i was working on the things you suggestion, and forgot to put it back in ;)
     
    killaklown, Aug 19, 2006 IP
  12. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Glad to be of assistance :)
     
    void, Aug 19, 2006 IP