[英]When I click submit on my php form the landing page comes up blank and it does not submit date to the database
[英]I am attempting to make a login using phpmyadmin, but, when I click submit, it inserts blank entries into the database
我的課正在嘗試制作自己的游戲。.但是,我們無法將提交頁面發送到PhpMyAdmin中的數據庫。 當您單擊提交時,它將向數據庫發送空白條目,就像您沒有填寫任何空白一樣。 有人可以解決這個問題嗎? 謝謝!!
我的index.php頁面。
<html>
<head>
<meta charset="UTF-8">
<title> Register New Account </title>
<link rel="stylesheet" type="text/css" href="td.css">
</head>
<body>
<?php
/* $count=$count+1;
echo " count " . $count; */
if($_POST['submit_id'] == 1)
{
/* echo "testing"; */
if($_POST['Username'] == NULL)
{
$message = 'Please enter your Username.';
}
if($_POST['Email'] == NULL)
{
$message = 'Please enter your Email.';
}
if($_POST['Confirm'] == NULL)
{
$message = 'Please re-enter your Email.';
}
if($_POST['Password'] == NULL)
{
$message = 'Please enter your Password.';
}
if($_POST['Email'] != $_POST['Confirm'])
{
$message = 'Your emails did not match, Please enter your emails again.';
}
}
if( $message == NULL )
{
// if there is no error, test to see if there is already an account by the player_name
$MySQLlink = new mysqli("localhost", "root", "******", "Tower_Defense");
// check connection - take out later
if ( !$MySQLlink )
{
printf( "Could not connect to MySQL server : %s", mysqli_connect_error() );
exit();
}
else
{
printf( "Connected to the MySQL server" );
echo "<br>";
}
$result = mysqli_query( $MySQLlink, "SELECT * FROM Users WHERE ( email = 'email' ) " );
if($row = mysqli_fetch_array($result))
{
$message = "There is an account with that email address already. Please choose another email account";
}
mysqli_free_result($result);
$result = mysqli_query( $MySQLlink, "SELECT * FROM Users WHERE ( Username = '$Username' ) " );
if( $row = mysqli_fetch_array($result) && $message == NULL )
{
$message = "There is an account by that player name already. Please choose another Login name";
mysqli_free_result($result);
}
else
{
//echo "next date <br>";
// create account
$Username = ($_POST['Username']);
$Password = ($_POST['Password']);
$Email = ($_POST['Email']);
$email = ($_POST['email']);
//echo "Next one<br>";
$TableList = " `Username`, `Password`, `Email`, `Confirm` ";
$Values = " '$Username', '$Password', '$Email', '$Confirm' ";
if($message != NULL)
{
echo "$message";
}
?>
<div id="container" >
<div id="header">
<h1 id="h1">Besco's Biscuits</h1>
<a href= "http://192.168.131.**/towerdefence/about/index.html" id="a1">About</a>
<a href="http://192.168.131.**/towerdefence/instruction/index.html" id="a2">Instructions</a>
<a href="http://192.168.131.**/towerdefence/create/index.html" id="a3"> The Creation Of The Game</a>
<a href="http://192.168.131.**/towerdefence/cu/index.html" id="a4">Contact Us</a>
</div>
<br /> <br /> <br />
<table align = "center">
<tr>
<td>
Welcome to <b> Besco's Biscuits </b>. Please fill out the following <br />
areas and we will begin your adventure soon. :)
</td>
</tr>
</table>
<br /> <br /> <br /> <br /> <br />
<table align = "center">
<tr>
<td>
<form action = "<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> <br />
Username: <input type="text" name="Username" id= "Username"> <br />
Email: <input type = "text" name = "Email" id= "Email"> <br />
Confirm: <input type = "text" name = "Confirm" id= "Confirm"> <br />
Password: <input type = "password" name = "Password" id = "Password"> <br />
<input type = "submit" value = "Register" id="submit_id" value = "1">
<input type = "reset" name="Reset" value="Check if Available!" class = "account">
</form>
</td>
</tr>
</table>
</body>
</html>
我的insert.php頁面
<html>
<body>
<?php
$Username = $_POST['name'];
$con=mysqli_connect("localhost", "root", "******", "Tower_Defense");
//Check Connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Users (Username, Email, Confirm, Password)
VALUES
('$_POST[Username]','$_POST[Email]',' $_POST[Confirm]',' $_POST[Password]')";
if (!mysqli_query($con,$sql))
{
die ('Error: ' . mysqli_error($con));
}
else
{
echo "1 record added";
echo $_POST[Username];
//echo "Where is Username?";
echo $_POST[Email];
//echo "Where is Email?";
echo $_POST[Confirm];
//echo "Where is Confirm";
echo $_POST[Password];
//echo "Where is Password";
}
mysqli_close($con);
?>
</body>
更新:
我添加了有人建議將支票移至insert.php的更改,現在電子郵件和確認電子郵件支票不起作用。 有人可以幫忙嗎?
index.php
<html>
<body>
<div id="container" >
<div id="header">
<h1 id="h1">Besco's Biscuits</h1>
<a href= "http://192.168.131.34/towerdefence/about/index.html" id="a1">About</a>
<a href="http://192.168.131.34/towerdefence/instruction/index.html" id="a2">Instructions</a>
<a href="http://192.168.131.34/towerdefence/create/index.html" id="a3"> The Creation Of The Game</a>
<a href="http://192.168.131.34/towerdefence/cu/index.html" id="a4">Contact Us</a>
</div>
<br /> <br /> <br />
<table align = "center">
<tr>
<td>
Welcome to <b> Besco's Biscuits </b>. Please fill out the following <br />
areas and we will begin your adventure soon. :)
</td>
</tr>
</table>
<br /> <br /> <br /> <br /> <br />
<table align = "center">
<tr>
<td>
<form action = "insert.php" method = "post"> <br />
Username: <input type="text" name="Username" id= "Username" required = "1"> <br />
Email: <input type = "text" name = "Email" id= "Email" required = "1"> <br />
Confirm: <input type = "text" name = "Confirm" id= "Confirm" required = "1"> <br />
Password: <input type = "password" name = "Password" id = "Password" required = "1"> <br />
<input type = "submit" value = "Register" id="submit_id" value = "1">
<input type = "reset" name="Reset" value="Reset Page" class = "account">
</form>
</td>
</tr>
</table>
</body>
</html>
insert.php
<html>
<body>
<?php
if($_POST['submit_id'] == 1)
{
echo "testing";
if($_POST['Email'] != $_POST['Confirm'])
{
$message = 'Your emails did not match, Please enter your emails again.';
}
}
if( $message == NULL )
{
// if there is no error, test to see if there is already an account by the player_name
$MySQLlink = new mysqli("localhost", "root", "abc123", "tower_defense");
// check connection - take out later
if ( !$MySQLlink )
{
printf( "Could not connect to MySQL server : %s", mysqli_connect_error() );
exit();
}
else
{
printf( "Connected to the MySQL server" );
echo "<br>";
}
$result = mysqli_query( $MySQLlink, "SELECT * FROM Users WHERE ( email = 'email' ) " );
if($row = mysqli_fetch_array($result))
{
$message = "There is an account with that email address already. Please choose another email account";
}
mysqli_free_result($result);
$result = mysqli_query( $MySQLlink, "SELECT * FROM Users WHERE ( Username = '$Username' ) " );
if( $row = mysqli_fetch_array($result) && $message == NULL )
{
$message = "There is an account by that player name already. Please choose another Login name";
mysqli_free_result($result);
}
else
{
//echo "next date <br>";
// create account
$Username = ($_POST['Username']);
$Password = ($_POST['Password']);
$Email = ($_POST['Email']);
$email = ($_POST['email']);
//echo "Next one<br>";
}
}
if($message != NULL)
{
echo "$message";
}
$con=mysqli_connect("localhost", "root", "abc123", "tower_defense");
//Check Connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Users (Username, Email, Confirm, Password)
VALUES
('$_POST[Username]','$_POST[Email]',' $_POST[Confirm]',' $_POST[Password]')";
if (!mysqli_query($con,$sql))
{
die ('Error: ' . mysqli_error($con));
}
else
{
echo "1 record added";
echo $_POST[Username];
//echo "Where is Username?";
echo $_POST[Email];
//echo "Where is Email?";
echo $_POST[Confirm];
//echo "Where is Confirm";
echo $_POST[Password];
//echo "Where is Password";
}
mysqli_close($con);
?>
</body>
</html>
我在這里看到兩個主要問題-
首先 ,表單的動作指向自身。 這意味着$ _POST數組提交給index.php,而您的insert.php頁面無權訪問該信息 。 Index.php運行驗證檢查,如果一切檢查完畢,它將為變量分配$ _POST值並退出。 那就是數據死亡的地方。 沒有將信息獲取到文件insert.php的方法。 因此,如果您在瀏覽器中手動打開文件insert.php,則$ _POST數組將為空,並且只會插入空格。
有幾種解決方法。 最簡單,最快捷的方法是單頁解決方案-將insert.php代碼移到最后else塊內的index.php文件中。
else {
//echo "next date <br>";
// create account
$Username = $_POST['name'];
//etc.. code to insert data from insert.php
另一種解決方案是將所有驗證代碼移至insert.php,在該頁面上顯示任何表單錯誤,並在驗證失敗時使用戶返回頁面。 在這種情況下,您可以將表單的操作更改為insert.php:
<form action="insert.php" method="post">
這種方法不太用戶友好,也不是理想的解決方案。 確實更好的做法是使用Javascript進行表單驗證,使用PHP進行表單處理。 那可能超出了你的課程范圍...
其次 ,此代碼可廣泛用於SQL注入。 您需要使用參數化查詢,而不是直接將變量放入SQL語句中。 看一下關於如何使用mysqli參數化查詢的SO問題 。
我發現的錯誤:
首先,您的代碼首先將從表單接收的值提交到index.php本身,因此不會因為插入查詢未運行而首先獲取插入值。
在index.php中,檢查查詢以選擇電子郵件和用戶名。 運行查詢時,變量沒有任何值,因為在查詢之后,這些值會轉移到幾行(在您有$ email = $ _POST ['Email']的行)。 此外,您在與電子郵件相關的查詢中錯過了$號。
即將插入insert.php時,您會在插入查詢viz中的全局變量$ _POST []中缺少引號。 $ _POST ['email']。
檢查這些錯誤,讓我知道它是否有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.