0

Sorry but I am fairly new to PHP, and translating some code over from ASP. While I have done Updates from .NET and c# and ASP. And Understand the theory behind it I am struggling to find the PHP code that works. It will be a really simple concept to some of you.

So I have a list that is pulled from a MS SQL database. This is a list of all 'pages' for example. next to each one is and edit button that looks like this:

<a href="UpdatePage.php?id="><img src="images/edit.gif" width="16" height="16" alt="" border="0"></a>

The important line being:

href="UpdatePage.php?id="

I need to set a variable after the ?id=

This is mean to redirect you to the edit page where I will populate a form with data from the DB.

The code I have written for that looks like this:

<?php
$query = "SELECT PageTitle, PageText ";
$query .= "FROM Pages ";
$query .= "WHERE PageID =  ";

$result = mssql_query($query);

$numRows = mssql_num_rows($result); 
?>

Again missing the bit after PageID = If I set this to all it pulls through all entries, if I hardcode it with an ID it also works. What im looking for is a way for the list page to pass the update page the PageID and a way to tell the query that is needs to only retrieve that entire relating to that PageID. If that makes sense. I have looked at alot of code and can't find a decent tutorial or explanation of this relating to PHP and MSSQL. Any tips or points in the right direction would be appreciated.

After looking through I feel it should be something like:

<?php echo $PageID; ?>

$PageID = $_GET['PageID'];

AM I on the right tracks?

Bohdi
  • 1,295
  • 4
  • 28
  • 62
  • Yes, but have you tried? And do not forget to read [Best way to prevent SQL Injection in PHP](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php). Also, I flag as not a real question. – j0k Sep 04 '12 at 10:37

1 Answers1

0

In my oppinion the best way of doing this is adding a callback function to your result array. From the query you will get an array with x elements ( feel free to use a paginator if you want ).

Then you will loop that array and for each row you will have the page_id you need and in that moment you can generate a correct link. And now you will generate the

<a href="UpdatePage.php?id=$loop['page_id']"><img src="images/edit.gif" width="16" height="16" alt="" border="0"></a>

and concatenate it with the existing row ( or better generate the whole row itself not to loop the array 2 times ). So try to hook a call inside your loop.

Doc : http://php.net/manual/en/language.types.callable.php

Cosmin
  • 1,482
  • 12
  • 26