0

I am currently working in a Evaluation system. I have a problem about the comment feature. The input field is dynamically created on how many personnel you will rate. I have an error when it comes in inserting an array of comment into may database and its type is text and in my database is varchar. when Im inserting my comment to database it is empty.

I already check if its under my form and I already check the name that I post and its all correct.

my input field

<input type="text" class="form-control" name="nComment[]">

php code who process

foreach($_POST['nPersonnelId'] as $i => $personnelId ){
        $rComment = $_POST['nComment'][$i];
        mysqli_query($conn,"INSERT INTO comment
                (personnelId,comment) 
                VALUES ('$personnelId','$rComment')");
    }
5
  • Post the error message that you get. Commented May 26, 2019 at 16:18
  • there is no error message sorry Commented May 26, 2019 at 16:22
  • Your code is vulnerable to SQL injection. You should use prepared statements. Commented May 26, 2019 at 16:32
  • chat.stackoverflow.com/rooms/193554/pies-den @Jasper Commented May 26, 2019 at 16:34
  • sorry @dharman I already change it to pdo thanks a lot @dharman! Commented May 26, 2019 at 16:45

1 Answer 1

1

You can serialize comment and save in database with:

$rComment = serialize($_POST['nComment'][$i]);

https://www.php.net/manual/en/function.serialize.php

and then when you read from database use unserialize to back to array.

https://www.php.net/manual/en/function.unserialize.php

Sign up to request clarification or add additional context in comments.

3 Comments

try change structure from varchar to text in database
yeah it works! but how did it end up in text not in varchar by the way? but thanks sincerely!
varchar have limit of chars that you able to insert for example varchar(120) means that you able to put only 120 chars, text is: See for maximum numbers: dev.mysql.com/doc/refman/5.0/en/storage-requirements.html TINYBLOB, TINYTEXT L + 1 bytes, where L < 2^8 (255 Bytes) BLOB, TEXT L + 2 bytes, where L < 2^16 (64 Kilobytes) MEDIUMBLOB, MEDIUMTEXT L + 3 bytes, where L < 2^24 (16 Megabytes) LONGBLOB, LONGTEXT L + 4 bytes, where L < 2^32 (4 Gigabytes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.