0

I am having the following error:

"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 'Not, Fotograf, Tarih) values ('','','','','','','','TC','','','','Ev' at line 1"

(I am using wamp6.4 that keeps asking "mysqli")

What can be wrong?

My PHP code:

$ekle = mysql_query("INSERT INTO kayitliste (AdayNo, KimlikNo, Ad, Soyad, MezunLise, DiplomaDerece, TelefonNo, Uyruk, VeliAdSoyad, VeliTelefon, Adres, Ulasim, Bolge, Yurt, Bolum, TercihSirasi, Burs, Dekont, Kimlik, Diploma, Odenen, Sinif, Not, Fotograf, Tarih) 
VALUES ('$AdayNo','$KimlikNo','$Ad','$Soyad','$MezunLise','$DiplomaDerece','$TelefonNo','$Uyruk','$VeliAdSoyad','$VeliTelefon','$Adres','$Ulasim','$Bolge','$Yurt','$Bolum','$TercihSirasi','$Burs','$Dekont','$Kimlik','$Diploma','$Odenen','$Sinif','$Not','$Fotograf','$Tarih') ");
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
bantandor
  • 267
  • 1
  • 15
  • 2
    I guess you should try echo $query before you execute and check if your query is correct – Priyesh Kumar Jun 11 '16 at 15:31
  • 1
    OMG, you have more that 20 columns in a table, is it so hard to normalize/decompose it into smaller tables? – Priyesh Kumar Jun 11 '16 at 15:31
  • 1
    You can use mysqli_query() to execute your query and mysqli_error() to get any error generated. – Priyesh Kumar Jun 11 '16 at 15:33
  • 1
    Not is a reserved word, so a poor choice for a table/column identifier – Strawberry Jun 11 '16 at 16:02
  • 2
    Possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – andrewsi Jun 12 '16 at 00:27
  • The problem was I used Not as a variable which was explained in the link that andrewsi gave. Not is a special syntax in mysql that is why mysql did not accept it as a column name and gave error. I changed the not to notlar and it fixed the problem. Thank you all. – bantandor Jun 12 '16 at 06:29

2 Answers2

2

mysql is deprecated. You should be using either mysqli or PDO with a parameterized query as shown below:

Mysqli:

$link = mysqli_connect("localhost", "root", "");
mysqli_select_db("Your database");

if ($stmt = mysqli_prepare($link, "INSERT INTO `kayitliste` VALUES (?, ?, ?, ?, ?)")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, $type, $AdayNo, $KimlikNo, $Ad, $Soyad, $MezunLise);

    /* Execute query */
    mysqli_stmt_execute($stmt);

    /* Bind result variables */
    mysqli_stmt_bind_result($stmt, $AdayNo, $KimlikNo, $Ad, $Soyad, $MezunLise);

    /* Close statement */
    mysqli_stmt_close($stmt);
}
  1. $type can be "s" for string, "i" for integer, "d" for double and "b" for blob.

  2. The question marks ? have to be as many as the values you want to insert to your database.

  3. In your case of many values, you should know what type each value is and write $type = "iisss", with as many letters as your values. Since your variables are in a language I do not know I assummed that these ending in No are integers and the other three strings.

PDO:

$sql = 'INSERT INTO `kayitliste` (`AdayNo`, `KimlikNo`, `Ad`, `Soyad`, `MezunListe`)
VALUES (:AdayNo, :KimlikNo, :Ad, :Soyad, :MezunListe)';

$sth = $dbh->prepare($sql);
$sth->bindParam(':AdayNo', $AdayNo, PDO::PARAM_INT);
/* Do that for every parameter */
/* PDO::PARAM_INT is the equivalent of "i" of mysqli in PDO. */
$sth->execute()
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
1

In cases such as this, it's helpful to see the generated query for yourself. Do this:

echo "insert into kayitliste (AdayNo, KimlikNo, Ad, Soyad, MezunLise, DiplomaDerece, TelefonNo, Uyruk, VeliAdSoyad, VeliTelefon, Adres, Ulasim, Bolge, Yurt, Bolum, TercihSirasi, Burs, Dekont, Kimlik, Diploma, Odenen, Sinif, Not, Fotograf, Tarih) 

values

('$AdayNo','$KimlikNo','$Ad','$Soyad','$MezunLise','$DiplomaDerece','$TelefonNo','$Uyruk','$VeliAdSoyad','$VeliTelefon','$Adres','$Ulasim','$Bolge','$Yurt','$Bolum','$TercihSirasi','$Burs','$Dekont','$Kimlik','$Diploma','$Odenen','$Sinif','$Not','$Fotograf','$Tarih') ";

And then check the query that is generated. I bet you'll find your error if you look carefully.

Vilx-
  • 104,512
  • 87
  • 279
  • 422