Jump to content

select / store value to $_SESSION['id'] /redirect fails


tunage

Recommended Posts

I am trying to do a select / save selection to $_SESSION['id'] / redirect but the only value I am able to pass to $_SESSION['id'] is the target url and not the selection.  output ->  http://i.imgur.com/QeGb89X.png
 
 
************************************ index.php ******************************************




<?php require_once("header.php");?>





<!-- <form name="addjob" method="POST" action="" onsubmit="return getURL(add-job.php)> -->
<form name="addjob" method="POST" action="" onsubmit="return getURL(add-job.php)">
<select name="bolredir">

<?php
// selection box submit
$query = "SELECT * FROM customer";
$result = $odb->query($query);
$custkey;
// var_dump($result);
if($result->rowCount() > 0) {
foreach($result as $item) {
echo '<option value='.$item['id'].'>';
echo ($item['cust_name'] .",". $item['cust_addr'] .",". $item['cust_phone'] .",". $item['id']."<br />\n");
echo '</option>';
$_SESSION['id'] = $item['id'];
}

}
// var_dump($_SESSION['cust_name']);
?>
</select>
<input type="submit" id="id" name="id" value="add-job.php">
</form>
</body>



***************************************** header.php ***********************************


darknet064tokyo contactdb # cat header.php
<?php
session_start();
if(isset($_POST['id'])){

//store as session variable
$_SESSION['id'] = $_POST['id'];
//forward browser
header("Location: ".$_SESSION['id']);
exit;

}


$host = "localhost";
$user = "custdb";
$pass = "";
$db = "accounting";

$odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);

?>
Link to comment
Share on other sites

the id field has the value I need in $_SESSION['id'] which is gained via the ->  $query = "SELECT * FROM customer";  (it is an auto increment distinct value). I need this value carried over to add-job.php from index.php via the $_SESSION['id'] variable. This is a dynamic value returned from the database query.


mysql> SELECT * FROM customer
    -> ;
+----+-----------+------------+------------+------------+
| id | cust_name | cust_addr  | cust_phone | cust_email |
+----+-----------+------------+------------+------------+
|  1 | bob       | 666 bo rd  | 2124442222 | bo@bo.com  |
|  2 | dude      | 666 jo rd  | 2124445555 | bub        |
|  3 | nnn       | kju        |       6666 | bo@bo2.com |
|  4 | brad      | 888        | 2124444444 | bo@jack.am |
|  5 | brad2     | smirk      | 2147483647 | go@jac.com |
|  6 | another   | 8888 bh rd | 2147483647 | knarly@me  |
|  7 | peach     | 69 too     | 2147483647 | iuhdihih   |
|  8 | peachy    | 4442222 rd |          0 | do@do.com  |
+----+-----------+------------+------------+------------+
8 rows in set (0.00 sec)

mysql> desc customer;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| cust_name  | varchar(100) | NO   |     | NULL    |                |
| cust_addr  | varchar(180) | NO   |     | NULL    |                |
| cust_phone | int(11)      | YES  |     | NULL    |                |
| cust_email | varchar(90)  | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.07 sec)

 

 

 

 

 

Here is a visual

I need this  ZZl5IKd.png

to go here xyRmKGb.png

and be reflected here via $_SESSION['id']  Ui0m2QT.png

Link to comment
Share on other sites

I like you 'question asking' skills with all the images and arrows... A bit overkill, but still fun.

I would really like to help you, but there are so many things wrong with your code that I wouldn't know where to start. I will focus only on your issue:

 

when you do your query loop :

foreach($result as $item) {
   echo '<option value='.$item['id'].'>';
   echo ($item['cust_name'] .",". $item['cust_addr'] .",". $item['cust_phone'] .",". $item['id']."<br />\n");
   echo '</option>';
   $_SESSION['id'] = $item['id'];
}

you have placed $_SESSION['id'] = $item['id']; inside the loop, that means $_SESSION['id'] keeps getting replaced with the next value.

 

I really have to ask this though: If your <option> element has the customer id as it's value, that's the point of trying to store the id in the session too? When the form is submitted, you can grab the id and then place it in the session variable.
 

 

Also, on your next page where you check for $_POST['id'], keep in mind that the name of your <select> element is bolredir and not id.

 

Hope this helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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