Jump to content

newbie error: Notice: Array to string conversion in ...


Recommended Posts

			  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

 

 

 

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