Help - Search - Members - Calendar
Full Version: Php Id Thing
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Banjo
Hi does anyone know how to do stuff like Games.php?id=123

So i would have a page with a list of links on for example Jokes and each Joke would have there own id

E.G

http://www.mysite.com/Jokes.php?id=123

and another would be like


http://www.mysite.com/Jokes.php?id=124

And so on...

And when you click a link it would load the joke with all the information like download link, send to friend link, etc... all in the main content area.

Thanks
Brandon
QUOTE(Banjo @ Jan 5 2008, 08:19 PM) *
Hi does anyone know how to do stuff like Games.php?id=123

So i would have a page with a list of links on for example Jokes and each Joke would have there own id

E.G

http://www.mysite.com/Jokes.php?id=123

and another would be like
http://www.mysite.com/Jokes.php?id=124

And so on...

And when you click a link it would load the joke with all the information like download link, send to friend link, etc... all in the main content area.

Thanks

I have Been wanting to do this lol
Brandon
QUOTE(Tom @ Jan 5 2008, 08:27 PM) *
It's usually for accessing dynamic content, for instance if each joke had its own database entry, the number appended to the query string might be the primary key of the joke being displayed.

If we're talking PHP, the technique you're after is using the $_GET superglobal. If my query string was "?file=24", then "<?php echo $_GET['file']; ?>" would print "24".

I don't get it lol.
Banjo
Thanks Tom you no any tutorial site or where i can download a example please i just used jokes as an example, i would use it for games and videos etc...
Ed
The id tends to relate to the id of a row in a database table, say for example you had a basic table:

CODE
id
title
joke


Your url being:

CODE
view.php?id=1


Something like the following could pull the joke with ID 1:

CODE
<?php
$link = @mysql_connect('localhost', 'myuser', 'mypassword');

if($link === false):?>
<h1 class="error">Could not establish database connection.</h1>
<?php
exit;
endif;

mysql_select_db('mydb', $link);

if(isset($_GET['id']) && ctype_digit($_GET['id'])):
   $result = mysql_query('SELECT title, joke FROM jokes WHERE id = ' . $_GET['id']);

   if(mysql_num_rows($result) == 1):
      $row = mysql_fetch_assoc($result);
?>
<h1 class="title"><?php echo $row['title']; ?></h1>
<p class="joke"><?php echo $row['joke']; ?></p>
<?php
   else:
?>
<h1 class="error">Couldn't find joke associated with the supplied ID.</h1>
<?php
   endif;
else:?>
<h1 class="error">The ID supplied is invalid.</h1>
<?php
endif;
?>


A very crude example, but it should point you in the right direction.
Brandon
QUOTE(Bread @ Jan 5 2008, 09:04 PM) *
The id tends to relate to the id of a row in a database table, say for example you had a basic table:

CODE
id
title
joke


Your url being:

CODE
view.php?id=1


Something like the following could pull the joke with ID 1:

CODE
<?php
$link = @mysql_connect('localhost', 'myuser', 'mypassword');

if($link === false):?>
<h1 class="error">Could not establish database connection.</h1>
<?php
exit;
endif;

mysql_select_db('mydb', $link);

if(isset($_GET['id']) && ctype_digit($_GET['id'])):
   $result = mysql_query('SELECT title, joke FROM jokes WHERE id = ' . $_GET['id']);

   if(mysql_num_rows($result) == 1):
      $row = mysql_fetch_assoc($result);
?>
<h1 class="title"><?php echo $row['title']; ?></h1>
<p class="joke"><?php echo $row['joke']; ?></p>
<?php
   else:
?>
<h1 class="error">Couldn't find joke associated with the supplied ID.</h1>
<?php
   endif;
else:?>
<h1 class="error">The ID supplied is invalid.</h1>
<?php
endif;
?>


A very crude example, but it should point you in the right direction.

Thank You Bread! smile.gif
Banjo
Ok thanks the line

CODE
mysql_select_db('mydb', $link);


would you change the mydb bit to the database name.

and how would you add to the database?

I'll look for some more examples. Thanks guys
Ed
QUOTE(Banjo @ Jan 5 2008, 09:21 PM) *
Ok thanks the line

CODE
mysql_select_db('mydb', $link);


would you change the mydb bit to the database name.

and how would you add to the database?

I'll look for some more examples. Thanks guys


Yes, 'mydb', would be the database name, see manual:
http://www.php.net/mysql_select_db

An insert would using an insert query:

SQL
INSERT INTO jokes (title, joke) VALUES ('My Joke Title', 'Here is my joke.');


You would incorporate a form for submitting or just enter them manually with something like phpMyAdmin. I'm not really in the mood for writing a whole joke submitting app... anyone else feel free to go ahead and do so.
Banjo
Thanks Bread ill have a go and try and do it.

