Jump to content

Dynamic Url Or Pages In Php


Oh_What_A_Noob

Recommended Posts

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:)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by MMDE
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.