sonnieboy Posted February 24, 2017 Share Posted February 24, 2017 Greetings again, I have tried everything and everywhere but I just can't find the solution to this latest issue. This is the last piece of my project. I will appreciate your highly valued expert help on this. We have an order.php page. User inputs data into a row. User might click to add another row and input data into that row as well. When done, user clicks the submit button which takes him or her to preview.php. The code below is on preview.php: <?php error_reporting(E_ALL); echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>"; if(isset($_POST['employeename'])) $employeename = $_POST['employeename']; if(isset($_POST['email'])) $email = $_POST['email']; if(isset($_POST['ttitle'])) $ttitle = $_POST['ttitle']; $rowIDs = $_POST['rowIDs']; $row2IDs = $_POST['row2IDs']; echo $employeename .'<br>'; echo $ttitle .'<br> <hr width=400 align=left>'; $rowIDs = $_POST['rowIDs']; foreach ($rowIDs as $id) { $sourcename = $_POST['sourcename' . $id]; $sourceaddress = $_POST['sourceaddress' . $id]; $income = $_POST['income' . $id]; echo 'Name: '. $sourcename . '<br />'; echo 'Address: '. $sourceaddress . '<br />'; echo 'Income: '. $income . '<br /><br>'; } foreach ($row2IDs as $id) { $spousename = $_POST['spousename' . $id]; $spouseAddress = $_POST['spouseAddress' . $id]; $spouseIncome = $_POST['spouseIncome' . $id]; echo 'Name: '. $spousename . '<br />'; echo 'Address: '. $spouseAddress . '<br />'; echo 'spouseIncome: '. $spouseIncome . '<br /><br>'; echo 'Your email: '. $email . '<br /><br>'; } ?> <body> <form action='final.php' method = 'POST'> <input type="hidden" name="employeename" value="<?php echo $employeename; ?>"> <input type="hidden" name="ttitle" value="<?php echo $ttitle; ?>"> <input type="hidden" name="sourcename[]" value="<?php echo $_POST['sourcename' . $id]; ?>"> <input type="hidden" name="sourceaddress[]" value="<?php echo $_POST['sourceaddress' . $id]; ?>"> <input type="hidden" name="income[]" value="<?php echo $_POST['income' . $id]; ?>"> <input type="hidden" name="spousename[]" value="<?php echo $_POST['spousename' . $id]; ?>"> <input type="hidden" name="spouseAddress[]" value="<?php echo $_POST['spouseAddress' . $id]; ?>"> <input type="hidden" name="spouseIncome[]" value="<?php echo $_POST['spouseIncome' . $id]; ?>"> <a href="javascript:history.go(-1)">Return to correct changes</a> <input type="submit" value="submit" /> </form> </body> If everything looks goo to the user on this page, the user clicks Submit and final.php processes the submit request and inserts the records into the database if everything is ok. When only one row of data is submitted, the record is processed and submitted successfully into the database. However, when more than one row is processed, it fails with the following error: Notice: Array to String Conversion error in C:\httpdocs\xampp\myfolder\final.php I believe the reason I am getting that error in final.php page is because most of those values are being processed as strings when they should be processed as arrays. Can one of you experts point me in the right direction of modify the code below: This place is my last hope to get this working. Thank you for your assistance. $sql = 'INSERT INTO `mydb`.`wp_mytable` ( `employeeID`' . ', `sourcename`, `sourceaddress`, `income`,`spousename`,`spouseAddress`,`spouseincome` )' . ' VALUES ( ? , ? , ? , ? , ? , ? , ? )'; if( $sth = mysqli_prepare($conn,$sql) ) { mysqli_stmt_bind_param($sth,'sssssss' ,$last_id ,$_POST["sourcename"] ,$_POST["sourceaddress"] ,$_POST["income"] ,$_POST["spousename"] ,$_POST["spouseAddress"] ,$_POST["spouseIncome"] ); Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 24, 2017 Share Posted February 24, 2017 Yes well when you define an html form element as an array, it can have multiple values and will be an array when converted to an internal PHP variable. For example, you have: It's not really clear why you have some fields defined as arrays and others that are not. I can't comment on that, but it's fishy. In order to figure out what you have there are things like is_array() in php. if (is_array($_POST["sourcename"])) { } It seems to me if this is a multiple line item type application, then it ought to be all or none for the fields in the POST. Once you've detected that those are arrays rather than simple strings, you will need to loop on the section of code that binds and then executes the queries. With mysqli_ you don't have to prepare the query again, but only bind the values and then execute the query. Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 24, 2017 Author Share Posted February 24, 2017 Thanks very much for your response. To respond to this: The reason some of them are not arrays is because they require one response only. For instance, an employee (employeename) can provide his/her name once. His or her title once and his or her email once. however, when dealing with income source (sourcename, sourceaddress, income), those could come from multiple sources. That's why a user is given an option to click to add more sources of income. If you could be kind enough to give me just one example of how to check if an array and what to do if it is, I would really appreciate. Thank you again Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 24, 2017 Share Posted February 24, 2017 Spousename, SpouseAddress, etc, are arrays because this is an application for Mormons? I joke, but in order to understand how many times you need to loop, as well as reference the array elements, I would need to better understand what the data would look like in the case there are multiple entries. I don't see the correlation between the possible arrays that leads to individual rows of data where some of the data is the same, and other data would vary. Probably in this circumstance you would want to write a function that builds a $rowData[] array where you have everything you need to do your inserts and then you would for loop through that array doing a prepare/execute for each array element. Just strictly answering your question, is basically me showing how you reference data in a numerically indexed array. One way is to use a for .. loop, another is a foreach. I really can't figure out from your code or your description which is appropriate. With that said, simplistically: if (is_array($_POST['sourcename'])) { $count = count($_POST['sourcename']); for ($x = 0; $x echo "{$_POST['sourcename']} "; } } As you will see from that (or from var_dump() of the various POST arrays, you will have a simple array of strings, that can be addressed numerically starting at zero. echo $_POST['sourcename'][0]; // first element echo $_POST['sourcename'][1]; // 2nd element etc. As I stated, the problem is that some of the fields could have 3 elements, while others have 5, or 1. Your database structure seems to be flat, but it's also unclear how the data is related on a row by row basis. Quote Link to comment Share on other sites More sharing options...
sonnieboy Posted February 24, 2017 Author Share Posted February 24, 2017 (edited) Thank you (again) very much. I really appreciate your help. I could dump all the code I have here but they won't help much. So, let me try and hopefully, I can make some sense. On the first page called order.php, we have several rows separated by divs. The idea of this app is to get employees to declare their sources of income other than their regular pay. So, the database is designed, I think to have normalized data. We have employee table that stores employee info. Then the main table. So, an emp can have one or more rows of data associated with his or her name. So if I for instance, have 3 income sources as a result of my wife's dealings with different entities, I would enter spousename, spouseaddress and spouseincome. Since there is more information related to my spouse income source, I would click a button to give me more rows to provide more information about my wife's income source. So employee name, title, email can only be provided once. honoria can only be provided once. And the rest can be provided once or more than once. Then the next page called preview.php where you have a couple of foreach loops and hidden form fields, is used by user to preview entries. On this page, you can see the array works great as it captures however many rows or entries an employee enters regarding his/her income sources. The biggest challenge now is to capture all the values coming from the hidden form fields and insert them into the database. I am not able to do that on the INSERT. For instance, using the example you just showed, I have this: $_POST['sourcename'] which is on the final.php page with the INSERT statement, if I need to capture the array of $_POST['sourcename'] and use that on the insert statement, how do I do it? That's where I am struggling mightily. Thanks so much for your help. Edited February 24, 2017 by sonnieboy Quote Link to comment 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.