Jump to content

Post variables from a while loop from a second table in form


Recommended Posts

I have 2 tables.  I'm trying post 3 variables in order to insert them in table2. The loop works I see the variables but when I use POST outside the loop I'm not getting the variables.  


I'm using a SELECT to get 3 variables from table1 which I'm getting those variables.  I want to insert these variables in table2 but can't see the input value when looking at the source.


The loop works. I get results in the loop but the input post echo's nothing. I've tried many different ways but can't seem to get it to work. Can someone help?



<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
while($row = mysqli_fetch_array($result))
{
echo ("<input type='text' value='$row[name]' name='name' id='name'>");
echo ("<input type='text' value='$row[cover]' name='cover' id='cover'>");
echo ("<input type='text' value='$row[pageno]' name='pageno' id='pageno'>");
}
?>

<input type="hidden" name="name" id="name" value="<?php echo "{$_POST['name']}"; ?>">
<input type="hidden" name="cover" id="cover" value="<?php echo "{$_POST['cover']}"; ?>">
<input type="hidden" name="pageno" id="pageno" value="<?php echo "{$_POST['pageno']}"; ?>">

?  I don't have a solution.  What I'm trying to achieve with the code above is to post the variables you see in my first post and add them to the db.  The variables you see are coming from a different table.  All I need to do is grab those variables and post them.  The hidden inputs are not populating with those variables.  The loop is working <  I see the variables but the inputs not populating. 

Yes three columns of one row of data per selection.  The form is involved because that's where they start.  They fill out the form select a book name and everything with that book name needs to be added  cover, page-no to table2.  Yes they are hidden fields so the user can only see the name.  I have text in the input in the loop for testing.  I did this to see if it was working but name will be the only thing the user will see... the other variables will be hidden.  

A database should hold data in a single place and the only things that are replicated in other tables are key values. So the only item you transfer to table2 should be the book_id. You get the book name etc by referencing the book table via the id value.

You still only need the id, then when the form is posted

INSERT INTO table2 (book_id, name, cover, pageno) 
SELECT book_id, name, cover, pageno 
FROM table1
WHERE book_id = $posted_id

I found away that works for the code that I have.  There's one issue.  

 

1.  If there's more than one book title it will echo all book titles for name, cover all cover names, and all pageno for all books.

2.  When the db is populated it does the same as the echo, it populates the columns with all - names, covers, and pageno's for all books instead of the specific book selected.  

 

I'm not sure how to loop through the inputs and print only the book selected and populate the db with what was selected only. I hope that makes  sense.  

<select name="name">
<option value="<?php echo "{$_POST['name']}"; ?>"> </option>                      
       
<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT * FROM books");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");

$cover = '';
$pageno = '';

while($row = mysqli_fetch_array($result))
{
echo ("<option value='$row[name]'>$row[name] $row[cover], $row[pageno] </option>"); 

$cover .= "$row[cover]";
$pageno .= "$row[pageno]";

}

?>
                      
</select>

<input type="hidden" name="cover" value="<?php echo $cover; ?>">
<input type="hidden" name="pageno" value="<?php echo $pageno; ?>">
Edited by jgreen
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.