0
<?php
mysql_connect("mysql6.000webhost.com","a6124751_murali1","***");
$db= mysql_select_db("a6124751_signup");
$topic=$_GET["Topic"];
$question=$_GET["Question"];
$company =$_GET["Company"];
$query = "INSERT INTO questions (topic, question, company) VALUES ($topic, $question, $company)";
$sql1=mysql_query($query);
if (!$sql1) {
 die('Invalid query: ' . mysql_error());
}
?>

this is my php code in server where there is a table named 'questions' and i am trying to insert the data into it from the input got from the GET method using form at front end, i can figure out that data is coming properly from the client which i have checked using echo. I am getting an error as

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 'name, type your question here, company)' at line 1

Don't know what is the error in the query. anyone find it out asap. thank you

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
murali kurapati
  • 1,510
  • 18
  • 23

3 Answers3

5

You need to quote your values

('$topic', '$question', '$company')

since those are strings.

Plus, you should escape your data for a few reasons. Not let MySQL complain about certain characters such as hyphens etc., and to protect against SQL injection.

Use prepared statements:

Reference(s):


Edit:

As an example using your present MySQL API:

$topic    = mysql_real_escape_string($_GET['topic']);
$question = mysql_real_escape_string($_GET['question']);
$company  = mysql_real_escape_string($_GET['company']);

I don't know what your inputs are called, so that's just an example.

You mentioned about using $_GET for debugging but using a POST method.

  • Change all $_GET to $_POST above.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

Try this

<?php
$db = mysqli_connect('mysql6.000webhost.com', 'a6124751_murali1', 'default@123', 'a6124751_signup');

if (!$db) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

$topic = $_GET["Topic"];
$question = $_GET["Question"];
$company = $_GET["Company"];

$query = "INSERT INTO questions (topic, question, company) VALUES ('$topic', '$question', '$company')";
$sql1=mysqli_query($db, $query);
if(!$sql1)
{
    die('Invalid query: ' . mysqli_error($db));
}
?>

Fixes in your code

  • The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

  • You need to quote your values ('$topic', '$question', '$company')

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
0

You have to put the values in single qoutes, if that are char types:

$query = "INSERT INTO questions (topic, question, company) VALUES ('$topic', '$question', '$company')";

But you should not longer use the deprecated mysql_*API. Use mysqli_* or PDO with prepared statements.

Jens
  • 67,715
  • 15
  • 98
  • 113