[英]Data not saving when submit is press having ( Notice: Undefined index: firstname in C:\xampp\htdocs\C.W\register.php on line 18
enter code here
<?php
//declare the basic variables
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "coursework";
//create connection
$link = new mysqli($servername, $username, $password, $dbname);
if($link->connect_error){
die ("connection failed: " . $link->connect_error);
}
$sql = "INSERT INTO user (firstname, lastname, email, phn_number, password) VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['email']."', '".$_POST['phn_number']."', '".$_POST['password']."')";
if (mysqli_query($link, $sql)){
echo "Signed up Successfully";
}else{
echo "Error: Sign up Unsuccessfull $sql . " . mysqli_error($link);
}
//close db
$link -> close();
?>
this is my PHP file code.这是我的 PHP 文件代码。 Every time I run it creats a new user but the data entered by the user doesn't save in the database.每次运行它都会创建一个新用户,但用户输入的数据不会保存在数据库中。 and an error shows up并出现错误
Notice: Undefined index: firstname in C:\xampp\htdocs\C.W\register.php on line 18
Notice: Undefined index: lastname in C:\xampp\htdocs\C.W\register.php on line 19
Notice: Undefined index: email in C:\xampp\htdocs\C.W\register.php on line 20
Notice: Undefined index: phn_number in C:\xampp\htdocs\C.W\register.php on line 21
Notice: Undefined index: password in C:\xampp\htdocs\C.W\register.php on line 22
help!!!!帮助!!!!
update your code with this one:用这个更新你的代码:
if($link->connect_error){
die ("connection failed: " . $link->connect_error);
}
$sql ="INSERT INTO user (firstname, lastname, email, phn_number, password) VALUES ('Dinanath', 'Thakur', 'dina@example.com', 123456789, 'xyz')";
your html form should be in the format, update your html form:您的 html 表单应采用以下格式,更新您的 html 表单:
<p>
<label for="firstname"> First Name: </label>
<input type="text" name="firstname" id="firstname"> <br/> <br/>
<label for="lastname"> Last Name: </label>
<input type="text" name="lastname" id="lastname"> <br/> <br/>
<label for="email"> E-mail: </label>
<input type="text" name="email" id="email"> <br/> <br/>
<label for="phn_number"> Phone Number: </label>
<input type="text" name="phn_number" id="phn_number"> <br/> <br/>
<label for="password"> Password: </label>
<input type="text" name="password" id="password">
</p>
<input type="submit" value="Sign Up">
You might forgot to include this:你可能忘了包括这个:
form action="name_of_your_php_file.php" method="POST"表单动作=“name_of_your_php_file.php”方法=“POST”
Inserting this to the code you provided above gives us:将此插入到您上面提供的代码中,我们得到:
<form action="name_of_your_php_file.php" method="POST">
<p> <label for "firstname"> First Name: </label>
<input type="text" name"firstname" id="firstname"> <br/ > <br/ >
<label for "lastname"> Last Name: </label> <input type="text" name"lastname" id="lastname"> <br/ > <br/ >
<label for "email"> E-mail: </label> <input type="text" name"email" id="email"> <br/ > <br/ >
<label for "phn_number"> Phone Number: </label> <input type="text" name"phn_number" id="phn_number"> <br/ > <br/ > <label for "password"> Password: </label>
<input type="text" name"password" id="password"> </p>
<input type="submit" value="Sign Up"></form>
if($link->connect_error){
die ("connection failed: " . $link->connect_error);
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$pno = $_POST['pno'];
$pass = $_POST['pass'];
$sql ="INSERT INTO user (firstname, lastname, email, phn_number, password) VALUES ('$fname', '$lname', '$email', '$pno', '$pass')";
$res = mysql_query($sql);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.