PHP + MySQL UPDATE查询(多列)不起作用

时间:2013-07-17 22:40:51

标签: php mysql

好吧,这可能是我忽视的一些简单的东西,但我已经梳理过这么多次......!

只需通过PHP表单进行简单更新,提取变量,构建查询:

// Connect
$connect = mysql_connect('host','login','passwd');
if (!$connect) { $errors .= "Not connected : ".mysql_error(); }
$database = mysql_select_db('db', $connect);
if (!$database) { $errors .= "Could not get to that database: ".mysql_error(); }

// Update database table
$info = "UPDATE `gsa_officers` SET `term` = '2012-2013', `office` = 'President', `type` = 'Poop', `dept` = 'Visual Arts', `name` = 'Matthew W. Jarvis', `email` = 'president@gsa.ucsd.edu', `blurb` = 'Serves as Chair of both the GSA Executive Committee and Council, oversees the direction of the organization and ensures the execution of its responsibilities and commitments.', `picture` = 'http://gsa.ucsd.edu/sites/gsa.ucsd.edu/files/mat%20w%20jarvis.jpg' WHERE `id` = 1";
$query = mysqli_query($info);
if (!$query) { $errors .= "Bad query: ".mysql_error(); }
mysql_close($connect);

我没有错误,只打印出来:“查询错误:”

我错过了什么/做错了什么? 任何额外的眼球都会被赞赏^ _ ^

4 个答案:

答案 0 :(得分:1)

您正在使用mysql_connect()连接到数据库服务器并选择数据库。然后使用mysqli_query()运行查询,然后使用mysql_error()报告错误。你不能混用这些API。

您应该始终使用mysqli_*个函数,因为mysql_*函数已弃用,将从PHP的未来版本中删除。

示例:

// Connect
$mysqli = new mysqli('host','login','passwd','db');
if ($mysqli->connect_error) { $errors .= "Not connected : ".$mysqli->connect_error; }

// Update database table
$info = "UPDATE `gsa_officers` SET `term` = '2012-2013', `office` = 'President',
  `type` = 'Poop', `dept` = 'Visual Arts', `name` = 'Matthew W. Jarvis', 
  `email` = 'president@gsa.ucsd.edu', 
  `blurb` = 'Serves as Chair of both the GSA Executive Committee and Council, oversees the direction of the organization and ensures the execution of its responsibilities and commitments.',
  `picture` = 'http://gsa.ucsd.edu/sites/gsa.ucsd.edu/files/mat%20w%20jarvis.jpg' 
  WHERE `id` = 1";
if (!$mysqli->query($info)) {
    $errors .= "Bad query: ".$mysqli->error;
}
$mysqli->close();

答案 1 :(得分:0)

更改此行

$query = mysqli_query($info);

$query = mysql_query($info);

注意

mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

答案 2 :(得分:0)

您使用简单mysql(不是mysqli)进行连接,但使用mysqli_query发送查询

答案 3 :(得分:0)

如果你使用mysql_query而不是mysqli_query怎么办?