AlAnoodZ
AlAnoodZ

Reputation: 29

MySQL insert error in PHP

I've been trying to get the code below to execute, although no error appears, the row is not inserted when I check the result in the Database. There seems to be a problem in passing $query into $sql, because when I echo the same value of $query and add it as text into $sql it works fine.

any idea what is the error ?

$i=3;

$query = '"'."INSERT INTO data (ID,QN,QI,CB,BK,QK)".""."VALUES("."'".$i."','".$price[17]."','".$price[47]."','".$price[77]."','".$price[107]."','".$price[137]."')".'"';

echo $query;

$connect= mysql_connect("localhost", "urs" , "password") or die(mysql_error());

mysql_select_db("data", $connect);

$sql= $query; 

mysql_query($sql,$connect)

Upvotes: 0

Views: 78

Answers (1)

Giacomo1968
Giacomo1968

Reputation: 26066

The problem is your query has no space right before VALUES which would make the query syntactically incorrect as far as MySQL goes. It would render like this when the script runs:

INSERT INTO data (ID,QN,QI,CB,BK,QK)VALUES(…);

MySQL would choke on that query. Instead, try this version of your query with one space added before the VALUES:

$query = '"'."INSERT INTO data (ID,QN,QI,CB,BK,QK)".""." VALUES("."'".$i."','".$price[17]."','".$price[47]."','".$price[77]."','".$price[107]."','".$price[137]."')".'"';

But that said, your concatenation makes 100% no sense & is unnecessarily complex. I’ve cleaned that up & this should work:

$query = "INSERT INTO data (ID,QN,QI,CB,BK,QK) VALUES('$i','$price[17]','$price[47]','$price[77]','$price[107]','$price[137]');";

The key you need to understand is double quotes in PHP allow for string substation. So no need to concatenate to the degree you did.

Also you are missing a semi-colon after the mysql_query() so it should be like this:

mysql_query($sql,$connect);

So here is my cleaned up version of your code:

// Set the value for `$1`.
$i = 3;

// The query.
$query = "INSERT INTO data (ID,QN,QI,CB,BK,QK) VALUES('$i','$price[17]','$price[47]','$price[77]','$price[107]','$price[137]');";

// Set the connection or die returning an error.
$connect = mysql_connect("localhost", "urs" , "password") or die(mysql_error());

// Select the database;
mysql_select_db("data", $connect);

// Run the query or die returning an error.
mysql_query($query, $connect) or die(mysql_error());

But now really looking at it you are selecting a database named data and then inserting into a table within that database called data as well? Is that correct? Seems like you might have mixed up the idea of a database & a table so double check that.

Also note that I added an or die(mysql_error()); after mysql_query($query, $connect). Your first or die(mysql_error()); only covers errors in the connection & not errors that would appear when the query runs. Now if the query has an error, it will die with an error being returned.

That said, you should explore using mysqli_* commands instead of mysql_* since mysql_* commands are depreciated in PHP 5.3 to 5.4 and are completely eliminated in PHP 5.5.

Upvotes: 3

Related Questions