![](/img/trans.png)
[英]Storing a string in a variable in php and searching for that variable in mysql database?
[英]Searching a file and storing it in a variable php
我有一个像这样的文本文件
1 wordsgohere
2 morewordsgohere
3 yougetthepoint
我想将上述字符串之一分配给该人的user_id。 假设您是第三人注册,您的user_id为3,而deposit_id为'yougetthepoint'。 但是,当我回显user_id时,即使数据库中有2或3个用户,它也始终为0,并且在查看数据库时,id数会增加。 它也不会把用户放在数据库中。 如果我用其他东西替换deposit_id,它将把用户放在数据库中。 我认为是因为new_str从未定义。
// id of new user
$user_id = $this->db_connection->lastInsertId();
echo $user_id;
// 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);
}
}
// 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', $new_str, 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。
现在,您想将Deposit_id添加到生成的条目中。 只需更新条目( UPDATE users SET deposit_id=:deposit_id WHERE user_id=:user_id;
)。
但我认为您会得到想要的结果。
文本文件中的数字真的是user_id吗? 或只是一个枚举? 您可以解析它并创建一个按文件顺序包含deposit_ids的数组。 现在,您可以通过对每个数组条目运行一个插入来插入所有行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.