I am trying to insert values in database and values are not being inserted, here is the code i have:
$user_name = "username";
$password = "password";
$database = "database";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = 'INSERT INTO table (anInt, DomainName, URL, Rank, PageRank, Google, Bing, Boss, IndexedPage, Backlinks) VALUES ($anInt, $Domain, $URL, $Rank, $Pagerank, $Google, $Bing, $Yahoo, $Pages, $backlinks)';
$result = mysql_query($SQL);
mysql_close($db_handle);
print "Records added to the database";
it is printing that records added to the database but when looking at the database nothing is being added. some of the values are doubles, text, and ints. Is there anyway to debug this? I will be adding more information to the post if someone asks me to.
and of course I have an else statement i just thought it is not relevant since it is telling me that records are added.
First of all, you should escape the string values you are passing into the SQL query, using
mysql_real_escape_string.Then, you should add quotes, in your SQL query, arround the fields that are meant to contain strings.
I don’t really know which fields are integers and which fields are strings, but you should be using something like this to build your SQL query :
This is supposing that only
DomainNameandURLare meant to contain strings — you might have to usemysql_real_escape_stringand add quotes arround the values for some other fields too, if needed.Then, you should take a look at the return value of
mysql_query: for aninsertquery, in case of an error, it’ll returnfalse.Here, if your
$resultvariable isfalse, you should usemysql_errorandmysql_errno: they’ll allow you to know what error happened — it will help detecting errors in your SQL query, for instance.If this doesn’t solve the problem, you should try outputting the SQL query, and run it using something like phpMyAdmin, to make sure it’s OK.