【发布时间】:2011-02-07 21:18:18
【问题描述】:
我是编程新手,我找到了这个脚本,我想根据我的需要调整它,这个脚本将邮件地址保存在 MySQL db 中,但我想将它保存在文件 txt 中,可以重新使用此代码?
地址.php
require "includes/connect.php";
$msg = '';
if($_POST['email']){
// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
try{
if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
throw new Exception('Invalid Email!');
}
$mysqli->query("INSERT INTO coming_soon_emails
SET email='".$mysqli->real_escape_string($_POST['email'])."'");
if($mysqli->affected_rows != 1){
throw new Exception('This email already exists in the database.');
}
if($ajax){
die('{"status":1}');
}
$msg = "Thank you!";
}
catch (Exception $e){
if($ajax){
die(json_encode(array('error'=>$e->getMessage())));
}
$msg = $e->getMessage();
}
}
connect.php 在哪里
<?php
error_reporting(E_ALL ^ E_NOTICE);
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
@$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno()) {
die('<h1>Could not connect to the database</h1><h2>Please try again after a few moments.</h2>');
}
$mysqli->set_charset("utf8");
?>
【问题讨论】:
-
“重用”这个没有意义,因为可能有 5% 的代码与数据库无关。
-
是的,这听起来像是完全重写
标签: php mysql email text-files