Anyone see anything wrong with this?

Discussion in 'PHP' started by belgin fish, Jan 26, 2010.

  1. #1
    Hey, well I got this query and it keeps saying

    mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource

    I can't figure out what's wrong, could anyone gimme a hand?\


    <?if ((isset($_GET['action'])) && ($_GET['action'] == "updatesettings")) {
        $newpass =  mysql_real_escape_string($_POST['newpassword']);
    	$newpassmd5 = md5($newpass);
    	
    	$query ="UPDATE settings SET value='".$newpass."' WHERE name= 'admin_password'";
        $result = mysql_query($query);
    	if (mysql_affected_rows()>0) {
    		echo "<font color=\"#00CC00\">Password Updated</font><br/>";
    	}
    	else
    	{
    		echo "<font color=\"#FF0000\">Password Update Failed</font><br/>";
    	}
    	$query ="UPDATE settings SET value='".$newpassmd5."' WHERE name= 'admin_password_md5'";
        $result = mysql_query($query);
    	if (mysql_affected_rows()>0) {
    		echo "<font color=\"#00CC00\">Password Updated</font><br/>";
    	}
    	else
    	{
    		echo "<font color=\"#FF0000\">Password Update Failed</font><br/>";
    	}
    }
    ?>
    PHP:
    But I'm pretty sure the queries getting through and changing the password.

    Thanks
     
    belgin fish, Jan 26, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sounds like you have an error in your query or you've not connected to the database properly.
    Straight after the query run the mysql_error() and see what it produces
     
    JAY6390, Jan 27, 2010 IP
  3. jwitt98

    jwitt98 Peon

    Messages:
    145
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I could be wrong, but I didn't think you needed to concatenate your query like that. Shouldn't it just be:

    $query ="UPDATE settings SET value='$newpass' WHERE name= 'admin_password'";
    Code (markup):
     
    jwitt98, Jan 27, 2010 IP
  4. The Men

    The Men Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    mysql_affected_rows() takes exactly 1 argument ( you haven't passed one ).
     
    The Men, Jan 27, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good point The Men lol. I didnt even see that
     
    JAY6390, Jan 27, 2010 IP
  6. belgin fish

    belgin fish Well-Known Member

    Messages:
    1,544
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    185
    Digital Goods:
    2
    #6
    I have connected to the db, and I had modified it to set the db connection into variables and asigned an argument to mysql_affected_rows() and it didn't change anything.
     
    belgin fish, Jan 27, 2010 IP
  7. The Men

    The Men Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    
    $connection = mysql_connect("localhost", "root", "password") or die(mysql_error());
    $database = mysql_select_db("sample") or die(mysql_error());
    
    $query = mysql_query("UPDATE users SET password='sample' WHERE id=1") or die(mysql_error());
    
    if (mysql_affected_rows($connection) == 0) {
    	echo "Nothing updated!";
    } else {
    	echo "Updated: " . mysql_affected_rows($connection);
    }
    
    ?>
    PHP:
    Works just fine - executed it 2 times .. Updated: 1 changed to Nothing updated!.
    Are you sure you have a connection ?
     
    The Men, Jan 27, 2010 IP
  8. belgin fish

    belgin fish Well-Known Member

    Messages:
    1,544
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    185
    Digital Goods:
    2
    #8
    ya I'm sure i got one cause it actually does update the value in the database.
     
    belgin fish, Jan 27, 2010 IP