I am running an insert statement from a PHP file and then I run some code to check if the insert worked. PHP tells me "success". But when I check my database to see if the record exists, it does not. Furthermore, when I run the sql statement directly in MySQL administrator, it works fine. I am quite certain that I am not inserting into the wrong database or table. Here is a cleaned up version of the code:
$sql = "INSERT INTO companies (name, source_id, source_site, seeking_cf, currency, i_target, equity_offered, money_raised, num_investors, raised_pct, descr, city, state, country, categories_id, logo) VALUES (\"XYZ Widgets\", \"31\", 22, 1, \"£\", 250000, 0, 0, 0, 0, \"(Business Plan\", \"London\", \"\", \"UK\", 0, \"http://www.website.com/xyz.jpg\")";
$rs = mysql_query($sql, $conn);
if(mysql_affected_rows($conn) == 0) {
$utoh = "fail";
} else {
$utoh = "success";
}
echo $utoh;
So the result says "Success" but the record is not in the db. PHP is version 5.3. MySQL is 5.5.
As much as I hate to condone using mysql_
, you should do the error checking on the mysql_query
statement...
<?php
$sql = "INSERT INTO companies (name, source_id, source_site, seeking_cf, currency, i_target, equity_offered, money_raised, num_investors, raised_pct, descr, city, state, country, categories_id, logo) VALUES (\"XYZ Widgets\", \"31\", 22, 1, \"£\", 250000, 0, 0, 0, 0, \"(Business Plan\", \"London\", \"\", \"UK\", 0, \"http://www.website.com/xyz.jpg\")";
$rs = mysql_query($sql, $conn);
if (!$rs)
{
echo mysql_error();
}
else
{
echo 'success';
}
We can now see what the error was.
Use single quotes
instead of double quotes.
Your code is prone to SQL Injection. Use PDO or MYSQLI
Example of using PDO extension:
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
?>
this will allow you to insert records with single quotes.
Try this:
$sql = "INSERT INTO companies (name, source_id, source_site, seeking_cf, currency, i_target, equity_offered, money_raised, num_investors, raised_pct, descr, city, state, country, categories_id, logo) VALUES (\"XYZ Widgets\", \"31\", 22, 1, \"£\", 250000, 0, 0, 0, 0, \"(Business Plan\", \"London\", \"\", \"UK\", 0, \"http://www.website.com/xyz.jpg\")";
$rs = mysql_query($sql, $conn);
if(!$rs) {
$utoh = "fail";
} else {
$utoh = "success";
}
echo $utoh;
Did u try?:
1:
mysql_query("set names utf8;");
before mysql_query($insert)?
2:
change quotes " by ' inside query