Populate HTML loaded form from PHP/MySQL - Stack Overflow
Figuring out how to send my HTML page form data to mySQL database table
How to make a PHP/HTML form with MySQL? - Stack Overflow
Taking mySQL database input from HTML form with PHP - Stack Overflow
I am new to coding as I wish to create a website idea I have had for a few years. I have written the HTML and CSS code for the frontend and created a database and datatable on mySQL, but I am stuck on understanding the process to move the data from my HTML form to mySQL database table.
I was intending to use python, and have added the below line within my HTML form, as well as a button to send the data:
<form action="getdata.py" method="post">
Could someone please explain the procedure needed in order to put this forms data into mySQL database?
I do not understand flask/django and their involvement with python. My understanding going into this was that I should write a script on vscode in python that retreives the forms data, and somehow then send this to the my database table. Could someone please clarify these next steps?
If i understood good what you are asking, the HTML and the PHP can be on the same page.
Take in mind that the better practice is to put the processnig code in the top of the file.
<?php
if(isset($_POST['button'])){
$mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");
if (mysqli_connect_errno()) {
printf("Problem with connection: %s\n", mysqli_connect_error());
exit();
}
else {
$var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
$var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
$var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
$var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
$var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
$sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
$res = mysqli_query($mysqli, $sql);
if ($res === TRUE) {
echo "User added.";
exit();
}
else {
printf("Error: %s\n", mysqli_error($mysqli));
}
}
mysqli_close($mysqli);
}
?>
<html>
<head></head>
<body>
<form id="form" name="form" action="" method="POST">
<label id="lbluser">Name:</label>
<input type="text" name="name" id="name" /><br/>
<label id="lbllastaname">Lastname:</label>
<input type="text" name="lstname" id="lstname" /><br/>
<label id="lblmail">E-mail:</label>
<input type="text" name="email" id="email" /><br/>
<label id="lblpassword">Password:</label>
<input type="password" name="password" id="password" /><br/>
<label id="lblpassword">Repeat password:</label>
<input type="password" name="passwordr" id="passwordr" /><br/>
<button type="submit" name="button" value="insert">OK</button>
</form>
</body>
</html>
After i understand that you are looking for a HTML and PHP code both in the same file and Answer from Bob0t. The only thing which you might need to change here is
isset($_POST['button']) to isset($_POST['BtnSubmit']) and change the <button type=”submit” name=”button” value=”insert”> to <button type=”submit” value=”BtnSubmit”>
<?php
if(isset($_POST['BtnSubmit'])){
$mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");
if (mysqli_connect_errno()) {
printf("Problem with connection: %s\n", mysqli_connect_error());
exit();
}
else {
$var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
$var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
$var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
$var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
$var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
$sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
$res = mysqli_query($mysqli, $sql);
if ($res === TRUE) {
echo "User added.";
exit();
}
else {
printf("Error: %s\n", mysqli_error($mysqli));
}
}
mysqli_close($mysqli);
}
?>
<html>
<head></head>
<body>
<form id="form" name="form" action="" method="POST">
<label id="lbluser">Name:</label>
<input type="text" name="name" id="name" /><br/>
<label id="lbllastaname">Lastname:</label>
<input type="text" name="lstname" id="lstname" /><br/>
<label id="lblmail">E-mail:</label>
<input type="text" name="email" id="email" /><br/>
<label id="lblpassword">Password:</label>
<input type="password" name="password" id="password" /><br/>
<label id="lblpassword">Repeat password:</label>
<input type="password" name="passwordr" id="passwordr" /><br/>
<button type=”submit” value=”BtnSubmit”>OK</button>
</form>
</body>
</html>
You are getting blank options AFTER each option with an expected value because you have failed to write a closing option tag. / needs to be written into the second option tag like this:
while ($row = mysqli_fetch_array($result)) {
echo "<option>{$row['CourseID']}</option>";
}
The option tags still render even if you don't properly close them. In this case, the error presents itself by generating twice the desired tags.
I recommend that you use MYSQLI_ASSOC as the second parameter of your mysqli_fetch_array call or more conveniently: mysqli_fetch_assoc
In fact, because $result is iterable, you can write:
foreach ($result as $row) {
echo "<option>{$row['CourseID']}</option>";
}
About using extract($_POST)...
I have never once found a good reason to use extract in one of my scripts. Not once. Furthermore, the php manual has a specific Warning stating:
Warning Do not use extract() on untrusted data, like user input (e.g. $_GET, $_FILES).
There are more warning down the page, but you effectly baked insecurity into your code by calling extract on user supplied data. DON'T EVER DO THIS, THERE IS NO GOOD REASON TO DO IT.
Here is a decent page that speaks about accessing submitted data: PHP Pass variable to next page
Specifically, this is how you access the expected superglobal data:
$name = $_POST['name'];
$testsentence = $_POST['testsentence'];
$courseid = $_POST['course'];
You must never write unfiltered, unsanitized user supplied data directly into your mysql query, it leads to query instability at best and insecurity at worst.
You must use a prepared statement with placeholders and bound variables on your INSERT query. There are thousands of examples of how to do this process on Stackoverflow, please research until it makes sense -- don't tell yourself that you'll do it layer.
Make sure you added extract($_POST) (or something similar) in your PHP code!
You need to extract the parameters from your POST request before using them, otherwise your $name, $testsentence, and $courseid will be undefined.