繁体   English   中英

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

[英]PHP+MySQL UPDATE query (multiple columns) not working

好吧,我可能忽略了一些简单的事情,但是我已经梳理了这么多次。

只需通过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);

我没有收到任何错误,它只会打印出:“错误查询:”

我想念/做错了什么? 任何额外的眼球都表示赞赏^ _ ^

您正在使用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();

更改此行

$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.

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM