Oh_What_A_Noob Posted November 5, 2012 Share Posted November 5, 2012 Well it's been my very first initiative to build a dynamic page in php. As i'm a newbie in php, i don't know much about php programming. i've made a database named "dynamic" and it's table name "answer" after that i've inserted four fields namely 'id', 'A1','A2', 'A3'. I inserted the value in id=1 which are A1=1,A2 and A3-0, In id=2, i have inserted A1=0, A2=1, A3=0 In id-3, i have inserted A1 and A2=0 A3=1 So now what i wanted is whenever i will click on the link of id=1 then it will display the content of id=1 and so on... What i've done so far are:- $conn= mysql_connect("localhost","root", ""); $db= mysql_select_db("dynamic", $conn); $id=$_GET['id']; $sql= "select * from answer order by id"; $query= mysql_query($sql); while($row=mysql_fetch_array($query, MYSQL_ASSOC)) { echo "<a href='dynamic.php?lc_URL=".$row['id']."'>Click Here</a>"; if($row['A1']==1) { echo "A1 is 1"; } else if($row['A2']==1) { echo "A2 is 1"; } else if($row['A3==1']) { echo "A3 is 1"; } else { echo "Wrong query"; } } ?> When i've executed this codes then it is showing me the exact id and it is going to the exact id but the values has not been changing.. I want whenever i will click on the id then it will display the exact value like if i click on id=2 then it will echo out "A2 is 1" nothing else.... Can anyone please help me out? I also have noticed about:-$id=$_GET['id']; what is it and how to use it. Can anyone explain me out.. Thanks alot in advance:) Quote Link to comment https://forums.phpfreaks.com/topic/270304-dynamic-url-or-pages-in-php/ Share on other sites More sharing options...
Scott_S Posted November 5, 2012 Share Posted November 5, 2012 You have several issues going on here that is preventing you from seeing your desired output (both in php and sql), not to mention that you should never deploy code like that out into the wild but for educational purposes running on your localhost it is fine. What do you want to see when you load the page dynamic.php Right now you should see three "click here" links followed by some text. Something like this: Click HereA1 is 1A2 is 1A3 is 1 Is this what you want to see? Quote Link to comment https://forums.phpfreaks.com/topic/270304-dynamic-url-or-pages-in-php/#findComment-1390308 Share on other sites More sharing options...
MMDE Posted November 5, 2012 Share Posted November 5, 2012 (edited) function legal_input($input){ if(preg_match('/\A[0-9]+\Z/', $input, $match)) return true; return false; } $conn = mysql_connect("localhost","root", ""); if(!$conn){ echo 'couldn\'t connect to dbms'; exit(); } $db = mysql_select_db("dynamic", $conn); if(!db){ echo 'couldn\'t select db'; exit(); } $id = $_GET['id']; if(legal_input($id)) $result = mysql_query("SELECT * FROM answer WHERE id = $id ORDER BY id", $conn); else{ echo 'illegal input'; exit(); } if(mysql_num_rows($result)==0){ echo 'no rows in result'; exit(); } while($row=mysql_fetch_assoc($result)){ echo '<a href="dynamic.php?lc_URL='.$row['id'].'">Click Here</a><br>'; foreach($row AS $key=>$column){ if($key=='id') continue; if($column==1){ echo $key.' is '.$column.'<br>'; } } } I usually write some typos, I probably have this time too... but maybe you understand some more from it. Edited November 5, 2012 by MMDE Quote Link to comment https://forums.phpfreaks.com/topic/270304-dynamic-url-or-pages-in-php/#findComment-1390333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.