-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>
I didn’t change anything in your code
– Douglas Takiuti
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.
– Renato Maia
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
– Renato Maia
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"]
– Douglas Takiuti
*you overwrite
– Douglas Takiuti
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
– Renato Maia
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
– Renato Maia