2016-03-04 47 views
0

任何人都可以告诉我为什么下面的代码给了我一个错误页面500.还有,怎么样,我可以纠正它。PHP MySql插入给服务器错误500

mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); 
mysql_select_db("xxx") or die(mysql_error()); 

$sql = "INSERT INTO oc2_ads (id_user, id_category) 
VALUES ('$id_user', '$id_category')"; 
mysql_query($sql); 

if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close(); 

谢谢

+0

你在使用什么服务器? – Oliver

+4

请[停止使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [这些扩展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[编写]​​(http://en.wikipedia.org/ wiki/Prepared_statement)语句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+2

[您的脚本存在SQL注入攻击的风险。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

回答

4

不能混合数据库的API。您从旧的mysql_*函数开始,然后转到某些OOP版本API(MySQLi或PDO)。如果你想使用旧的API,一路过关斩将,你这样做:

mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); 
mysql_select_db("xxx") or die(mysql_error()); 
$sql = "INSERT INTO oc2_ads (id_user, id_category) 
VALUES ('$id_user', '$id_category')"; 
$result = mysql_query($sql); 

if ($result === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . mysql_error(); 
} 

Your script is at risk for SQL Injection Attacks.

stop using mysql_* functionsThese extensions已在PHP 7中删除。了解有关PDOMySQLiprepared对帐单,并考虑使用PDO,it's really pretty easy