Btw if i searched for this would i search for Php dynamic content?
Brandon
Banjo You might want users to add a joke from your website so here is what you can do.
First create a file called jokeinput_.php
and on that page have this code

CODE
<html>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="jokeinsert.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Add a Joke to Our Database</strong></td>
</tr>
<tr>
<tr>
<td>Title</td>
<td>:</td>
<td><input name="Title" type="text" id="Title"></td>
</tr>
<tr>
<td>Joke</td>
<td>:</td>
<td><input name="Joke" type="text" id="Joke"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</html>

Second create a page called jokeinsert.php this page will add your joke into the database.

CODE
<?php

$host="localhost";
$username="yourusername";
$password="yourpassword";
$db_name="yourdatabase";
$tbl_name="yourtable";

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$Title=$_POST['Title'];
$Joke=$_POST['Joke'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(id, title, joke)VALUES('$Title', '$Joke')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='jokeinsert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>

Now check your Table to see if your data was entered.

Banjo
Ok thanks man very helpfull and would i use the code by Bread to display it.

I dont want users to add them just me but i can make a admin panel and have that on there

I wanted it so i can have instead of haveing loads of pages like vid1.html, vid2.html etc... for loads of videos i can just have a php script which gets the data from database like video description, title, send to friend link, download link, etc...

Not just for videos but jokes, games etc...

Btw Crown what do i fill in for

CODE
$tbl_name="yourtable";


Thanks again ill see if i can get this to do what i want it to.
Brandon
QUOTE(Banjo @ Jan 5 2008, 11:19 PM) *
Ok thanks man very helpfull and would i use the code by Bread to display it.

I dont want users to add them just me but i can make a admin panel and have that on there

I wanted it so i can have instead of haveing loads of pages like vid1.html, vid2.html etc... for loads of videos i can just have a php script which gets the data from database like video description, title, send to friend link, download link, etc...

Not just for videos but jokes, games etc...

Btw Crown what do i fill in for

CODE
$tbl_name="yourtable";


Thanks again ill see if i can get this to do what i want it to.

CODE
$tbl_name="yourtable";


That is the name of the table you have created in the database. So if the table was called videos or jokes you would set
CODE
$tbl_name="videos";
or
CODE
$tbl_name="jokes";

So if the table below was the table you created you would use `jokes` as your table name do you understand?
CODE
CREATE TABLE `jokes` (
  `ID` int(4) NOT NULL auto_increment,
  `title` varchar(65) NOT NULL default '',
  `joke` varchar(65) NOT NULL default '',
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;
Banjo
Thanks Crown thats why it didn't work for me i forgot

CODE
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;


for the databse.

Brandon
QUOTE(Banjo @ Jan 6 2008, 12:52 PM) *
Thanks Crown thats why it didn't work for me i forgot

CODE
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;


for the databse.

Your Welcome wink.gif
Ed
I mentioned to Crown on IRC that his code was vulnerable. I told him to use 'mysql_real_escape_string', yet he was not sure what I meant (or truth), so here's Crown's code modified so it's not vulnerable:

CODE
    
Banjo You might want users to add a joke from your website so here is what you can do.
First create a file called jokeinput_.php
and on that page have this code

CODE

<html>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="jokeinsert.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Add a Joke to Our Database</strong></td>
</tr>
<tr>
<tr>
<td>Title</td>
<td>:</td>
<td><input name="Title" type="text" id="Title"></td>
</tr>
<tr>
<td>Joke</td>
<td>:</td>
<td><input name="Joke" type="text" id="Joke"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</html>

Second create a page called jokeinsert.php this page will add your joke into the database.

CODE
<?php

$host="localhost";
$username="yourusername";
$password="yourpassword";
$db_name="yourdatabase";
$tbl_name="yourtable";

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$Title=mysql_real_escape_string($_POST['Title']);
$Joke=mysql_real_escape_string($_POST['Joke']);

// Insert data into mysql
$sql="INSERT INTO $tbl_name(id, title, joke)VALUES('$Title', '$Joke')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='jokeinsert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>


Might be worth noting that my code does leave you open to XSS vulnerabilities. This code isn't meant to be used in its entirety, it was to serve as an example.

But anywhere, here's the fix:

CODE
<?php
$link = @mysql_connect('localhost', 'myuser', 'mypassword');

if($link === false):?>
<h1 class="error">Could not establish database connection.</h1>
<?php
exit;
endif;

mysql_select_db('mydb', $link);

if(isset($_GET['id']) && ctype_digit($_GET['id'])):
   $result = mysql_query('SELECT title, joke FROM jokes WHERE id = ' . $_GET['id']);

   if(mysql_num_rows($result) == 1):
      $row = mysql_fetch_assoc($result);
?>
<h1 class="title"><?php echo htmlspecialchars($row['title'], ENT_QUOTES); ?></h1>
<p class="joke"><?php echo htmlspecialchars($row['joke'], ENT_QUOTES); ?></p>
<?php
   else:
?>
<h1 class="error">Couldn't find joke associated with the supplied ID.</h1>
<?php
   endif;
else:?>
<h1 class="error">The ID supplied is invalid.</h1>
<?php
endif;
?>
Brandon
QUOTE(Bread @ Jan 6 2008, 04:21 PM) *
I mentioned to Crown on IRC that his code was vulnerable. I told him to use 'mysql_real_escape_string', yet he was not sure what I meant (or truth), so here's Crown's code modified so it's not vulnerable:

CODE
    
Banjo You might want users to add a joke from your website so here is what you can do.
First create a file called jokeinput_.php
and on that page have this code

CODE

<html>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="jokeinsert.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Add a Joke to Our Database</strong></td>
</tr>
<tr>
<tr>
<td>Title</td>
<td>:</td>
<td><input name="Title" type="text" id="Title"></td>
</tr>
<tr>
<td>Joke</td>
<td>:</td>
<td><input name="Joke" type="text" id="Joke"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</html>

Second create a page called jokeinsert.php this page will add your joke into the database.

CODE
<?php

$host="localhost";
$username="yourusername";
$password="yourpassword";
$db_name="yourdatabase";
$tbl_name="yourtable";

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$Title=mysql_real_escape_string($_POST['Title']);
$Joke=mysql_real_escape_string($_POST['Joke']);

// Insert data into mysql
$sql="INSERT INTO $tbl_name(id, title, joke)VALUES('$Title', '$Joke')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='jokeinsert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>


Might be worth noting that my code does leave you open to XSS vulnerabilities. This code isn't meant to be used in its entirety, it was to serve as an example.

But anywhere, here's the fix:

CODE
<?php
$link = @mysql_connect('localhost', 'myuser', 'mypassword');

if($link === false):?>
<h1 class="error">Could not establish database connection.</h1>
<?php
exit;
endif;

mysql_select_db('mydb', $link);

if(isset($_GET['id']) && ctype_digit($_GET['id'])):
   $result = mysql_query('SELECT title, joke FROM jokes WHERE id = ' . $_GET['id']);

   if(mysql_num_rows($result) == 1):
      $row = mysql_fetch_assoc($result);
?>
<h1 class="title"><?php echo htmlspecialchars($row['title'], ENT_QUOTES); ?></h1>
<p class="joke"><?php echo htmlspecialchars($row['joke'], ENT_QUOTES); ?></p>
<?php
   else:
?>
<h1 class="error">Couldn't find joke associated with the supplied ID.</h1>
<?php
   endif;
else:?>
<h1 class="error">The ID supplied is invalid.</h1>
<?php
endif;
?>

Thanks A lot Bread, I'm try to do a like php script so I can delete a joke from the table from the script ? Would you be able to help us with this, I have tried to do this but i keep getting this message Parse error: syntax error, unexpected $end in C:\wamp\www\Test\del.php on line 53
Banjo
Ok Thanks Bread. I did edit his old one before and just added a ID field and that works aswell.
Banjo
Hi, heres a working example i did useing bread and crowns old code.

http://schoolrebel.zzl.org/Jokes/

and a example of a added joke

http://schoolrebel.zzl.org/Jokes/view.php?id=1

i tried to put a link to the joke just added after you add one by doing this in the insert file (You will no where i tried to put it if you download the files)

CODE
echo "Your id is $Id , go to view page <a href='view.php?id=$Id'>here</a>";

but that didn't work.

Download here
Click to view attachment

Anyway ill try bread's new code now.
Banjo
Get a error with Breads code.

Anyway does anyone know how i would display all the jokes url on a page so the page would be like

Joke1
http://www.mysite.com/view.php?id=1

Joke2
http://www.mysite.com/view.php?id=2

etc... and where it say joke1 and joke2 that would be the joke title.

Also im stuck on how to add the $id to a url if you read above post explain how i tried there.

Thanks laugh.gif
Brandon
QUOTE(Banjo @ Jan 6 2008, 09:37 PM) *
Get a error with Breads code.

Anyway does anyone know how i would display all the jokes url on a page so the page would be like

Joke1
http://www.mysite.com/view.php?id=1

Joke2
http://www.mysite.com/view.php?id=2

etc... and where it say joke1 and joke2 that would be the joke title.

Also im stuck on how to add the $id to a url if you read above post explain how i tried there.

Thanks laugh.gif

Ima try have alook 2moz when i have more time ok
mark_ce
Please allow me to insert this DB code for newbies.

Ex.: database.php
CODE
<?php
$host=place_hostname_here;
$username=place_username_here;
$password=place_password_here;
$database=place_database_name_here;
mysql_connect("$host","$username","$password") or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
?>

Then you may just put this into Ex.: index.php
CODE
<?php
include('database.php');
?>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.