-1

嗨,伙计们,我在将表单数据添加到数据库时遇到问题。由于某种原因,没有插入数据。这是我的代码:

<?php include_once 'secure/connect.php'; ?>
<?php 
$name = "Your Name";
$email = "Your Best Email";
$msg_to_user = "";
if ($_POST['name'] != ""){
    //Be sure to filter this data to deter SQL injection
    $name =  $_POST['name'];
    $name = stripslashes($name);
    $name = strip_tags($name);

    $email = $_POST['email'];
    $email = stripslashes($email);
    $email = strip_tags($email);

    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);
    if(!$email){
        $msg_to_user = '<h4><font color="FF0000">Please Type an email address ' . $name . '</font></h4>';
    } else if ($numRows > 0) {
        $msg_to_user = '<h4><font color="FF0000">' . $email . ' is already in our system</font></h4>';
    } else {
        $sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) VALUES ('$name', '$email', now())") or die (mysql_error());
        $msg_to_user = '<h4><font color="0066FF">Thanks' . $name . ', You have been added successfully</font></h4>';
        $name = "";
        $email = "";
    }
}
?>

我的 html 表单如下所示:

<div class="topForm">
<H3 style="text-align:center">SIGN UP FOR OUR NEWSLETTER</H3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="name" value="<?php echo $name; ?>"/>
<input type="text" name="email" value="<?php echo $email; ?>"/><br/>
<input name="mySubmitBtn" type="submit"  value="SUBMIT">
<?php echo $msg_to_user; ?>
</form>
</div>

非常感谢所有菲利普

这就是我现在所拥有的,没有任何工作仍然有效......

<?php 
$name = "Your Name";
$email = "Your Best Email";
$msg_to_user = "";
if ($_POST['name'] != ""){

    include_once 'secure/connect.php';

    //Be sure to filter this data to deter SQL injection
    $name =  $_POST['name'];
    $name = stripslashes($name);
    $name = strip_tags($name);

    $email = $_POST['email'];
    $email = stripslashes($email);
    $email = strip_tags($email);

    $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
    $numRows = mysql_num_rows($sql);
    if(!$email){
        $msg_to_user = '<h4><font color="FF0000">Please Type an email address ' . $name . '</font></h4>';
    } else if ($numRows > 0) {
        $msg_to_user = '<h4><font color="FF0000">' . $email . ' is already in our system</font></h4>';
    } else {
        $sql_insert = mysql_query("INSERT INTO newsletter (name, email) VALUES ('".$name."', '".$email."')") or die (mysql_error());
        $msg_to_user = '<h4><font color="0066FF">Thanks' . $name . ', You have been added successfully</font></h4>';
        $name = "";
        $email = "";
    }
}
?>
4

3 回答 3

1

你必须改变now()你的代码。并使用以下代码。

$time = time()  ;
$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime) VALUES ('".$name."', '".$email."', '".$time."' )") or die (mysql_error());
于 2012-12-20T12:22:37.560 回答
1

不考虑其他错误或不一致。另请注意,您应该使用 mysqli 或 pdo。但是php使用time()

$sql_insert = mysql_query("
                           INSERT INTO newsletter 
                           (name, email, dateTime) 
                           VALUES 
                           ('$name', '$email', ".time().")
                         ");

或者如果你想要一个日期时间而不是时间戳,你可以使用这个date()函数。

于 2012-12-20T12:02:16.883 回答
0
  1. 确保您已连接到数据库!看看怎么echo mysql_error();

  2. 如果提交了表单,则捕获值,然后进行清理

  3. 插入查询

ps:看看下面是做什么的:

if(isset($_POST['name']) ...

echo mysql_insert_id();

time() not now()

查看id插入的新数据

如果您按照这些步骤操作并且已连接到数据库,您的代码应该可以工作

于 2012-12-20T12:06:59.353 回答