如何将php,sql和Javascript连接在一起,

时间:2016-05-12 03:44:12

标签: javascript php sql

我正在登录页面,我有javascript做验证(检查字段是否为空)sql存储数据和php做什么php做(idk)....无论如何,当我按提交它告诉我不能POST /login.php

是否可以在网站上对其进行测试,看看它是否真的有效,或者代码是完全错误的。

   public static object CreateBinding(string binding, object service)
   {
       switch (binding)
       {
           case "ServiceA":
               ChannelFactory<IServiceA> ServiceFactoryA = new ChannelFactory<IServiceA> (binding);
               service = ServiceFactoryA.CreateChannel();
               break;

           case "ServiceB":
               ChannelFactory<IServiceB> ServiceFactoryB = new ChannelFactory<IServiceB> (binding);
               service = ServiceFactoryB.CreateChannel();
               break;

           default:
               ChannelFactory<IServiceC> ServiceFactoryC = new ChannelFactory<IServiceC> (binding);
               service = ServiceFactoryC.CreateChannel();
               break;
       }

       OpenChannel(service);

       return service;
   }

php external

<?php

$server = 'localhost';
$username = 'root';
$passowrd = 'cosc_453';
$dbname = 'login'


if(!empty($_POST['user']))


{ $query = mysql_query("SELECT * FROM UserName where userName ='$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 

$row = mysql_fetch_array($query) or die(mysql_error());


{ $_SESSION['userName'] = $row['pass']; echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; } 



else { echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; 
    }
  }
 }

if(isset($_POST['submit'])) 


{ SignIn();

 } ?>

javscript外部

     function validate(){

   if ( document.getElementById (user).value=="")
      {
       alert ("Please enter your user name");
   }
   else if ( document.getElementById(pass).value=="")
alert("Please enter you password");
 else {
alert("Processing Login........");
}
      }

sql external

  CREATE TABLE UserName ( 
 UserNameID int(9) NOT NULL auto_increment,
userName VARCHAR(40) NOT NULL, 
 pass VARCHAR(40) NOT NULL, 
PRIMARY KEY(UserNameID) );

 INSERT INTO 
UserName (userName, pass) 
VALUES
 ("cosc" , "453");

2 个答案:

答案 0 :(得分:0)

您的mysql没有与数据库的连接。请停止使用mysql,改为使用mysqli

<?php

    $con = mysqli_connect("localhost","root","cosc_453","login");

    // Check connection
    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "SELECT * FROM UserName WHERE userName ='".$_POST[user]."' AND pass = '".$_POST[pass]."'";
    $result = mysqli_query($conn,$sql);
    $count_result = mysqli_num_rows($result);

    // Login Success URL

    if($count_result  == 1)
    {
        // If you validate the user you may set the user cookies/sessions here
        #setcookie("logged_in", "user_id");
        #$_SESSION["logged_user"] = "user_id";

        $_SESSION["secret_id"] = $row['secret_id'];

        if($row['level'] == 1)
        {
            // Set the redirect url after successful login for admin
            $resp['redirect_url'] = 'admin/';
        }

        else if($row['level'] == 2)
        {
            // Set the redirect url after successful login for user
            $resp['redirect_url'] = 'user/';
        }

    }
    else
    {
         echo "Invalid username or pass";
    }


?>

答案 1 :(得分:0)

要添加Eh Ezani所说的内容,您的HTML中存在问题。当我相信你的意思是onsubmit时,你的表单属性会读取提交。可能想尝试类似的东西。

<form method="Post" action="login.php" onsubmit ="return validate()"> 

     User:<br><input type="text" name="user" size="40"><br> 

     Password:<br><input type="password" name="pass" size="40"><br>

     <input id="button" type="submit" name="submit" value="Log-In">

</form> 

此外,“将MySQLi用于较旧的MySQL函数。”i“代表”改进“。改进列表可在the docs中找到。

-credited to

Difference between mysqli and mysql?