Page 1 of 1

Back again, this time my buttons...work! [SOLVED]

Posted: Mon May 19, 2008 1:19 pm
by nightswore
Okay, so I am making a button so that it adds +1 to a value in a given cell in my database. I do this by calling the ID for the row that my cell is in, posting it, then using it to determine where the proper cell is. Then I increase the number by one, and UPDATE the cell with my new value and return myself to the previous page. The button form from my first page looks like this:

Code: Select all

<?php
 
$data = mysql_query("SELECT * FROM dealers") or die(mysql_error());
 
while($info = mysql_fetch_array( $data )) : ?>
 
<td align="center"><?php echo $info['in']?><br>
        <form action="dealercounter.php" method ="post">
        <input name="in" type="hidden" value="<?php print $info['dealership']?>">
    <input type="submit" value="In +1" >
    </form>
    </td>
<? endwhile; ?>
I have everything connecting properly up top and such then when the button is pressed it takes you to:

Code: Select all

<?php
$newname = $HTTP_POST_VARS['in'];
 
@ $db = mysql_pconnect('localhost','****','*******');
 
mysql_select_db(intranet);
 
$result = mysql_query("SELECT * FROM dealers
WHERE dealership='$newname'");
 
while($row = mysql_fetch_array($result))
  {
  echo $row['dealership'] . " " . $row['in'];
  $increment = $row['in'];
  $increment++;
  echo "<br>".$increment;
  }
    mysql_query("UPDATE dealers SET in = '$increment'
        WHERE dealership = '$newname'");
  
  $result2 = mysql_query("SELECT * FROM dealers
WHERE dealership='$newname'");
 
while($row = mysql_fetch_array($result2))
  {
  echo "<br>".$row['dealership'] . " " . $row['in'];
  }
  
  echo '<META HTTP-EQUIV="Refresh" Content="0; URL=dealertrade.php">';    
  exit;
?>
there are two loops in there that I use for testing, the first is to print out the original data, then increments the specified value and then prints the new number. The second loop prints the new data which should be updated as this point, since my update script is between the two. (when testing I leave out the redirect and exit script so that I can see what prints out to make sure its working)

I have this working perfectly on another database, doing the exact same thing. Can anyone let me know WHY this is not behaving here? If you need any additional information I will be happy to provide, just not sure what all you may need.

Edit: It occured to me that I never actually specified the problem, whoops. Anyway the problem is that when I go through all of this the update isn't updating properly. It updates the number but doesn't update the database.


-Z

Re: Back again, this time my buttons only work half time time.

Posted: Mon May 19, 2008 2:20 pm
by Jade
Have you tried printing the query?

Code: Select all

 
 
echo "UPDATE dealers SET in = '$increment'  WHERE dealership = '$newname'";
 
mysql_query("UPDATE dealers SET in = '$increment' WHERE dealership = '$newname'")
or die ('there was an error: ' . mysql_error());
 
If the dealership name has an apostrophe in it or there is no dealership name that may be giving you the problem.

Re: Back again, this time my buttons only work half time time.

Posted: Mon May 19, 2008 2:37 pm
by nightswore
The button isn't accessible unless a line of working data in the table is available.

I just tried printing the query (neat trick I wasn't sure if that would actually work) and it is uploding the right value and checking against the right data. So that doesn't seem to be the issue here.

Like I said, I have this *exact* code working in the *exact* same way on another page with a different set of variables and a different table in the database, and it works perfectly fine. That is the frustrating bit. :banghead:

Re: Back again, this time my buttons only work half time time.

Posted: Mon May 19, 2008 2:49 pm
by Jade
Does the user you're logged into mysql as have update permissions?

Re: Back again, this time my buttons only work half time time.

Posted: Mon May 19, 2008 2:52 pm
by nightswore
Yes, I'm using the administrator log in for the database. Good question though, it is usually something that obvious too, like last time, i forgot to put a simple 'print' command into something, hah.

Re: Back again, this time my buttons only work half time time.

Posted: Mon May 19, 2008 2:58 pm
by Jade
One thing I noticed,

You're running a loop, but then you're refreshing the page at the end of the loop.

Code: Select all

 
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=dealertrade.php">';  
 
If you do that then the page will refresh before it ever gets to the next set in the loop.

Re: Back again, this time my buttons only work half time time.

Posted: Tue May 20, 2008 10:06 am
by nightswore
Okay, so I ended up being able to fix this problem this morning. Apparently for THIS table my database was *really* dense and needed some extra clarification. Here is what I ended up replacing my UPDATE line with so that it would work. I thought I'd share so that this thread was useful in future.

Code: Select all

mysql_query("UPDATE `intranet`.`dealers` SET `in` = '$increment'
    WHERE CONVERT( `dealers`.`dealership` USING utf8 ) = '$newname'");
intranet is the database, dealers is the table, in is the cell I'm modifying, and dealership is the key I was using to identify the cell 'in'. $increment was the data I was updating with, $newname was the name of the dealership, aaaaand the CONVERT/USING was just some crap my database needed I guess.

Hope this was helpful to someone, glad I got it working though!

Re: Back again, this time my buttons only work half time time.

Posted: Tue May 20, 2008 4:02 pm
by Jade
Make sure you put solved in your original post title :) Glad you worked it out.