更新已在PHP中执行的表
我有一个这样的文本文件更新已在PHP中执行的表,php,mysql,database,Php,Mysql,Database,我有一个这样的文本文件 1 wordsgohere 2 morewordsgohere 3 yougetthepoint 我想将上面的一个字符串分配给此人的用户id。假设你是第三个注册的人,你的用户id是3,你的存款id是“yougetthepoint” // write new users data into database $query_new_user_insert = $this->db_connection->prepare('INSERT INTO u
1 wordsgohere
2 morewordsgohere
3 yougetthepoint
我想将上面的一个字符串分配给此人的用户id。假设你是第三个注册的人,你的用户id是3,你的存款id是“yougetthepoint”
// write new users data into database
$query_new_user_insert = $this->db_connection->prepare('INSERT INTO users (deposit_id, user_name, user_password_hash, user_email, user_activation_hash, user_registration_ip, user_registration_datetime) VALUES(:deposit_id, :user_name, :user_password_hash, :user_email, :user_activation_hash, :user_registration_ip, now())');
$query_new_user_insert->bindValue(':deposit_id', 'placeholderid', PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_name', $user_name, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_password_hash', $user_password_hash, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_email', $user_email, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_activation_hash', $user_activation_hash, PDO::PARAM_STR);
$query_new_user_insert->bindValue(':user_registration_ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
$query_new_user_insert->execute();
// id of new user
$user_id = $this->db_connection->lastInsertId();
// searches text file for address
$lines_array = file("test.txt");
foreach($lines_array as $line) {
echo $line;
if(strpos($line, $user_id) != false) {
list(, $new_str) = explode($user_id, $line);
}
}
现在,这会将所需的存款id放入新的存款str。现在,我需要使用新的存款str更新表中的存款id
任何帮助都会很好
已尝试此操作,但无法将其更新。我只是想看看是否可以只更新第一个用户的帐号
$query_new_user_update = $this->db_connection->prepare('UPDATE users set deposit_id :deposit_id where user_id =1');
$query_new_user_update->bindValue(':deposit_id', 'doesthischange', PDO::PARAM_STR);
$query_new_user_update->execute();
您需要在底部添加如下内容:
$query_new_user_update = $this->db_connection->prepare('UPDATE users set deposit_id = :deposit_id where user_id = last_insert_id()');
$query_new_user_update->bindValue(':deposit_id', $new_str, PDO::PARAM_STR);
$query_new_user_update->execute();
在本例中,last\u insert\u id()
编辑:
顺便说一句,你可能想要改变
if(strpos($line, $user_id) != false) {
// bad: does not recognize strings that start with $user_id.
// It only recognizes strings that contain $user_id when
// the $user_id is not at the start of the line.
进入
!=false
将无法正常工作,因为当一行以用户id开头时,str\u pos
将返回0
,这意味着用户id位于字符位置0。但是0==false
,因为它是松散的比较。因此,您现在需要使用进行严格比较的==
,以检查匹配类型<代码>0!==false
,因为0是INT,false是BOOL
同时避免将用户id(例如
1
)与右侧文本中的任何数字匹配。或者使用包含该数字的其他用户标识。(例如11
,12
,13
,等等)您还需要搜索以用户id开头的字符串,并检查用户id后的空白处<代码>更新语句并没有那么不同。你应该能够通过谷歌找到例子。如果你的文本文件已经作为一个不同的表存在于数据库中,那么可能的副本会更好。在这种情况下,第一个查询中的子查询就足够了。您需要在用户id后面使用空格来执行strpos。否则,用户10、11、12等将获得用户1的文本,这正是我一直在寻找的。除了我不能让它工作。我试着改变它,看看它是否会用一个随机字符串来更新用户ID1的存款id,而什么都没有。将在帖子中发布代码。@SolidCloudinc aay,在我的3行中,第二行仍然包含query\u new\u user\u insert
,而不是query\u new\u user\u update
。我现在就在我的答案中编辑它。现在再试一次。是的,我注意到了并更改了它,但它仍然没有更新它。我还尝试了更新用户1。但是,新的if语句可以在旧的if语句不能工作的地方工作。@SolidCloudinc究竟什么是mysql类型?智力?瓦查尔?文本。。。确保它不是数字类型(所以不是Int、Float、Decimal)@SolidCloudinc-ow,我现在看到了。我在存款id
和:存款id
之间也缺少一个=
。但实际上,您应该学习如何发现并读取失败查询的查询错误消息。
if(strpos($line, $user_id . ' ') === 0) {
// good: only recognizes string that start with
// $user_id followed by a white space