Jump to content

[SOLVED] Edit an article by ID


greens85

Recommended Posts

Hi all,

 

I have the following code which drags out all jobs from a database:

 

<?php
// Make a MySQL Connection
$query = "SELECT * FROM edworld_jobs ORDER BY jobID DESC";  
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo '<strong>Job Ref:</strong>'." ".$row['jobref'];?>
<br/>
<?php echo '<strong>Job Title:</strong>'." ".$row['jobtitle'];?>
<br/>
<br/>
<a href="edit.php" class="bodylinks">Edit Vacancy</a>
<?php echo '<hr style width="100%" size="1" noshade="noshade" color="#999999">';
}
?>

 

How can I make a link which then drags up the job in another page by ID. So that each link has the jobID associated with it?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/166653-solved-edit-an-article-by-id/
Share on other sites

Change this line

<a href="edit.php" class="bodylinks">Edit Vacancy</a>

to

<a href="edit.php?id=<?php echo $row['jobID']?>" class="bodylinks">Edit Vacancy</a>

 

Now in edit.php to retrieve the Job id. you'd use $_GET['id']. Example code for edit.php

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
    $job_id = (int) $_GET['id'];
    $sql = 'SELECT * FROM edworld_jobs WEHERE jobID = '.$job_id;
    $result = mysql_query($sql);

    if(mysql_num_rows($result) == 1)
    {
           // display form to edit job
    }
}

Hi Wildteen,

 

Ive changed it to your code and although it looks like it should work, when I hover over the links they have no idea associated with them:

 

i.e. edit.php?id=

 

Any ideas what ive done wrong here?

 

And when i click through it returns a blank page  :confused:

My code wont output anything. It is just the basics. You need to add the code in yourself to edit the Job

 

The code to edit the job needs be where the comment (orange text) is. Like so

    if(mysql_num_rows($result) == 1)
    {
           // display form to edit job
?>

Edit JobID #<?php echo $job_id; ?> here

Add your form here

<?php
    }

My code wont output anything. It is just the basics. You need to add the code in yourself to edit the Job

 

The code to edit the job needs be where the comment (orange text) is. Like so

    if(mysql_num_rows($result) == 1)
    {
           // display form to edit job
?>

Edit JobID #<?php echo $job_id; ?> here

Add your form here

<?php
    }

 

I have added the following line:

 

span class="main">*</span> Contact Name:
<input type="text" name="contactname" id="contactname" value="<?php echo $row['contactname'];?>" class="bodyinput" />

 

however this doesnt drag in the data from the database, would you be able to tell me why?

 

thanks

Got it sorted!

 

Needed a while statment in there:

 

while($row = mysql_fetch_array($result)){

You do not need a while loop as the query will only return one result. You only need to have

$row = mysql_fetch_array($result);

 

The only time you use a while loop is if the query returns more than one result.

Archived

This topic is now archived and is closed to further replies.

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