jgreen Posted August 11, 2015 Share Posted August 11, 2015 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']}"; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/ Share on other sites More sharing options...
Barand Posted August 11, 2015 Share Posted August 11, 2015 Forget about your "solution" for now - what is it you want to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518535 Share on other sites More sharing options...
jgreen Posted August 11, 2015 Author Share Posted August 11, 2015 ? 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. Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518542 Share on other sites More sharing options...
Barand Posted August 12, 2015 Share Posted August 12, 2015 So you want to transfer three columns of data from table1 to table 2? Why do you have form involved? They are hidden fields so the user cannot see or edit them? Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518561 Share on other sites More sharing options...
jgreen Posted August 12, 2015 Author Share Posted August 12, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518587 Share on other sites More sharing options...
Barand Posted August 12, 2015 Share Posted August 12, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518597 Share on other sites More sharing options...
jgreen Posted August 12, 2015 Author Share Posted August 12, 2015 Yep so I guess your not going to look at my code and answer my question. Can someone look at the code and answer my original question? It would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518608 Share on other sites More sharing options...
Barand Posted August 12, 2015 Share Posted August 12, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518615 Share on other sites More sharing options...
akphidelt2007 Posted August 12, 2015 Share Posted August 12, 2015 Where is the form script that you are using to submit these POST variables? Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518639 Share on other sites More sharing options...
jgreen Posted August 13, 2015 Author Share Posted August 13, 2015 (edited) 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 August 13, 2015 by jgreen Quote Link to comment https://forums.phpfreaks.com/topic/297741-post-variables-from-a-while-loop-from-a-second-table-in-form/#findComment-1518671 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.