Reputation: 25
Hi I am trying to insert data into a db and I am getting an error when it gets to the IP address, it seems the decimal places in the ip are causing the error.
INSERT INTO farms (name, ip, amount) VALUES (somename, 123.32.32.1, 432432) SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.32.1, 432432)' at line 2
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'myDB';
$table = 'myTable';
$name = $_GET['name'];
$ip = $_GET['ip'];
$amount = $_GET['amount'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO $table (name, ip, amount)
VALUES ($name, $ip, $amount)";
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
Upvotes: 1
Views: 854
Reputation: 3660
$sql = "INSERT INTO $table (name, ip, amount)
VALUES ('$name', '$ip', $amount)";
You missed quotes for char/varchar.That may be the problem
Upvotes: 1