2016-03-04 49 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