greens85 Posted June 17, 2008 Share Posted June 17, 2008 Hi all, I want to achieve a php redirect feature using mySQL. What i'm looking to do is to have a database which contains the following fields: ID AccountCode Category Then i want a html form that contains an input text field with a submit button. The idea is that I will have some PHP that will check what account code the user has entered and based on this they will be directed to different pages. E.G. A user enters the account code: NE1234 they are taking to page A, however if they enter the account code: AB4321 they will be taken to page B. I'm sure this is a fairly simple problem but Im new to PHP so i really have no idea where to begin on this one other that the fact i should need an IF Statement. Thanks In Advance. Can someone offer me advice on this? Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/ Share on other sites More sharing options...
MatthewJ Posted June 17, 2008 Share Posted June 17, 2008 Add a field to the table for the page url, then just check which one they submitted and use the url for the destination of a redirect. Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-567239 Share on other sites More sharing options...
greens85 Posted June 17, 2008 Author Share Posted June 17, 2008 I think what im lacking is the knowledge of how to make the URL a link. This is my attempt so far (from my very limited knowledge). The form where the account code is entered: <form method="post" action="checkcode.php"> <title>Account Code:</title> <input name="AccountCode" type="text" id="AccountCode" size="25" /> <input type="submit" name="submit" id="submit" value="submit" /> </form> The PHP: <?php $host = 'myhostname'; $dbuser = 'myuser'; $dbpass = 'mypassword'; $dbname = 'mydbname'; $db = @mysql_connect($host,$dbuser,$dbpass) or die("error=could not connect to $host"); $db = mysql_select_db($dbname); $UserAccountCode = $_POST['AccountCode']; if ( $UserAccountCode == 'NE0101') { echo ('Welcome user with account code NE0101, your special prices await...'); $query = mysql_query("SELECT pageURL FROM accountcodes WHERE AccountCode = '$UserAccountCode'"); echo $query; } if ( $UserAccountCode == 'DL0101') { echo ("Welcome user with account code DL0101, your special prices await..."); $query = mysql_query("SELECT pageURL FROM accountcodes WHERE AccountCode = '$UserAccountCode'"); echo $query; } if ( $UserAccountCode == 'AV0101') { echo ("Welcome user with account code AV0101, your special prices await..."); $query = mysql_query("SELECT pageURL FROM accountcodes WHERE AccountCode = '$UserAccountCode'"); echo $query; } ?> What i need to do is wrap the echo $query into a href. My database is set up in the following manner: ID ------- Account Code ------- Category ------- pageURL 1 ------- NE0101 ------- A ------- www.someurl.com/path/page.php Am i on the right lines here? Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-567334 Share on other sites More sharing options...
greens85 Posted June 17, 2008 Author Share Posted June 17, 2008 Having tried to get this to work i realise it isnt going to be suitable for what i need to do. I need a situation where the Category field dictates which URL will be displayed. If the category is originally an A - the url might be http://www.mydomain.com/pagea.php But if a situation arose where the category letter was changed i would need the url to change aswell, lets say http://www.mydomain.com/pageb.php So the account code will never change but the category letter might and if it doesnt i need to display a different URL. As i say im really new to PHP and have no idea how i can go about this. Any help is appreciated even if someone can point me in the direction of a tutorial, ive searched but i dont really know what search terms would find me answers to this. ??? Thanks for your help Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-567379 Share on other sites More sharing options...
MatthewJ Posted June 17, 2008 Share Posted June 17, 2008 <?php $host = 'myhostname'; $dbuser = 'myuser'; $dbpass = 'mypassword'; $dbname = 'mydbname'; $db = @mysql_connect($host,$dbuser,$dbpass) or die("error=could not connect to $host"); $db = mysql_select_db($dbname); $UserAccountCode = $_POST['AccountCode']; $query = mysql_query("SELECT pageURL FROM accountcodes WHERE AccountCode = '$UserAccountCode'"); $row = mysql_fetch_assoc($query); $url = $row['pageURL']; echo "Welcome user with account code $UserAccountCode, your special prices await...<br /><a href=\"$url\">Click to view</a>"; ?> That should do the same as your code, and will output the link "Click to view" that will point to the url from the database. Let me know Also, you will want to sanitize the data before using it in a query just to be safe. As far as the category changing, are you saying it would change in the database? If so, you would just need to make sure that you change the url stored for that record. Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-567459 Share on other sites More sharing options...
greens85 Posted June 18, 2008 Author Share Posted June 18, 2008 Also, you will want to sanitize the data before using it in a query just to be safe. Sorry I dont know what you mean by santize the data. As far as the category changing, are you saying it would change in the database? Yes the category would be changed in the database manually, i.e. someone would open up the database and change the category to suit the needs at that particular time. If so, you would just need to make sure that you change the url stored for that record. The problem with this is that there will be literally thousands of records present in the database, so there the possibility that many records will be changing at any given time, so changing the URL for each could become very time consuming. Although my php isnt great i thought something along the line of the following code might get same job done but without the need to have a URL field in the database. <?php $host = 'myhostname'; $dbuser = 'myuser'; $dbpass = 'mypassword'; $dbname = 'mydbname'; $db = @mysql_connect($host. $dbuser, $dbpass) or die ("error = could not connect to $host"); $db = mysql_select_db($dbname); $UserAccountCode = $_POST['AccountCode']; $query = mysql_query("SELECT Category FROM accountcodes WHERE AccountCode = '$UserAccountCode'"); $row = mysql_fetch_assoc ($query); $category = $row ['Category']; $url1 = "http://www.mydomain.com/acccodes/page1.php"; $url2 = "http://www.mydomain.com/acccodes/page2.php"; $url3 = "http://www.mydomain.com/acccodes/page3.php"; if ($UserAccountCode == 'AB1234' && $category == 'A'"); echo "Welcome user with account code $UserAccountCode, your special prizes await...<br /><a href=\"$url1\">HERE</a>"; else if ($UserAccountCode == 'BC1234' && $category == 'B'"); echo "Welcome user with account code $UserAccountCode, your special prizes await...<br /><a href=\"$url2\">HERE</a>"; else if ($UserAccountCode == 'CD1234' && $category == 'C'"); echo "Welcome user with account code $UserAccountCode, your special prizes await...<br /><a href=\"$url3\">HERE</a>"; echo "Welcome user with account code $UserAccountCode, your special prices await...<br /><a href=\"$url\">Click to view</a>"; Could you let me know if you think it would do or job, or if any of the syntax is wrong, as i say PHP isnt my strong point. Cheers Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-567991 Share on other sites More sharing options...
zenag Posted June 18, 2008 Share Posted June 18, 2008 if ($UserAccountCode == 'AB1234' && $category == 'A'") { echo "Welcome user with account code $UserAccountCode, your special prizes await...<br /><a href=".$url1.">HERE</a>"; } else if ($UserAccountCode == 'BC1234' && $category == 'B'"){ echo "Welcome user with account code $UserAccountCode, your special prizes await...<br /><a href=".$url2.">HERE</a>"; } else if ($UserAccountCode == 'CD1234' && $category == 'C'"){ echo "Welcome user with account code $UserAccountCode, your special prizes await...<br /><a href=".$url3.">HERE</a>"; } Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-568003 Share on other sites More sharing options...
greens85 Posted June 18, 2008 Author Share Posted June 18, 2008 Is this the correct syntax for multiple if statment? if ($category == 'A'); echo "Welcome user with account code $UserAccountCode, your special prices await...<br /><a href=\"$url1\">HERE</a>"; else if ($category == 'B'); echo "Welcome user with account code $UserAccountCode, your special prices await...<br /><a href=\"$url2\">HERE</a>"; else if ($category == 'C'); echo "Welcome user with account code $UserAccountCode, your special prices await...<br /><a href=\"$url3\">HERE</a>"; Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-568029 Share on other sites More sharing options...
greens85 Posted June 18, 2008 Author Share Posted June 18, 2008 Hi all, Problems is solved barring some simple validation incase the user enters an incorrect account code. I should be able to code this without too many difficulties, thanks to everyone for there help. Thanks once again. Link to comment https://forums.phpfreaks.com/topic/110558-php-redirect/#findComment-568061 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.