4

I need to set two variable in a url, $id and $job. The data should come from mysql select statement, [id] and [job_number_id]. My page displays a client job proposal and if there is more than one all proposals are displayed but the [id] and the [job_number_id] determines what is displayed on the webpage. I dont have a clue as to how this done. Any help would be greatly appreciated. Here's the code:

<?php

$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];

$query =    "SELECT id, client_job_name, job_number_id
            FROM `job_name`
            WHERE `id`='$id'";
$allJobs = $db->query($query);

?>

<?php foreach ($allJobs as $site_title) : ?>
 <p> 
  <tr><?php echo '<a href="'.$url.'">'.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'</a>'; ?>
    <td></td>
  </tr>
</p>
<?php endforeach; ?>
3
  • Is this the full code ? You are missing session_start() , database connection.. Commented Jan 29, 2014 at 7:27
  • What error it is showing ? Commented Jan 29, 2014 at 7:28
  • @Shankar Damodaran the session_start() is located in another file. There are a few files more files that I didn't include. At this point I'm not receiving any errors because I haven't added a variable to the http address. I need to get the id and job number id and add it to the url. Commented Jan 29, 2014 at 7:30

2 Answers 2

3

If you want the variables to be avaialble in the URL you need to read them with $_GET.

Getting the arguements from a url such as index.php?id=1&job_number_id=3 will look like that:

if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
    $id = $_GET['id'];
    $job_number_id = $_GET['job_number_id'];
}

To set it in your foreach statement:

<?php foreach ($allJobs as $site_title) : ?>
 <p> 
      <tr><?php 

$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];

echo '<a href="'.$url.'">'.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'</a>'; 

    ?>
    <td></td>
  </tr>
</p>
<?php endforeach; ?>

PLEASE remember to read about SQL injection and making sure you are escaping your inputs. Or even better - use a prepared statement.

Currently your script is volunerable, since everyone could just alter the URL and manipluate your DB.

Hope this helps!

Sign up to request clarification or add additional context in comments.

10 Comments

After adding this code, I'm now getting a Warning: Invalid argument supplied for foreach(). Before I had no errors, what happened?
It could be various reasons. Do var_dump($_GET) and post it so I can look. Also, are you using mysql, mysqli or pdo? do var_dump on $alljobs as well.
var_dump on $alljobs array(0) { } object(PDOStatement)#5 (1) { ["queryString"]=> string(76) "SELECT id, client_job_name, job_number_id FROM job_name WHERE id='8'" }
var_dump on ($_GET) Notice: Undefined index: job_number_id in C:\xampp\htdocs\Estimate_lar\home.php on line 33
the $_GET error is because you are not setting job_number_id in the url
|
0

Try this.

<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){

$query =    "SELECT id, client_job_name, job_number_id  FROM `job_name`  WHERE `id`='$id'";
$allJobs = $db->query($query);

}else{
echo "Id not in session";
}
?>
 <table>
<?php if ($allJobs)  { foreach ($allJobs as $site_title) : ?>

  <tr>
  <td>
 <a href="<?php echo $url; ?>?client_job_name=<?php echo $site_title['client_job_name'];?>&job_number_id=<?php echo $site_title['job_number_id'];?> "> <?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?></a>
  </td>

 </tr>

<?php endforeach; ?>
</table>

 <?php }else{ echo 'No results Found' ;} ?>

This may help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.