这是我提交表单后的代码。我已经成功地在页面上显示值,但我希望将这些相同的值插入到数据库中,同时将它们作为电子邮件发送给用户和管理员电子邮件。如果表单$_POST
的一部分值,则为用户的电子邮件。我想稍后提取并更新值。那不是问题。问题是实现保存到数据库,电子邮件客户端和管理员,同时或一个接一个地保存到数据库。
注意:我正在使用API在显示之前直接转换某些值,因此在控制器或模型中执行此操作可能会保存原始值而不是转换后的值,这是不需要的。除非有更好的方法来做到这一点。我正在使用CodeIgniter 3。
请帮帮我。谢谢。代码如下:
<?php
function generateRandomString($length = 26) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
?>
<?php
$user_id = $_POST['user_id'];
$email = $_POST['email'];
$username = $_POST['username'];
$txn_ref = strtoupper(generateRandomString());
$txn_status = "Pending";
$exc_from = $_POST['exc_from'];
$exc_to = $_POST['exc_to'];
$from_amount = $_POST['from_amount'];
$to_amount = $_POST['to_amount'];
$account = $_POST['account'];
?>
<div class="row">
<h3>Your <?php echo $exc_from ?> to <?php echo $exc_to ?> Transaction Details</h3>
<div class="table-responsive">
<table class="table table-striped">
<tbody>
<tr>
<th>Payment ID</th>
<td><?php echo $txn_ref ?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo $email ?></td>
</tr>
<tr>
<th>Username</th>
<td><?php echo $username ?></td>
</tr>
<tr>
<th>You Give</th>
<td><?php echo $from_amount ?> <?php echo $exc_from ?></td>
</tr>
<tr>
<th>You Get</th>
<td><?php echo $to_amount ?> <?php echo $exc_to ?></td>
</tr>
<tr>
<th>Your <?php echo $exc_to ?> Wallet</th>
<td><?php echo $account ?></td>
</tr>
<tr>
<th>Transaction Status</th>
<td><?php echo $txn_status ?></td>
<td></td>
</tr>
<tr>
<th>Make Payment to</th>
<td><?php echo $ouraccount; ?></td>
<td></td>
</tr>
<tr>
<th>View in Dashboard</th>
<td><a href="<?=base_url()?>transaction/<?php echo $txn_ref ?>">View Transaction</a><br></td>
<td></td>
</tr>
</tbody>
</table>
答案 0 :(得分:0)
对于对我的问题的答案感兴趣的人,我工作了一下,想出了什么对我有用。也许它可以帮助你。我开始编辑,因为我不太确定我是否遵循了最佳实践,但我遵循的大部分最佳实践都没有给出预期的结果。一切都按此顺序进入同一个文件。
<?php
function generateRandomString($length = 26) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString; }?>
<?php
$user_id = $_POST['user_id'];
$email = $_POST['email'];
$username = $_POST['username'];
$txn_ref = strtoupper(generateRandomString());
$txn_status = "Pending";
$exc_from = $_POST['exc_from'];
$exc_to = $_POST['exc_to'];
$from_amount = $_POST['from_amount'];
$to_amount = $_POST['to_amount'];
$account = $_POST['account'];
?>
在页面上显示
<!-- Display Values -->
<div class="row">
<h3>Your <?php echo $exc_from ?> to <?php echo $exc_to ?> Details</h3>
<div class="table-responsive">
<table class="table table-striped">
<tbody>
<tr>
<th>Payment ID</th>
<td><?php echo $txn_ref ?></td>
</tr>
...
<th>View in Dashboard</th>
<td><a href="<?=base_url()?>transaction/<?php echo $txn_ref ?>">View
Transaction</a><br></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
插入数据库
<?php
//Insert into Database
$params = array(
'user_id' => $user_id ,
'email' => $email ,
'username' => $username ,
'txn_ref' => $txn_ref ,
'txn_status' => $txn_status ,
'exc_to' => $exc_to ,
'exc_from' => $exc_from ,
'from_amount' => $from_amount ,
'to_amount' => $to_amount ,
'account' => $account ,
);
$this->db->insert('transactions',$params);
return $this->db->insert_id();
?>
发送电子邮件
<?php
//Send as Email
$m_user_id = $user_id;
$m_email = $email;
$m_username = $username;
$m_txn_ref = $txn_ref;
$m_exc_to = $exc_to;
$m_exc_from = $exc_from;
$m_from_amount = $from_amount;
$m_to_amount = $to_amount;
$m_account = $account;
$message = 'Message Here';
$this->load->library('email');
config['mailtype'] = 'html';
$this->email->from('from@email.com', 'From');
$this->email->to($m_email);
$this->email->cc('cc@email.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
if ($this->email->send(FALSE))
{
echo 'Mail was not sent';
}
?>
我希望这会有所帮助