Convergeter string array and save to database with Codeignater

Asked

Viewed 42 times

-3

I have a checkbox set to send as a string to the database for the same comma-separated field, however I am getting a return to convert to string array as below:

In the controller I have the following method to handle the checkbox:

if( isset( $_POST['documento'] ) && is_array( $_POST['documento'] ) ) {
    $documentos = implode( ', ', $_POST['documento'] );
} else {
    $documentos = '';
}

In the view, the checkbox:

<div class="customcheck">
      <input type="checkbox" value="RG" name="documento[]">
      <input type="checkbox" value="CPF" name="documento[]">
      
</div>

1 answer

0


I tested it like this and it worked:

<form action="resposta.php" method="POST">
    <input type="checkbox" value="RG" name="documento[]">
    <input type="checkbox" value="CPF" name="documento[]">
    <input type="checkbox" value="certidão" name="documento[]">
    <input type="checkbox" value="titulo" name="documento[]">
    <input type="checkbox" value="CPF" name="documento[]">
    <input type="submit" value="Enviar">
</form>

php response file.:

if (isset($_POST['documento']) && is_array($_POST['documento'])) {
    $documentos = implode(', ', $_POST['documento']);
} else {
    $documentos = '';
}

$username = '****';
$password = '****';

try {
    $pdo = new PDO('mysql:host=localhost;dbname=stackoverflow', $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO documentos (documentos) VALUES (?)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$documentos]);

    echo "Enviado com sucesso!";

} catch (PDOException $e) {

    echo 'ERROR: ' . $e->getMessage();
}

database:

CREATE TABLE `documentos` (
  `id` int(11) NOT NULL,
  `documentos` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `documentos`
  ADD PRIMARY KEY (`id`);
  • I didn’t change anything in your code

  • I am using the Codeignater framework for the example and when you put this structure Try { $Pdo = new PDO('mysql:host=localhost;dbname=stackoverflow', $username, $password); $Pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO documents (documents) VALUES (?)"; $stmt = $Pdo->prepare($sql); $stmt->execute([$documents]); echo "Successfully shipped!" ; } catch (Pdoexception $e) { echo 'ERROR: '. $e->getMessage(); } I cannot see inside the model apply in my example.

  • I posted below the controller with the function to save, as I told you follows the structure of Codeignater (MODEL, CONTROLLER and VIEW) I have several controls and they are being saved in the bank normally I am just not able to leave as string the options marked in the checkbox . Returns a message saying: Message: Cannot unset string offsets after you have changed the code

  • in your answer I believe you subscribe to the $data variable with a "string" and then try to access an array that does not exist $data["se_img"]

  • *you overwrite

  • I got it, I’m assigning a value to $data = $string_json; and soon after I assign $data["se_img"], this is the case where you talk about overwriting. Okay, so I should use another variable name that’s getting the document array. In your saved example considering you had only the form with the input checkbox, in my case I have several inputs

  • I noted that in the return you have the following line VALUES (', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', 'array', ', ', ', ', ', ', ', ', ', ', '', ''', ''''', ''''', ''''), notice that you send an array and not to selected lists of values

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.