Nathan Winch
Nathan Winch

Reputation: 107

SQL insert is not working

Please can someone help? The code below seems to work and gives no errors, but when I check the database, it hasn't added anything. Tearing my hair out!

<?php
$bcode = $_GET['barcode'];
$businessid = $_GET['businessid'];
$servername = "---------";
$username = "-------"; 
$password = "-------";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO 'db741921215'.'Scans' (Barcode, Success, Business) VALUES ('".$barcode."', 'Y', '".$businessid."')"; 
$con->close();
} ?>

The columns in table 'Scans' is as below:

Table Description

Help is very, very much appreciated!!!

Upvotes: 0

Views: 33

Answers (2)

blurfus
blurfus

Reputation: 14041

You seem to be missing the step where you execute the sql statement

I can see where you define it but I don't see where it's executed. i.e.

$conn->query($sql); 

Also, you seem to be missing a letter when closing the connection: $con->close() should be $conn->close();

Upvotes: 0

dmgig
dmgig

Reputation: 4568

I see a couple things - use backticks "`" around your table name definitions, not single quotes. Also, save yourself some eye strain and use the fact that PHP interpolates variables just fine within doublequoted strings.

$sql = "INSERT INTO `db741921215`.`Scans` 
  (Barcode, Success, Business) 
  VALUES 
  ('$barcode', 'Y', '$businessid')";

Also - you never actual execute the query, do you?

$conn->query($sql);

Upvotes: 1

Related Questions