In a php script, I’ve attempted an insert statement several different ways:
$query = "INSERT INTO table (charfield,charfield,intfield,decimalfield) VALUES ('$stringvar','$stringvar',$intvar,$decimalvar)";
$query = "INSERT INTO table (charfield,charfield,intfield,decimalfield) VALUES ('".$stringvar."','".$stringvar."',".$intvar.",".$decimalvar.")";
$query = 'INSERT INTO table (charfield,charfield,intfield,decimalfield) VALUES ("'.$stringvar.'","'.$stringvar.'",'.$intvar.','.$decimalvar.')';
$query = 'INSERT INTO table (charfield,charfield,intfield,decimalfield) VALUES ("'.$stringvar.'","'.$stringvar.'","'.$intvar.'","'.$decimalvar.'")';
I’ve executed it several different ways too using combinations of these:
mysql_real_escape_string($query);
mysql_query($query);
$result = @mysql_query($query);
I’ve echo’d out the statement that is being concatenated and it looks fine. Even if I copy and paste that into phpmyadmin sql editor, it executes fine. The database is MySQL and the user has the correct permissions.
What am I doing wrong?
EDIT:
Error message using or die:
Access denied for user 'schaffins'@'localhost' (using password: NO)
I’ve added a user with the rights to select, insert, and update and I’m connecting using that user in the php before executing anything.
Also, I was able to insert into another table I made in the same script.
Sometimes the database connection happens only after certain actions &| validations take place. Make sure you have an open connection before attempting to execute any sql. If the connection is nested within an if/else code block, the conditions may not be satisfied and the db connection will not be established.
Try finding the db connection and moving (or copying) to a more ‘globalized’ location within the php – aka somewhere in the php that ensures the db connection is made before any if statements that might exclude the connection from being established based on conditionals that aren’t allowing