Jump to content

Recommended Posts

I'm trying to write string variables into a table.  Something like this:

 

$string = "random string";
$anotherstring = "random string";

mysql_query("INSERT INTO table (field1, field2) VALUES("$string", "$anotherstring") ") 
or die(mysql_error()); 

 

What am I missing?  When I run the code, I get an error about MySQL syntax being incorrect.

Link to comment
https://forums.phpfreaks.com/topic/172911-solved-how-to-send-variable-to-mysql/
Share on other sites

You can't put double quotes inside double quotes without escaping them... or use single quotes like below

 

<?php
$string = "random string";
$anotherstring = "random string";

mysql_query("INSERT INTO table (field1, field2) VALUES('$string', '$anotherstring') ")
or die(mysql_error());
}
?>

Hello Mate,

 

mysql_query("INSERT INTO `table` (field1, field2) VALUES('$string', '$anotherstring')") or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

 

That will work. May i ask where are you connecting to mysql ?,

 

And what are the errors you get ?,

 

James.

Here's what the relevant section of code looks like:

 

mysql_connect($hostname,$username,$password) or die(mysql_error());

mysql_select_db($database) or die(mysql_error());

$file = date("m.d.y") . ".txt";

if (file_exists($file)) {

$fh = fopen($file, 'r');
$string = fread($fh, filesize($file));
fclose($fh);

mysql_query("INSERT INTO table (field1) VALUES('$string') ") 
or die(mysql_error()); 

}

 

James, I tried your code and still got the following error:

 

Fatal error: Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 

 

That's not the complete error message, making it a little hard to help.

 

I'll guess that your data contains SQL special characters that is breaking the query. You need to use mysql_real_escape_string() on all string data put into a query to escape the data.

My apologies.  Here is the full error:

 

Fatal error: Query failed: 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 't you always thought about hitting upon a hot page about [Keyword]? Are you into' at line 1 in C:\xampp\htdocs\test2.php on line 32

Reread this -

.. your data contains SQL special characters that is breaking the query. You need to use mysql_real_escape_string() on all string data put into a query to escape the data.

 

Yep, that was it.  :thumb-up:

 

Just shows how little I know about SQL.  :-[

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.