0

そのため、ニュースレターのサインアップフォームのユーザー情報を保存しようとしています.XAMPPでは正常に機能しますが、ライブDB(GoDaddy)にデータを保存するのに問題があります. また、エラーステートメントをエコーアウトし、クエリが空であると言いましたが、理由がわかりません...これが私のコードです(メール複製テストもローカルで機能しましたが、明らかに機能しなくなりました):

<?php

//Start session
session_start();

//Log into the server
@ $db = mysql_pconnect("hostnamewashere", "usernamewashere", "pwwashere");

//Select the database desired
mysql_select_db("databasenamewashere");

//If no connection can be made, echo it out to the screen
if(!$db){
    echo "Error: Could not connect to the database. Please try again later.";
exit;
}

//Retrieve data from form and create variables
$email = Trim(($_POST['email'])); 
$ip_orig = $_SERVER['REMOTE_ADDR'];
$ip = ip2long($ip_orig);
$date = date('Y-m-d');  
$time = date('H:i:s');  

//Validate data
if(empty($_POST['email'])) {
$_SESSION["errorMsg"] = "Please enter your email.";
Header("Location: index.php");
exit;
}

if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", ($_POST['email']))) { 
$_SESSION["errorMsg"] = "Incorrect email format; please try again.";
Header("Location: index.php");
exit; 
}

//Check to see that no duplicates exist in the database
$sql = "SELECT newsletter_email FROM Newsletter WHERE newsletter_email='".$email."'";
$result = mysql_query($sql);

if(empty($result)) {
$num_results = 0;
}else {
$num_results = mysql_num_rows($result);
}

if($num_results !== 0) {
$_SESSION["errorMsg"] = "Sorry, that email address is already in our database.";
Header("Location: index.php");
exit;
}

//Place into database
$sql = "INSERT INTO Newsletter(newsletter_email, newsletter_ip, newsletter_date,     newsletter_time) VALUES('".$email."', '".$ip."', '".$date."', '".$time."')";

//Run query and get result back; shouldn't return anything
$result = mysql_query($sql);

//Redirect user to successful registration page
Header("Location: index.php");
$_SESSION["errorMsg"] = "Thanks! We will be in touch with you soon.";
exit;

//Close the database connection
mysql_close($db);

?>
4

1 に答える 1

1

スクリプトを少し書き直しましたが、実際に見た唯一のエラーは、date_default_timezone_set("America/Los_Angeles"); がなかったことです。設定。

以下のスクリプトは私にとってはうまくいきます:

<?php
//Start session
session_start();
date_default_timezone_set("America/Los_Angeles");

$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'pass';
$dbname = 'test';

if ($_POST)
{

    //Retrieve data from form and create variables
    $email = trim($_POST['email']); 
    $ip_orig = $_SERVER['REMOTE_ADDR'];
    $ip = ip2long($ip_orig);
    $date = date('Y-m-d');  
    $time = date('H:i:s');  

    //Validate data
    if(empty($_POST['email'])) {
    $_SESSION["errorMsg"] = "Please enter your email.";
    header("Location: index.php");
    exit;
    }

    if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", ($_POST['email']))) { 
        $_SESSION["errorMsg"] = "Incorrect email format; please try again.";
        header("Location: index.php");
        exit; 
    }

    //Log into the server
    $db = @mysql_pconnect($dbhost, $dbuser, $dbpass);

    //Select the database desired
    mysql_select_db($dbname);

    //If no connection can be made, echo it out to the screen
    if(!$db){
        echo "Error: Could not connect to the database. Please try again later.";
    exit;
    }

    //Check to see that no duplicates exist in the database
    $sql = "SELECT newsletter_email FROM Newsletter WHERE newsletter_email='".$email."'";
    $result = mysql_query($sql);

    if (mysql_num_rows($result) > 0)
    {
        $_SESSION["errorMsg"] = "Sorry, that email address is already in our database.";
        header("Location: index.php");
        exit;
    }

    //Place into database
    $sql = "INSERT INTO Newsletter(newsletter_email, newsletter_ip, newsletter_date, newsletter_time) VALUES('".$email."', '".$ip."', '".$date."', '".$time."')";

    //Run query and get result back; shouldn't return anything
    $result = mysql_query($sql);

    //Redirect user to successful registration page
    $_SESSION["errorMsg"] = "Thanks! We will be in touch with you soon.";
    header("Location: index.php");
    exit;

    //Close the database connection
    mysql_close($db);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="index.php">
<input name="email" value="" />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>
于 2012-06-26T15:48:49.530 に答える