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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nightswore
Forum Newbie
Posts: 13
Joined: Thu May 15, 2008 11:26 am

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

Post 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
Last edited by nightswore on Wed May 21, 2008 9:22 am, edited 1 time in total.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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.
nightswore
Forum Newbie
Posts: 13
Joined: Thu May 15, 2008 11:26 am

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

Post 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:
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post by Jade »

Does the user you're logged into mysql as have update permissions?
nightswore
Forum Newbie
Posts: 13
Joined: Thu May 15, 2008 11:26 am

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

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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.
nightswore
Forum Newbie
Posts: 13
Joined: Thu May 15, 2008 11:26 am

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

Post 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!
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post by Jade »

Make sure you put solved in your original post title :) Glad you worked it out.
Post Reply