I am using WAMP to try and learn a little PHP and SQL. I'm trying to take user input from a very basic table here:
<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e-mail" name="e-mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="Password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" />
</div>
</fieldset>
</form>
and I want to be able to take the input and post to a database. I've been trying to make this happen with this code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ($_POST[name], $_POST[e-mail], $_POST[password])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
var_dump('name', 'e-mail', 'password');
?>
When I try and insert the "" as in $_POST["name"] I get an error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16
When I try to remove the "" I get this error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16
I also tried to set the variables in the top of the code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$name=$_POST['name']
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ('name','email', 'password');
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
var_dump('name', 'e-mail', 'password');
?>
This way I ended up with an error message saying:
( ! ) Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in C:\wamp\www\input.php on line 9
I was able to echo the name in another script using the $_POST, I am not sure why it will not work with the SQL command. If anyone would help out, and/or give me some resources to learn/study from as well I would appreciate it!
You have a missing ';' after: $name=$_POST['name']
Hi Please note some points
1) You will get warnings of undefined index because you are not checking about values posted or not. We Should use isset()
before using values.
2) Second try to use lowercase
whenever you are giving any name to any tag in php to be sure that no error exist due to typing in lowercase
or uppercase
.
3) Try to use underscore
if your word is long so use e_mail
;
So you can use this php
code
<?php
if(isset($_POST['register'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$email = $_POST['e_mail'];
$password = $_POST['password'];
$sql = "INSERT INTO MyGuests(name, email, password) VALUES('$name','$email','$password')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
}
?>
I have added name to your submit
button also changed e-mail
to e_mail
and name="Password"
to name="password"
<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e_mail" name="e_mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" name="register" />
</div>
</fieldset>
</form>