Jump to content

[SOLVED] Wrong id in URL


Andy17

Recommended Posts

Before you read my code, I want to start out by saying that I'm new to PHP so I cannot promise that my code does not look like a mess to some of the more experienced coders out there! However, I cleaned it up and removed the irrelevant code for you (I have various security checks but they have nothing to do with my problem).

 

My problem is that I have a variable called $number, which stores the id of the MySQL row that is displayed on the page. For example, if $number is 4, then row 4 (id = 4) is displayed from the MySQL database. This all works like a charm, but I want each row to have an individual link, meaning that whenever a user clicks previous/next, the link should be updated so that people can click the link and see the correct row displayed.

 

I coded the above, but there is a problem. The id in the link (randompage.php?id=X, where X should be = $number) is always one step behind the $number. This means that when a user clicks next and $number is set to 1, the id in the URL is 0 and so on. When you go back, you will see the id increase by 1 the first time because it's one step behind.

 

I hope you get my point. If not, please check out the script in action here (there are other parts of codes in play, but the principle is the same - the relevant parts of my code that could have something to do with my problem is what you will see in the code box below):

http://menu.jokeheaven.eu/randomjoke.php

 

<?php
// What happens here is quite obvious. I do this in case someone clicks a link that contains id=X so that the appropriate row's content will be displayed
$number = $_GET['id'];

// Establishing MySQL connection
mysql_connect("db_server", "username", "password") or die(mysql_error());

// Selecting database
mysql_select_db("db_name") or die(mysql_error());


// Here I have a code that displays row X's content even if the user did not click any button (if he or she clicked a link with id=X).
// However, I believe this code is not relevant in terms of my question


// Checking if the user press the "Next" button
if (isset($_POST['next']))

{

	$number++;
	button1($number);

}

// Checking if the user press the "Previous" button
elseif (isset($_POST['previous']))

{

	$number--;
	button1($number);

}

else

{

	// If $number is not set, it's set to 0
	if(!isset($number))

		{

			$number = 0;

		}

}


function button1($number)

{

// Finding the row with the number as id - if it exists
$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);

if ($result)

{

//Storing the number of found rows in $count
$count = mysql_num_rows($result);

if ($count == 1)

	{

		$row = mysql_fetch_array($result);

		// The rest of the code in here is irrelevant

	}

}

// End of button1 function
}

?>


<form action="<?= $php_SELF; echo "randomjoke.php?id=" . $number; ?>" method="post">

<input type="hidden" name="number" value="<?php echo $number; ?>"/>

<input type="submit" name="previous" value="Previous">

<input type="submit" name="next" value="Next">

</form>

 

If I did not explain myself properly, please let me know and I will try again.

 

Thanks in advance for any serious replies (please do not comment on my coding unless it's useful (I'm stilling learning!).).

Link to comment
https://forums.phpfreaks.com/topic/123941-solved-wrong-id-in-url/
Share on other sites

Try echoing  $number in different places so taht you can follow how it's value changes throughout script.

 

I did that. It made me come to the conclusion that the problem is that $number is saved in the URL before the function is run (this happens when a button is pressed) but I do not know how to update the URL after the function is run. I added some more comments in my code:

 

<?php
// What happens here is quite obvious. I do this in case someone clicks a link that contains id=X so that the appropriate row's content will be displayed
$number = $_GET['id'];

// Establishing MySQL connection
mysql_connect("db_server", "username", "password") or die(mysql_error());

// Selecting database
mysql_select_db("db_name") or die(mysql_error());


// Here I have a code that displays row X's content even if the user did not click any button (if he or she clicked a link with id=X).
// However, I believe this code is not relevant in terms of my question


// Checking if the user pressed the "Next" button
if (isset($_POST['next']))

{

                // If the "Next" button was clicked, $number is increased by one (to go to the next MySQL row (take a look at the query in the button1 function))
	$number++;

                // $number is passed to the button1 function so that the variable can be used in the MySQL query inside the function (to find the appropriate row to display on the page)
	button1($number);

}

// Checking if the user pressed the "Previous" button
elseif (isset($_POST['previous']))

{

                // If the "Previous" button was clicked, $number is decreased by one (to go to the previous MySQL row (take a look at the query in the button1 function))
	$number--;

                // $number is passed to the button1 function so that the variable can be used in the MySQL query inside the function (to find the appropriate row to display on the page)
	button1($number);

}

else

