0
我在PHP中製作簡單的郵件系統。如何將名稱轉換爲id並將其保存爲數據庫作爲PHP中的id?
我用了兩個表:
- 用戶表
- 信息表
我登錄時使用的來自用戶的表,有太多使用會話。 使用ID消息發送消息正在發送。但是,當我在「mto」字段中使用它們的名稱時,它會自動帶上0的ID ..
任何人都可以回答我嗎?
撰寫按鈕的代碼:
<a class="btn btn-block btn-compose btn-lg" id="loginButton"><i class="fa fa-" ></i> Compose Mail </a>
<!-- The login modal. Don't display it initially -->
<form id="loginForm" method="post" class="form-horizontal" style="display: none;">
<div class="form-group">
<label class="col-xs-3 control-label">To</label>
<div class="col-xs-5">
<input type="hidden" class="form-control" name="from" />
</div>
<div class="col-xs-5">
<input type="text" class="form-control" name="to" value="<?php echo 'name' ?>"/>
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Subject</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="ssubject" />
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Message</label>
<div class="col-xs-5">
<textarea name="smessage" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-3">
<button type="submit" class="btn btn-default">Send</button>
</div>
</div>
</form>
如果我們把這個id然後它需要id,但我想從用戶名採取名稱並存儲在消息表中的id。
請幫幫我。
<?php
include('../session.php');
$from="$session_id";
$to=mysql_real_escape_string($_POST["to"]);
$subject=mysql_real_escape_string($_POST["ssubject"]);
$message=mysql_real_escape_string($_POST["smessage"]);
if($from==$to){
echo "Message could not be sent";
die();
}
if($from>1 && $to>1){
echo "Message could not be sent";
die();
}
//echo $to.$message;
//validate before insert
if(strlen($subject)>50){
echo "Subject was too long";
die();
}
if(strlen($message)>150){
echo "Message was too long";
die();
}
include('../dbsource.php');
$mysqli=connect();
if(insert($mysqli,$from,$to,$subject,$message)>0)
{
echo "<script type=\"text/javascript\">".
"alert('Message was successfully sent');".
"</script>";
// $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
}
else
{
echo "<script type=\"text/javascript\">".
"alert('there was error sending message.. please try it later');".
"</script>";
}
?>
<?php
function insert($mysqli,$from,$to,$subject,$message){
$mdate=date('Y-m-d');
$mtime=date('H:i:s');
$query = "INSERT INTO messages (mfrom,mto,subject,message,mdate,mtime)".
"VALUES ('$from','$to','$subject','$message','$mdate','$mtime')";
if($mysqli->query($query)>0)
return($mysqli->insert_id);
else
return 0;
}
?>
我對用戶和消息表有所認識,用戶「id」與消息「to」和「from」字段有關係 –