SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: updating script

  1. #1
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    updating script

    please this update script not working
    this is the script
    PHP Code:
    require_once("include/session.php");
    require_once(
    "include/dataconnect.php"); 
    require_once(
    "include/functions.php"); 
    //"<pre>";print_r($_POST); echo "</pre>";
    if(array_key_exists('item'$_POST)){
        
    $items $_POST['item'];
        
        
    //Loop through $_POST items, updating the database for each item
        
    foreach ($items as $item) { 
            
    $Pquantity intval($item[0]); 
            
    $Pidno intval($item[1]); 
            
    $queryreg mysql_query("
                UPDATE repplac
                     SET Pquantity = 
    {$Pquantity}
                     WHERE
                           Pidno = 
    {$Pidno}
                     AND
                           Uname = '
    {$_SESSION['username']}'
            "
    ) or die(mysql_error()); 
    this is the form
    PHP Code:
    <?php

      
    // Output start of table and form
      
    echo "
        <form action='updatepplac.php' method='Post' class='slistbar'>
        <table border='1'>
          <tr>
            <th>SHOP NAME</th>
            <th>PRODUCT NAME</th>
            <th>PRODUCT SIZE</th>
            <th>PRODUCT COLOUR</th>
            <th>PRODUCT QUANTITY</th>
            <th>PRICE</th>
            <th></th>
          </tr>"
    ;

      
    // Get DB results and loop, outputting table rows with counter
      
    $pplresult mysql_query("SELECT * FROM repplac") or die(mysql_error());
      for (
    $i 0$row mysql_fetch_assoc($pplresult); $i++) {
        echo 
    "
          <tr>
            <td>"
    .htmlspecialchars($row['Sname'])."</td>
            <td>"
    .htmlspecialchars($row['Pname'])."</td>
            <td>"
    .htmlspecialchars($row['Psize'])."</td>
            <td>"
    .htmlspecialchars($row['Pcolour'])."</td>
            <td>
              <input type='text' name='item[
    $i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />
              <input type='hidden' name='item[
    $i][Pidno]' id='Pidno' value='".htmlspecialchars($row['Pidno'])."' />
            </td>
            <td>"
    .htmlspecialchars($row['Price'])."</td>
            <td><a href='deleteproduct.php?del="
    .htmlspecialchars($row['Pidno'])."'>delete</a></td>
          </tr>"
    ;
      }

      
    // Close table and form
      
    echo "
        </table>
        <input type='submit' value='Submit' />
        </form>"
    ;

    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $queryreg mysql_query(
                UPDATE repplac 
                     SET Pquantity = 
    {$Pquantity} 
                     WHERE 
                           Pidno = 
    {$Pidno} 
                     AND 
                           Uname = '
    {$_SESSION['username']}
            "

    Check if this returns true or false. If it returns false, you have an error in your SQL connection, query.
    If it returns true, the query should be ok.

    Also, shouldnt you make a variable with the query which then gets run by mysql_query, and not a variable with mysql_query in it? I don't remember making queries this way.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    it returned true, and also i have assigned a variable but it is not updating

    PHP Code:
    if(array_key_exists('item'$_POST)){
        
    $items $_POST['item'];
        
        
        
    //Loop through $_POST items, updating the database for each item
        
    foreach ($items as $item) { 
            
    $Pquantity intval($item[0]); 
            
    $Pidno intval($item[1]); 
             
    $queryreg ="
                UPDATE repplac
                     SET Pquantity = 
    {$Pquantity}
                     WHERE
                           Pidno = 
    {$Pidno}
                     AND
                           Uname = '
    {$_SESSION['username']}'
            "

           
    mysql_query($queryreg) or die(mysql_error()); 
           
        } 

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Did you check if the value of $Pidno is what you expect it to be?

    Also, is the value of $Pquantity correct or is it the old quantity instead of the new one?

  5. #5
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    when i use
    PHP Code:
    "<pre>";print_r($_POST); echo "</pre>"
    it comes out with the new values,(showing that it has been posted
    but the code does not update with the post data,
    also i use affected _num_rows, it comes back with a result ,
    but in real sense it is not updating

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $queryreg =
                UPDATE repplac 
                     SET Pquantity = 
    {$Pquantity} 
                     WHERE 
                           Pidno = 
    {$Pidno} 
                     AND 
                           Uname = '
    {$_SESSION['username']}
            "

    Echo this just before you send the query, does the query work in phpmyadmin or similar database tool?

    edit: code block

  7. #7
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    the code works in phpmy admin,
    that is i substitute the variables say with a number, and the uname, with a proper username

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ow... try change your query like this:

    PHP Code:
    $queryreg ="  
                UPDATE repplac  
                     SET Pquantity = '" 
    $Pquantity "'
                     WHERE  
                           Pidno = '" 
    $Pidno "'  
                     AND  
                           Uname = '" 
    $_SESSION['username'] . "'  
            "


  9. #9
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    tried it still did not update

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    When you echo that query and paste it in phpmyadmin?

  11. #11
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    when i echo it, like substituting it with values, and real user name, it updates the db

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You did this?

    PHP Code:
    $queryreg ="   
                UPDATE repplac   
                     SET Pquantity = '" 
    $Pquantity "' 
                     WHERE   
                           Pidno = '" 
    $Pidno "'   
                     AND   
                           Uname = '" 
    $_SESSION['username'] . "'   
            "


    echo 
    $queryreg; exit; 
    Can you show us what is echooed?

  13. #13
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    brense it so funny when i echo it say the original quantity is 5, it comes back with value of 1
    also say the original quantity is 5 and i change to 7, when it is echoed it comes back with the value of 1

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I see, but the rest of the values are okay?

    Can you modify your code like this:
    PHP Code:
    if(array_key_exists('item'$_POST)){ 
        
    $items $_POST['item']; 
         
         
        
    //Loop through $_POST items, updating the database for each item 
        
    foreach ($items as $item) {  

            echo 
    $item[0] . "\n";

            
    $Pquantity intval($item[0]); 

            echo 
    $Pquantity;exit;
     
            
    $Pidno intval($item[1]);  
             
    $queryreg =
                UPDATE repplac 
                     SET Pquantity = 
    {$Pquantity} 
                     WHERE 
                           Pidno = 
    {$Pidno} 
                     AND 
                           Uname = '
    {$_SESSION['username']}
            "
    ;  
           
    mysql_query($queryreg) or die(mysql_error());  
            
        } 
    And tell me if the two values are the same or not? It should look like:
    1
    1

  15. #15
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    323
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    i filled the form initially with two items, one with quantity 4 and the other with quantity 5, and changed it to 8 and 9 resp, and when i submitted it it came back with 0(that is echoed 0)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •