0

So bassically I can't seem to send the array with the input values to my database.

I tried sending it seperately, it works, but it only sends the array or the way around. There are no errors.

if (isset($_POST['submit'])) {
    $services = implode ("|", $_POST['services']);
    mysqli_query($mysqli, "INSERT INTO klientai (package, name, surname, email, phone, message, services) VALUES('$_POST[package]', '$_POST[name]', '$_POST[surname]', '$_POST[email]', '$_POST[phone]', '$_POST[message]', '$services'");
}
2
  • echo $services to check you are getting a string with no special characters (eg quotes) in there. Commented Oct 17, 2019 at 22:48
  • everything is OK with the array, no special characters. I just can't seem to add it with other inputs together. Seperately it works. Commented Oct 17, 2019 at 22:51

1 Answer 1

1

mysql_query function is deprecated and is not secured, You should use another option.

You can use PDO for example: https://www.php.net/manual/en/book.pdo.php

open connection

$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

insert method 1

$sql = "INSERT INTO users (name, surname, sex) VALUES (?,?,?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$name, $surname, $sex]);

insert method 2

$data = [
    'name' => $name,
    'surname' => $surname,
    'sex' => $sex,
];

 $sql = "INSERT INTO users (name, surname, sex) VALUES (:name, :surname, :sex)";
 $stmt= $pdo->prepare($sql);
 $stmt->execute($data);

also check https://phpdelusions.net/pdo_examples/insert and https://www.startutorial.com/articles/view/pdo-for-beginner-part-1

In this method, you don't need to escape your strings for SQL injection and it should also solve your problem.

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

1 Comment

OP used mysqli_query in his code. Your code is not safe against SQL injection. You pointed OP to the best available tutorial, but you haven't used what is written there in your answer.

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.