{

	// If $number is not set, it's set to 0
	if(!isset($number))

		{

			$number = 0;

		}

}

// This function is run when either button is pressed ($number is either incremented or decremented a bit above before running the function)
function button1($number)

{

// $number is used as a search criteria in the query; it will find the row where the id matches the $number variable (if $number = 3, the row with id = 3 will be found)
$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);

// If the query above was successful, the code below will be run
if ($result)

{

//Storing the number of found rows in $count (this will always be 1 since there are never two rows with the same id in the MySQL table)
$count = mysql_num_rows($result);

        // If one row with the $number variable as id is found, then the code below will be run
if ($count == 1)

	{

		// The rest of the code in here is irrelevant; it's just displaying the content from the MySQL row

	}

}

// End of button1 function
}

?>


<form action="<?= $php_SELF; echo "randomjoke.php?id=" . $number; ?>" method="post">

<? // $number is saved in a hidden field so that it can be used on the next page ?>
<input type="hidden" name="number" value="<?php echo $number; ?>"/>

<? // These are just my two buttons that each call the function button1($number) ?>
<input type="submit" name="previous" value="Previous">
<input type="submit" name="next" value="Next">

</form>

try to copy and paste... see if this works

 

<?php
// What happens here is quite obvious. I do this in case someone clicks a link that contains id=X so that the appropriate row's content will be displayed
$number = $_GET['id'];

// Establishing MySQL connection
mysql_connect("db_server", "username", "password") or die(mysql_error());

// Selecting database
mysql_select_db("db_name") or die(mysql_error());


// Here I have a code that displays row X's content even if the user did not click any button (if he or she clicked a link with id=X).
// However, I believe this code is not relevant in terms of my question


// Checking if the user press the "Next" button
if (isset($_POST['next']))

{


	button1($number);
                          $number++;

}

// Checking if the user press the "Previous" button
elseif (isset($_POST['previous']))

{


	button1($number);
                         $number--;
}

else

{

	// If $number is not set, it's set to 0
	if(!isset($number))

		{

			$number = 1;

		}

}


function button1($number)

{

// Finding the row with the number as id - if it exists
$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);

if ($result)

{

//Storing the number of found rows in $count
$count = mysql_num_rows($result);

if ($count == 1)

	{

		$row = mysql_fetch_array($result);

		// The rest of the code in here is irrelevant

	}

}

// End of button1 function
}

?>


<form action="<?= $php_SELF; echo "randomjoke.php?id=" . $number; ?>" method="post">

<input type="hidden" name="number" value="<?php echo $number; ?>"/>

<input type="submit" name="previous" value="Previous">

<input type="submit" name="next" value="Next">

</form>

If I did not explain myself properly, please let me know and I will try again.

 

 

Unfortunately, it did not work, l0ve2hat3.

 

You don't need to comment $number++; for me :P

 

Ok... but where's that link you want to display?

 

I know, but I just wanted to make sure you knew how I used it in my MySQL query. :)

 

About the link, check the form action.

Yes it is, because form has no way of knowing if user is going to click on 'Next' or on 'Previous'. (I think I'm finally getting what you want to have ;) )

 

You could fix that using JavaScript

or

You could leave it as is, but redirect the user to correct url

// Checking if the user press the "Next" button
if (isset($_POST['next']))

{

                          $number++;
                          header("Location:".$_SERVER['PHP_SELF']."randomjoke.php?id=$number");
}

// Checking if the user press the "Previous" button
elseif (isset($_POST['previous']))

{
                         $number--;
                          header("Location:".$_SERVER['PHP_SELF']."randomjoke.php?id=$number");
}

Oh my God, Mchl - that works almost perfectly. You have no idea how relieved I am to finally have this fixed. I used:

 

header("Location: randomjoke.php?id=$number");

 

... or else it would add randomjoke.php?id=X after the already existing file name. :)

 

There is a small problem, though, but I might be able to fix it. If not, it's really a minor issue that 1/9,999 will notice - and even so it has no "consequences". I have no idea why I didn't think of using header, lol. :)

 

Thank you so much man.  ;D

No problem

 

It was wicked at first, to understand what you want, but then it was simple. ;)

 

Yeah I should have thought of that solution myself. But hey, I learn as I go. :)

 

I just fixed the other problem (it was really, really simple), so now the script has no bugs!

 

Thanks again - I owe you one! If there's something you think I can help you with (probably not coding, lol), then be sure to PM me. :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.