Page 1 of 1

Searching for another way to...

Posted: Mon Apr 25, 2011 8:54 pm
by musbah
I can't explain what i'm having a problem with easily but here it is,
i made it look for rows and their names but a button can't see the difference between 2 arrays in a while function because you click it at the end i need a work arround to be able to see which array the button needs to change the number for, here is the code:

Code: Select all

$sql="SELECT * FROM anime WHERE username = '$username' ";
$result=mysql_query($sql) or die(mysql_error());
while($rows=mysql_fetch_array($result))
{

	


 if (isset($_POST['submit1'])) 
 { mysql_query("UPDATE anime SET episode='$rows[episode]'+1 where name='$rows[name]' AND username='$username'") or die("cannot send your password"); 
 header("Location: {$_SERVER['HTTP_REFERER']}");
 }
 
  if (isset($_POST['submit2'])) 
 { mysql_query("UPDATE anime SET episode='$rows[episode]'-1 where name='$rows[name]' AND username='$username'") or die("cannot send your password"); 
 header("Location: {$_SERVER['HTTP_REFERER']}");
 }
?>
<form name="myform" action="atview.php" method="post">
<table>
<tr>
<center><td>anime:</td></center>
<center><td><?php echo "$rows[name]"; ?></td></center>
</tr>
<tr>
<center><td>episode:</td></center>
<center><td><input name="submit2" type="submit" value="-" /><?php echo "$rows[episode]"; ?><input name="submit1" type="submit" value="+"  /> </td></center>
</tr>
</table>
</form>
<?php
}
mysql_close();
i can't explain it well i hope you can understand what i mean when you read the code, the name that changes is for both names because name='$rows[name]' is just all of the rows...
what kind of function or thing can be a workaround for this??

Re: Searching for another way to...

Posted: Mon Apr 25, 2011 9:47 pm
by fugix
im not 100% sure on what you are asking...but i would start out by making 2 separate forms for each submit button..and tell them to do different things when clicked

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 5:59 am
by musbah
that's the the problem what i mean is there is a button when clicked that adds for example a +1 to $rows[episode] the problem is i can't specify which row it should update because $rows[name] is an array so the update would change all of the array functions to $rows[episode] +1 and not just one of the arrays...

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 6:09 am
by superdezign
Is this an administrator-only piece of code or will users be using this to navigate? If it's administrative, then you're problem is that you surround "$rows['episode']" with quotation marks, telling MySQL that it's a string value when it's actually an integer. Also, since that value is already in there, there's no need to specify the value. Just use:

Code: Select all

UPDATE `anime` SET `episode` = `episode` + 1 where `name` = '$rows[name]' AND `username` = '$username'
However, if this is code is for users (which it looks like), then you are doing it wrong. Updating a database for each particular user is unnecessary, at least in this manner. You could determine the episode that they are on by using a query string or $_SESSION variables. The query string would be the best IMHO. It allows for them to make direct links to the page that they are on to friends and such.

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 6:18 am
by musbah
thanks i ll try to work on that but that's not really the problem i'm having the thing is i need to be able to know the name of each individual thing because i want the button to work for one example from the name arrays for example but it won't work this way so i need a way to make it work, what i'm trying to do is when they click on the + button for example it adds an episode number and that works but it adds it to all the names on the list and not just the one i'm on don't know how i can make it just add to a specific one, where `name` = '$rows[name]' doesn't work since it takes everything...

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 6:22 am
by superdezign
That's because you are selecting every row in your SELECT statement with the same `username` value. Try specifying an `name` value in the WHERE clause, too.

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 6:28 am
by musbah
I know that that's the problem thing is i can't really specify a value because i don't know how i could specify one thing in the array

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 6:38 am
by superdezign
What array are you talking about? And how do you specify which anime they are viewing in the first place?

Re: Searching for another way to...

Posted: Tue Apr 26, 2011 7:07 am
by musbah
that's the problem i need a way to specify which anime but can't find a way to do it, been searching for hours on the web can't find a good way to specify which anime is which.