skip to Main Content

I’m new to programming and I’m currently studying PHP and the database, I got the error ‘Invalid parameter number: parameter was not defined in (on line 28)’ please tell me what the problem is ?

p.s And I also use Google Translator, so don’t be surprised at my poor English.

$post = "Work";
$full_name = "Jack";
$email = "@gmail";
$work_number = "3241";
$number = "8(903)432-19-11";

$parameters = [
["p"=>$post],
["f_n"=>$full_name],
["e"=>$email],
["w_n"=>$work_number],
["n"=>$number]
];

$sql = "INSERT contact_work ($post, $full_name, $email , $work_number , $number) VALUE(:p , :f_n , :e , :w_n , :n)";
$query=$connection->prepare($sql);

$query->execute($parameters);

I tried to look at various guides and answers of those who faced this problem, but I could not find a solution.

2

Answers


  1. There are multiple issue in your code:

    1. The SQL INSERT statement syntax is incorrect. The correct syntax for INSERT statement should be

      INSERT INTO table_name (column1, column2, …) VALUES (value1, value2, …).

    2. $parameters array key names should be same as placeholders (:p, :f_n, :e, :w_n, :n).

      $parameters = [
      ‘:p’ => $post,
      ‘:f_n’ => $full_name,
      ‘:e’ => $email,
      ‘:w_n’ => $work_number,
      ‘:n’ => $number
      ];

    try with below code:

    $post = "Work";
    $full_name = "Jack";
    $email = "@gmail";
    $work_number = "3241";
    $number = "8(903)432-19-11";
    
    $sql = "INSERT INTO contact_work (post, full_name, email, work_number, number) VALUES (:p, :f_n, :e, :w_n, :n)";
    $query = $connection->prepare($sql);
    
    $parameters = [
        ':p' => $post,
        ':f_n' => $full_name,
        ':e' => $email,
        ':w_n' => $work_number,
        ':n' => $number
    ];
    
    $query->execute($parameters);
    
    Login or Signup to reply.
  2. $post = "Work";
    $full_name = "Jack";
    $email = "@gmail";
    $work_number = "3241";
    $number = "8(903)432-19-11";
    //associative array with key values
    $parameters = [
        ':p' => $post,
        ':f_n' => $full_name,
        ':e' => $email,
        ':w_n' => $work_number,
        ':n' => $number
    ];
    //make sure u replace with correct column names
    $sql = "INSERT INTO contact_work (post, full_name, email, work_number, number) VALUES (:p, :f_n, :e, :w_n, :n)";
    $query = $connection->prepare($sql);
    $query->execute($parameters);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search