Sachindra
Sachindra

Reputation: 6969

an issue with MySQL query

There is a weird code here that I need to make work.. can you please help me correct it .

mysql_query("insert into table(column) values('$var[0]'));

Upvotes: 0

Views: 46

Answers (3)

Steve Obbayi
Steve Obbayi

Reputation: 6085

change the way you are using the variable like so:

<?php

mysql_query("insert into table(column) values('".$var[0]."')");

?>

and close the double quotes at the end as you had forgotten to do so.

Upvotes: 0

Martin Milan
Martin Milan

Reputation: 6390

Do you really have a table that only needs a single column to be populated?

Can you issue the query through your database admin tool directly, rather than going through PHP? What error do you get?

There are many reasons why your code as it currently stands might be falling over - constraints and permissions being just two. If you can post a helpful error message, we can post some helpful advice...

Martin

Upvotes: 0

rjh
rjh

Reputation: 50274

It looks like you are missing the double quote " at the end of your SQL string.

While you're at it, you should rewrite your query like so:

mysql_query("INSERT INTO table (column) VALUES ('" . mysql_real_escape_string($var[0]) . "')");

...unless you've already escaped $var[0], you should pass all variables through mysql_real_escape_string before interpolating them into an SQL query, to prevent SQL injection attacks.

Upvotes: 2

Related Questions