-1

I'm trying to update some records in my database via php. This is the part of my code that returns error:

<?php
    //turn on error reporting
    ini_set('display_errors',1);
    error_reporting(E_ALL);

    // Check if button name "Submit" is active, do this
    if(isset($_POST['Submit']))
    {
        $count=mysql_num_rows($result);
        for($i=0;$i<$count;$i++)
        {

     if(isset($title[$i],$descr[$i],$price[$i],$cname[$i],$pid[$i]))
            {
                $query = "
                   UPDATE products
                   SET title='$title[$i]',
                       descr='$descr[$i]',
                       price='$price[$i]',
                       cname='$cname[$i]'
                   WHERE pid='$pid[$i]'
               ";

                $upd = mysql_query($query) or die(mysql_error());
            }
            else
            {
                $upd = FALSE;
                echo "One of the variables isn't set.\n<br/>";
            }
        }

        if($upd)
        {
            echo "Successful";
            echo "<BR>";
            //display_manager_menu();
        }
        else {
            echo "Something wrong";
        }    
    }
    mysql_close();
    ?>

It returns: "Notice: Undefined variable: upd ". Since I set $upd as mysql_query result, I have no idea why I get this message. Any ideas?

Suspicius
  • 41
  • 1
  • 2
  • 9
  • Change `$upd=mysql_query($query);` to `$upd=mysql_query($query) or die(mysql_error());` and see what's going wrong. – Amal Murali Sep 29 '13 at 09:40
  • what is the value of $count ? – Software Guy Sep 29 '13 at 09:40
  • @AmalMurali Thanks for the instant reply! I tried it but unfortunately it returns the same – Suspicius Sep 29 '13 at 09:42
  • @SoftwareGuy This is it: $count=mysql_num_rows($result); – Suspicius Sep 29 '13 at 09:43
  • @Suspicius: 1. Turn on error reporting. 2. Use `var_dump` everywhere and test if the variables are what you think it is. 3. Indent your code properly. 4. Do not blindly assume that the query executed successfully. 5. Always make sure the variables are set before trying to use them. -- your code should look something like [this](http://pastebin.com/7D8RVbun). – Amal Murali Sep 29 '13 at 10:08
  • @AmalMurali Your reply was quite helpful to understand the whole poiont my my query. I followed what you show me and I get a number of these messages "One of the variables isn't set. " and "Something wrong". – Suspicius Sep 29 '13 at 10:20
  • @Suspicius: So, use `var_dump` and try to find out **why** those variables aren't set. I don't see you defining those variables in your current code. Where do they come from? – Amal Murali Sep 29 '13 at 10:24

4 Answers4

0

Change:

if($upd){

to:

if(isset($upd) && $upd){

If $_POST['submit'] isn't set, the variable $upd won't be set, either.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
kao3991
  • 402
  • 2
  • 8
  • 3
    This doesn't solve the issue -- rather hides it. – Amal Murali Sep 29 '13 at 09:41
  • I got rid of the notice. But my problem exists since when clicking on Submit button I get numerous notices about undefined variables of `title`, `price`, `descr`,`cname`and `pid` in my $query. – Suspicius Sep 29 '13 at 09:49
  • Which means your data is not being posted as no data is being assigned to those variables and therefore they are undefined. Try var_dump($_POST) and see what you get – The Baker Sep 29 '13 at 09:55
0

The problem is, that $udp is declared within a if-condition. When $_POST['Submit'] isn’t set, then $udp doesn’t get set either. You can solve this by initializing the variable before the if(...):

$udp = false; // initialize $udp before conditional
if(isset($_POST['Submit'])){
    for($i=0;$i<$count;$i++){
        $query="UPDATE products SET title='$title[$i]', descr='$descr[$i]', price='$price[$i]', cname='$cname[$i]' WHERE pid='$pid[$i]'";
        $upd=mysql_query($query);
    }
}

In this case, you initialize $udp with false, since mysql_query() also returns false on failure.

jotaen
  • 6,739
  • 1
  • 11
  • 24
-1

did you corectly close the php file with the ?> ? i've had this kind of problems solved by simply closing the file correctly.

vascofmdc
  • 13
  • 1
  • 7
-1

In addition to the answers of $upd only being declared in a conditional, you are writing code as if register_globals is on. You should not do this.

Replace $title with $_POST['title'], $descr with $_POST['descr'] and so on. This is easier done if you concatenate properly:

"blah blah blah '".mysql_real_escape_string($_POST['title'][$i])." blah blah blah...";

Also, sanitize inputs.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592