1

I cannot for the love of god figure out why this statement is not executing. When I limit it to

mysql_query("INSERT INTO my_pets() VALUES ()", $con);

it fires just fine, creating an empty row (NULL in every cell), but as soon as I give it columns and values, it refuses. See below.

Can someone point out a mistake or any other reason this (seemingly) correct code isn't firing when columns and values are specified?

Premises:

  • $h, $un, $pw, and $db are all fine, as I have copied it from documents that work as we speak.
  • There are no typos or mistakes in upper/lower case characters of column names and such.

The code:

<?php

session_start();

$h="...";      // Host name
$un="...";     // Mysql username
$pw="...";     // Mysql password
$db="...";     // Database name

$con = mysql_connect("$h", "$un", "$pw")or die("cannot connect"); 
mysql_select_db("$db")or die("cannot select DB");

$name = "Pip";
$gender = "F";
$species = "Dog";

mysql_query("INSERT INTO my_pets (name, gender, species) VALUES ('$name', '$gender', '$species')", $con);

mysql_close($con);

?>
VHK
  • 89
  • 5
  • Table create statement? – Uueerdo May 23 '16 at 22:06
  • What is the table structure? What does *it refuses* mean? (An actual error message would be better). – Shira May 23 '16 at 22:08
  • Table simply consists of rows that expect only varchar values with a limit of 255, and of course an id-column (primary key). – VHK May 23 '16 at 22:10
  • 3
    Try backticking `name`, as it's a mysql reserved word. – Don't Panic May 23 '16 at 22:11
  • I'm sorry, Shiranai7, I get no error message anywhere. Is there a way to force it, or am I not looking in the right places? – VHK May 23 '16 at 22:11
  • That did the trick, Dont Panic. You're a hero. – VHK May 23 '16 at 22:13
  • 1
    Can I mark it as resolved somewhere? (I'm new here) – VHK May 23 '16 at 22:13
  • I'm glad it worked for you! I haven't added an answer because I think the question may be a duplicate (sorry!) I haven't found the duplicate to link to yet, though. – Don't Panic May 23 '16 at 22:16
  • The linked question isn't specifically about `name`, but it's the same principle, and the answer should be applicable. – Don't Panic May 23 '16 at 22:21
  • You also should check out prepared statements, because if you take user input for the query you will be vulnerable for sql injections. http://www.w3schools.com/php/php_mysql_prepared_statements.asp – M_T May 23 '16 at 22:23
  • Also, I misspoke. It's a keyword, but not a reserved word. – Don't Panic May 23 '16 at 22:23
  • @Don'tPanic ^ correct https://dev.mysql.com/doc/refman/5.5/en/keywords.html – Funk Forty Niner May 23 '16 at 22:30
  • @VHK In order to be absolutely sure, you need to do `or die(mysql_error())` against `mysql_query()`. Then come back and give us the real error. – Funk Forty Niner May 23 '16 at 22:32
  • What settings or what MySQL version is forcing to use backtics for `name` in a column list? It's working on [sqlfiddle](http://sqlfiddle.com/#!9/74c52/1) and has always worked for me. – Paul Spiegel May 23 '16 at 22:51

1 Answers1

1

Obtained from PHP Documentation, If you're using PHP 7.0.0, mysql_query will no longer work:

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_query() PDO::query()

Good / Important note from someone who cares: This function should not be used for any future code and should be replaced for existing code, so I would recommend you change to PDO.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • I don't disagree with any of this, but it doesn't really answer the question. – Don't Panic May 23 '16 at 22:26
  • @Don'tPanic If he is using php 7.0.0 it would answer it though. This might possibly not be the reason, but it's possible it is :) – Webeng May 23 '16 at 22:27
  • At the beginning of the question, OP says that `mysql_query` works without columns and values. That wouldn't be the case if the function didn't exist. Although I guess that could be the case for a future reader. – Don't Panic May 23 '16 at 22:29