1

I'm having a problem to insert data from php to database.

File is: Patient.php

Table name is: patient_tbl

inside of table is: avatar and education

here is inserting code: There is no problem with this following code, value of 'education' will be inserted to table

$sql = "INSERT INTO " . $this->_table;
$sql .= "education,";
$params = array(
  urlencode($patient->getEducation()),
);
return $this->exec($sql, $params);
}

But now here is my problem, "Avatar" don't insert to table

The error message is: "Unable to prepare query."

$sql = "INSERT INTO " . $this->_table;
$sql .= "education, avatar, ";
$params = array(
  urlencode($patient->getEducation()),
  urlencode($patient->getAvatar()),
);
return $this->exec($sql, $params);
}

What's my prblem? I can't find a solution for this,.

I really appreciate your help.

Thank you

5
  • 1
    Well, for starters, the trailing , in your second query will cause a syntax error. Commented Oct 22, 2015 at 22:56
  • Print out the $sql and include it in the question. My guess is that the answer will be obvious (such as as the lack of parentheses around the column names). Commented Oct 22, 2015 at 22:56
  • Well, This is error message: Unable to prepare query. Commented Oct 22, 2015 at 23:04
  • why are you using urlencode? Commented Oct 22, 2015 at 23:55
  • Instead of adding "[SOLVED]" to the title, you should accept the (an) answer. Commented Oct 23, 2015 at 4:46

1 Answer 1

1

Base syntax sql query 'INSERT' is

INSERT [INTO] tbl_name [(col_name,...)]
        VALUES (expression,...),(...)

If you use some parameters, you need add brackets as that:

$sql = "INSERT INTO " . $this->_table;
$sql .= "(education, avatar) VALUES (?, ?)";
$params = array(
  urlencode($patient->getEducation()),
  urlencode($patient->getAvatar()),
);
return $this->exec($sql, $params);
}

And write, what in $this->exec?

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

1 Comment

Yes you are right, it was a small question mark missed there, just this ((?)) you are right. Thank you so much :)

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.