为什么我的订阅页面没有将字段放在MySQL数据库中?

时间:2013-12-26 17:20:19

标签: php mysql jquery

我有疑问,

我正在尝试将数据发送到我的数据库但它没有插入它们。我怎么能把它们送去发送呢?

index.php(脚本部分):

<div id="wel">
        <p id="title-call">Junta-te <font color="gray">a n&oacute;s!</font></p><hr color="#c96308"  noshadow />
        <p id="subx">Percorre este mundo e torna-te o treinador n&uacute;mero <font color="#c96308">UM</font>!</p> 
    </div>  
    <div id="loading"><br /></div>
    <div id="warn2"><br /></div>
    <div id="success"><br /></div>
    <div id="subscribe">
        <form id="sub" action="" method="POST" >
            <input type="text" name="username" id="username" placeholder="Escreve aqui o teu Nome..." /><br />
            <br />
            <input type="text" name="email" id="email" placeholder="Escreve aqui o teu E-mail..." /><br /><br />
            <input type="submit" id="submit" value="Subscrever" />
        </form>
    </div>
<script type="text/javascript">
    $(document).ready(function() {

        $("#sub").submit(function() {

            if($("#username").val()==""||$("#email").val()=="") {
                $("#warn2").hide(36000);
                $("#loading").css("display:inline");
                $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
                $("#loading").fadeIn(200);
                $("#loading").fadeOut(200);
                $("#warn2").css("display:none");
                $("#warn2").html("Por favor escreve o teu nome e/ou email!<br />");
                $("#warn2").hide(36000);
                $("#warn2").fadeIn(1000);                   
                $("#warn2").css("display:inline");
                $("#warn2").fadeOut(7000);

            }

            else {
                var s = $("#sub").serializeArray();

                $.ajax({
                    url: "subscribe.php",
                    method: "POST",
                    data: s,
                    success: function() {


                            $("#success").css("display:none");              
                            $("#success").hide(36000);
                            $("#loading").css("display:inline");
                            $("#loading").html("<img src='images/ajax-loader.gif' width='1%' height='3%' /> A carregar...");
                            $("#loading").fadeIn(200);
                            $("#loading").fadeOut(200);
                            $("#success").css("display:none");
                            $("#success").html("A tua subscri&ccedil;&atilde;o foi efectuada com successo!");
                            $("#success").hide(36000);
                            $("#success").fadeIn(1000);                 
                            $("#success").css("display:inline");
                            $("#success").fadeOut(7000);

                    }

            });

        }

        return false;




    });

        $("#MGa").click(function() {

            $("#hidGames").css("display: inline");
            $("#hidGames").fadeToggle(1000);


            return false;
        });

        $("#MGa").mouseover(function() {

            $("#arrow").html('<img src="images/arrow_wbm_hover.png" width="1%" height="1%" />');


        });

        $("#MGa").mouseout(function() {

            $("#arrow").html('<img src="images/arrow.png" width="1%" height="1%" />');


        });


        $("#arrow").html('<img src="images/arrow.png" width="1%" height="1%" />');

    });
</script>

subscribe.php:

<?php

include_once('db.php');

$username = $_POST["username"];
$email = $_POST["email"];
$date = date("Y-m-d h:i:s:A");


          $sql = mysql_query("INSERT INTO subscribers VALUES('','$username','$email','$date')");

         // mail function //

         $to = $email;
         $header = "From: info@wbm.pt";
         $header. = "Content-Type: text/html;";
         $subject = "Bem-vindo ao WBM!";
         $body = "
         Bem vindo ao WBM (World Basket Manager) <b>$username</b>,<br />
         <br />
         S&ecirc; o treinador n&uacute;mero <i>UM</i> deste mundo de treinadores.<br />
         - Cria a tua equipa;<br />
         - Compra e Vende jogadores;<br />
         - Calend&aacute;rio real;<br />
         - Equipas Originais (NBA);<br />
         - Est&aacute;dios;<br />
         - Forma as tuas pr&oacute;prias t&aacute;ticas;<br />
         <br>
         Tudo isto no mundo de treinadores de basquetbol!<br />
         Lan&ccedil;amento da vers&atilde;o BETA:<br />
            <b>Data prev&iacute;sivel: <i>25 de Maio de 2014</i></b><br />
         <br />
         Muito Obrigado,<br />
         <b>Equipa WBM</b>
         <br />
         <br />
         <h6>N&atilde;o responder a este e-mail!</h6>

         "; 

         // mail //

         mail($to,$header,$subject,$body);

    $rw = mysql_query("SELECT * FROM subscribers");

    $rows = mysql_num_rows($rw);

    if($rows!=0) {
        $nrow = $rows;
    } 
    else {

    }

         $to1 = "cell phone number";
         $header1 = "From: 15030@wbm.pt";
         $header1. = "Content-Type: text/play;";
         $subject1 = "Bem-vindo ao WBM!";
         $body1 = "
            T&ecirc;m $nrow que se subscreveram.

            Muito Obrigado.
            (Não responder a esta mensagem)

         "; 

         //sms to my cell phone //
         mail($to1,$header1,$header1.,$subject1,$body1);


?>

db.php:

<?php

$connect = mysql_connect('localhost','root','');
mysql_select_db('subscribers');

?>

我能做些什么工作呢?

1 个答案:

答案 0 :(得分:2)

你需要检查mysql错误,这会给你一个很大的线索。

$query = "INSERT INTO subscribers VALUES('','$username','$email','$date')";
$sql = mysql_query($query) or throw new Exception("Query Failed: {$query}. Error: ".mysql_error());

您的代码对SQL注入是开放的,此时您应该使用MySQLi或PDO,如果您要坚持使用mysql,请转义数据。