Faris MX Faris MX - 3 years ago 173
MySQL Question

PHP only adding Numbers to sql in column of VARCHAR

PHP only adding Numbers to MySQL in column of VARCHAR instead of texts
when using query directly in MySQL it works...but if I use $_POST from HTML, IT fails


if(mysqli_connect_error()) {
die("error in database");
$name =$_POST["name"];
$qeury = "INSERT INTO `test`(`number`, `name`) VALUES (NULL,$name)";
echo "done";
else {
echo "failed";

<!doctype html>
<meta charset="utf-8">
<title>Untitled Document</title>

<form method="post">
<input type="text" placeholder="enter a name" name="name">
<input type="submit" value="add">

Answer Source

You need quotes around text

$qeury = "INSERT INTO `test`(`number`, `name`) VALUES (NULL,'$name')";

Please, think about prepared query. It solve quotes problem and protect from sql injection.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download