我正在尝试在db中放入一些数据,但出现了问题。 这是我的代码:
$insert = mysql_query("
INSERT INTO wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name) VALUES ('NULL', 'teste', MD5('teste'), 'O Meu Nome', 'o_meu@email.com', 'http://www.o_meu_site.com/', '2011-06-15 00:00:00', '', '0', 'O Meu Nome');
");
$insert2 = mysql_query("
INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, '" . mysql_insert_id() . "', 'wp_capabilities', 'a:1:{s:13:\"administrator\";b:1;}');
");
$insert3 = mysql_query("
INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, '" . mysql_insert_id() . "', 'wp_user_level', '10');
");
?>
问题是: 最后一个INSERT是使用mysql_insert_id()不使用第一个查询中的ID,它使用的是umeta_id。 我需要改变什么? 请帮忙
答案 0 :(得分:1)
$insert = mysql_query("
INSERT INTO wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name) VALUES (NULL, 'teste', MD5('teste'), 'O Meu Nome', 'o_meu@email.com', 'http://www.o_meu_site.com/', '2011-06-15 00:00:00', '', '0', 'O Meu Nome');
");
$insertId = mysql_insert_id();
$insert2 = mysql_query("
INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value)
VALUES (NULL, '" . $insertId . "', 'wp_capabilities', 'a:1:{s:13:\"administrator\";b:1;}');
");
$insert3 = mysql_query("
INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value)
VALUES (NULL, '" . $insertId . "', 'wp_user_level', '10');
");
?>
答案 1 :(得分:1)
答案 2 :(得分:1)
在第一个插入语句中,您在单引号中嵌入了null。它将是一个字符串值
答案 3 :(得分:0)
您需要将返回的值存储在变量中,因为mysql_insert_id()
将始终使用上一个INSERT
语句中的ID。
mysql_query("
INSERT INTO wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name) VALUES ('NULL', 'teste', MD5('teste'), 'O Meu Nome', 'o_meu@email.com', 'http://www.o_meu_site.com/', '2011-06-15 00:00:00', '', '0', 'O Meu Nome');
");
$id = mysql_insert_id();
mysql_query("
INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, '" . $id . "', 'wp_capabilities', 'a:1:{s:13:\"administrator\";b:1;}');
");
mysql_query("
INSERT INTO wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, '" . $id . "', 'wp_user_level', '10');
");