php,将 db: 从 MySQL 更改为 txt 文件

发布于 2024-10-16 05:05:10 字数 1404 浏览 2 评论 0原文

我是编程新手,我找到这个脚本,我想根据我的需要进行调整,这个脚本将邮件地址保存在 MySQL 数据库上,但我想将其保存在文件 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");


?>

I am newbie in Programming, I find this script and I'd like to adapt this to my needs, this script save the mail address on MySQL db but I'd like to save this on file txt, is possible re-use this code?

address.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();
    }
}

where the connect.php is

<?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");


?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-10-23 05:05:10

该文件非常特定于使用数据库。然而,使用文件并不困难。我建议不要使用纯文件,而是考虑使用像 sqlite 这样的数据库,它使用文件而不是完全成熟的数据库。

如果您开始使用文件,则可以在 php 中非常轻松地操作文件。一些示例如下:

    $file = fopen('path/to/file', 'r+');

    fputs($file, '[email protected]'); 
    // fputs() is an alias for fwrite, they are interchangable

此页面详细介绍了使用 < 打开文件的不同模式代码>fopen()。您需要特别注意该模式是在文件的开头还是结尾打开文件指针。如果您向该文件写入了一些电子邮件,然后使用文件指针再次打开该文件以写入更多电子邮件,则会覆盖现有数据。这是使用数据库的一个很好的理由,因为它需要更大的麻烦才能删除现有数据。

fputs() 用于将行写入文件。显然,您可以传递变量而不是我在示例中硬编码的字符串,只需确保变量是一些有意义的字符串即可。例如,您无法传递一系列电子邮件。您必须循环遍历数组并为每封电子邮件调用 fputs()

That file is very specific to using a database. However, using a file is not difficult. I would suggest that rather than using a plain file, consider using a database like sqlite which uses files rather than a fully fledged database.

If you are set on using files, you can manipulate files very easily in php. Some examples are below:

    $file = fopen('path/to/file', 'r+');

    fputs($file, '[email protected]'); 
    // fputs() is an alias for fwrite, they are interchangable

This page details the different modes for opening a file with fopen(). You will want to pay extra special attention to whether the mode opens the file pointer at the start or end of a file. If you wrote some emails to the file, then later opened the file again with the file pointer at the start to write more emails, you would overwrite the existing data. This is a good reason to use a database as it requires a bigger screw up to delete your existing data.

fputs() is used to write lines to your file. Obviously you can pass a variable rather than the string I've put hard coded in the example, just make sure the variable is some meaningful string. You can't, for example, pass an array of emails. You would have to loop over the array and call fputs() for each email.

赢得她心 2024-10-23 05:05:10

你可以重复使用它的一部分;真正将数据保存到数据库的行是从 $mysqli->query 开始的行,后面的行是检查是否成功。删除它们,并将它们替换为您自己的文本文件保存函数(并将它们放入包含文件中,而不是定义 mysqli 连接器的给定文件中)。

You could reuse parts of it; the line that actually saves the data to the database is the line starting $mysqli->query, and the line afterward is the check for success. Remove those, and replace them with your own text file saving functions (and put those in an include file, rather than the given one which defines the mysqli connector).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文