sonnieboy Posted November 6, 2017 Share Posted November 6, 2017 if(isset($_POST)==true && empty($_POST)==false){ $pastorsname = $_POST['pastorsname']; $ministriesname = $_POST['ministriesname']; $xstartdate = $_POST['xstartdate']; $clientname = $_POST['clientname']; $url = $_POST['url']; } // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO xphias(pastorsname, ministriesname, xstartdate,clientname, url) VALUES ('$pastorsname','$ministriesname','$xstartdate','$clientname','$url')"; if ($conn->multi_query($sql) === TRUE) { echo "New records created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); <tbody> <tr> <td><INPUT TYPE="TEXT" NAME="pastorsname[]" SIZE="14"></td> <td><INPUT TYPE="TEXT" NAME="ministriesname[]" SIZE="14"></td> <td><INPUT TYPE="TEXT" NAME="xstartdate[]" SIZE="10"></td> <td><INPUT TYPE="TEXT" NAME="clientname[]" SIZE="14"></td> <td><INPUT TYPE="TEXT" NAME="url[]" SIZE="14"></td> <td><INPUT TYPE="submit" name="action" SIZE="14" value="Save"></td> <td><input type="button" value="Add New" onClick="addRow('dataTable')" /></td> </tr> </tbody> With the code above, user can dynamically add more rows to the table. This means these rows will be processed and inserted into the database as array data: And I am using this as my insert statement: When I run the code, I get an error: Notice Array to String converstion The value inserted into the database is called Array instead of the actual value. I know I am doing something wrong but can you please help? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/305564-newbie-error-notice-array-to-string-conversion-in/ Share on other sites More sharing options...
benanamen Posted November 6, 2017 Share Posted November 6, 2017 Here is an example of inserting a form array into a DB. This example uses a for loop. It's an old piece of code I had so it could probably be improved a bit. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", ""); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare("INSERT INTO mytable (field1, field2) VALUES (?,?)"); for ($i = 0; $i < count($_POST['field1']); $i++) { $stmt->execute(array( $_POST['field1'][$i], $_POST['field2'][$i] )); } } ?> <form method="post"> <b>field1 1</b><br> <label>field1 <input type="text" name="field1[]"></label> <br> <label>field2 <input type="text" name="field2[]"></label> <br> <b>field1 2</b><br> <label>field1 <input type="text" name="field1[]"></label> <br> <label>field2 <input type="text" name="field2[]"></label> <input name="" type="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/305564-newbie-error-notice-array-to-string-conversion-in/#findComment-1553443 Share on other sites More sharing options...
sonnieboy Posted November 6, 2017 Author Share Posted November 6, 2017 This has worked. Thank you very much, I just have to add error checking and successful submission message, Quote Link to comment https://forums.phpfreaks.com/topic/305564-newbie-error-notice-array-to-string-conversion-in/#findComment-1553463 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.