tags:

views:

37

answers:

2

For some reason, I just can't seem to get my head around this concept. Any advice/hints will be appreciated. As of now, I have this code:

if(isset($_GET['ID'])) {//Check if ID exists in $_GET
  $ID = mysql_real_escape_string($_GET['ID']); //prevent sql injection
  $resc = mysql_query("SELECT Message, Title, Type FROM Discussion WHERE ID='$ID' LIMIT 1");//Query the db for record

if(mysql_num_rows($resc) == 1) { //make sure the question exists
        $ques = mysql_fetch_assoc($resc); //convert mysql resource into array that can be used throughout script
                               }
                       }

Ok, so the above code simply allows me to access a particular row in this case, and from it, acquire the necessary information I may need. But what I want to know is, what if I want to give that question a URL? For example, when I post my(this) question on Stackoverflow, it will most likely be saved in a database, and will adopt an ID or something similar, so members of the community and I can reference to it at a later time. This will allow everyone to click on 'this' question because it will have a specific ID/reference. Similarly, I do have a specific ID in the MySQL table for the unique rows/questions, but how can I access a URL with that specific ID? I.e. be directed to that actual question when I click on it.
For example, the code that I have illustrated above comes from savedisc.php, which is simply saving a user's discussion acquired by a form in simple text into a MySQL table. So at a later time, when I do decide to access this discussion, how can I 'click' on it. I of course don't want to be directed to savedisc.php to see the user's discussion, but maybe something like savedisc.php?id=115521, considering that the actual question's information is indeed associated with the primary key ID=115521. Will I have to make another field or something?

Maybe I am doing something wrong in the code below? This comes from savedisc.php:

$message = $_POST['message'];
$title = $_POST['title'];
$represents = $_POST['represents'];
$ID = $_GET['ID'];

$sql="INSERT INTO Discussion (ID, Message, Title, Type)
VALUES
('','$message','$title','$represents')";

Thank you.

A: 

You basically have your answer already - by saying ?ID=115521, you pass on a value to your PHP script. This is what you get by refering to $_GET['ID'], ie the way of linking the URL to the database field. Or am I getting you wrong?

Nicolas78
@ nicolas78 - I edited some of my code, I am probably setting up something wrong. How will I actually pass on a value to my PHP code?
Newbie_25
you call your URL via myscript.php?ID=1then$_GET['ID']should return 1be sure to keep the right upper/lower case!
Nicolas78
actually the case is probably the problem: you write savedisc.php?id=115521, but then you call $ID = $_GET['ID']; - try $ID = $_GET['id'] instead!
Nicolas78
A: 

Don't quite understand the problem, but if you want to get the newly created ID after posting and redirect the user to that, use mysql_insert_id:

http://php.net/manual/en/function.mysql-insert-id.php

Andrew67
If IDs are unique per post, you should set the ID field to AUTO_INCREMENT and omit it from the INSERT statement so it automatically gets the next ID without needing your input. Otherwise, this method could be used by a malicious user to trigger an INSERT of an already existing post (although it'll most likely error out since it's the primary key)
Andrew67