Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active January 18, 2026 13:02
Show Gist options
  • Select an option

  • Save daveh/c5a691136c7e3b81dc8e72b3fc1054b3 to your computer and use it in GitHub Desktop.

Select an option

Save daveh/c5a691136c7e3b81dc8e72b3fc1054b3 to your computer and use it in GitHub Desktop.
HTML to MySQL using PHP (code to accompany https://youtu.be/Y9yE98etanU)
<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
<meta charset="UTF-8">
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
</head>
<body>
<h1>Contact</h1>
<form action="process-form.php" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name">
<label for="message">Message</label>
<textarea id="message" name="message"></textarea>
<label for="priority">Priority</label>
<select id="priority" name="priority">
<option value="1">Low</option>
<option value="2" selected>Medium</option>
<option value="3">High</option>
</select>
<fieldset>
<legend>Type</legend>
<label>
<input type="radio" name="type" value="1" checked>
Complaint
</label>
<br>
<label>
<input type="radio" name="type" value="2">
Suggestion
</label>
</fieldset>
<label>
<input type="checkbox" name="terms">
I agree to the terms and conditions
</label>
<br>
<button>Send</button>
</form>
</body>
</html>
<?php
$name = $_POST["name"];
$message = $_POST["message"];
$priority = filter_input(INPUT_POST, "priority", FILTER_VALIDATE_INT);
$type = filter_input(INPUT_POST, "type", FILTER_VALIDATE_INT);
$terms = filter_input(INPUT_POST, "terms", FILTER_VALIDATE_BOOL);
if ( ! $terms) {
die("Terms must be accepted");
}
$host = "localhost";
$dbname = "message_db";
$username = "root";
$password = "";
$conn = mysqli_connect(hostname: $host,
username: $username,
password: $password,
database: $dbname);
if (mysqli_connect_errno()) {
die("Connection error: " . mysqli_connect_error());
}
$sql = "INSERT INTO message (name, body, priority, type)
VALUES (?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if ( ! mysqli_stmt_prepare($stmt, $sql)) {
die(mysqli_error($conn));
}
mysqli_stmt_bind_param($stmt, "ssii",
$name,
$message,
$priority,
$type);
mysqli_stmt_execute($stmt);
echo "Record saved.";
@Arash167

Arash167 commented Jan 3, 2024

Copy link
Copy Markdown

@daveh

daveh commented Jan 4, 2024

Copy link
Copy Markdown
Author

@Arash167 This could be being caused by the space in the folder name you're developing in - try removing it (e.g. "html_learning" instead of "html learning").

If it's not that, try adding this code to your PHP file so that the error detail is shown:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

@Arash167

Arash167 commented Jan 4, 2024

Copy link
Copy Markdown

I've renamed my folder name to "html_learning" (it didn't work).
and I added the php code that you gave me and it still shows the first error

@daveh

daveh commented Jan 5, 2024

Copy link
Copy Markdown
Author

@Arash167 A 500 error means an error is occurring but PHP isn't configured to display that error. Try adding the code to the process_form script if that's where the error is being displayed.

@hackerdiganthjay

Copy link
Copy Markdown

@daveh Hello sir pls help me out with this when I enter the correct code and submit the registration form it shows this:
delete
Pls reply as quick as possible sir

@daveh

daveh commented Feb 5, 2024

Copy link
Copy Markdown
Author

@Que0ta

Que0ta commented Feb 6, 2024

Copy link
Copy Markdown

I am getting this error:
ex2

How can i fix it ? I've checked if "mysqli" is toggled in php.ini and it is okay.

Mb something is wrong with this php code ?

<?php

$name = $_POST["name"];
$message = $_POST["email"]; 

$host = "localhost";
$dbname = "message_ac";
$username = "root";
$password = "";
        
$conn = mysqli_connect(hostname: $host,
                       username: $username,
                       password: $password,
                       database: $dbname);
        
if (mysqli_connect_errno()) {
    die("Connection error: " . mysqli_connect_error());
}           
        
$sql = "INSERT INTO form (name, message)
        VALUES (?, ?)";

$stmt = mysqli_stmt_init($conn);

if ( ! mysqli_stmt_prepare($stmt, $sql)) {
 
    die(mysqli_error($conn));
}

mysqli_stmt_bind_param($stmt, "ss",
                       $name,
                       $message);

mysqli_stmt_execute($stmt);

echo "Record saved.";

@daveh

daveh commented Feb 7, 2024

Copy link
Copy Markdown
Author

@Que0ta It's likely that there's more than one php.ini file being used on your server - check to see if you need to uncomment out the mysqli line in a different php.ini file

@Que0ta

Que0ta commented Feb 7, 2024

Copy link
Copy Markdown

@daveh I've searched, uncomented where it needed to be, and still it shows me the exact error. Can it be something else ?
Btw, I'm using visual studio extension, can this error occur because of this extension, because I use it to serve my project ? =>

https://ibb.co/P1VvqYk

@daveh

daveh commented Feb 7, 2024

Copy link
Copy Markdown
Author

@Que0ta I'm not familiar with that extension but I don't see how it would affect it, unless that is what's running it and it doesn't contain mysqli - you can use phpinfo() to verify which actual php.ini file you need to edit.

@Que0ta

Que0ta commented Feb 12, 2024

Copy link
Copy Markdown

@daveh thank you for help and for your cool videos about php =) I've just reinstalled newer version of php 8.3.2, reinstalled xamp and putted my project folder into ./xampp/htdocs and it worked!

PS. Maybe it will help somebody else with similar problem

@jassperghost

jassperghost commented Feb 27, 2024

Copy link
Copy Markdown

@daveh i am getting this error helppp
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') VALUES (?, ?, ?, ?)' at line 1

@daveh

daveh commented Feb 27, 2024

Copy link
Copy Markdown
Author

@jassperghost There's a syntax error in your SQL - check it matches the code above.

@EricVigues

Copy link
Copy Markdown

Hello, I would like to know if you still have the css

@daveh

daveh commented Apr 1, 2024

Copy link
Copy Markdown
Author

@Oddball3

Oddball3 commented Apr 9, 2024

Copy link
Copy Markdown

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'xxxxx'@'localhost' (using password: YES) in C:\examp\htdocs\process-form.php:21 Stack trace: #0 C:\examp\htdocs\process-form.php(21): mysqli_connect('localhost', 'xxxxx', Object(SensitiveParameterValue), 'message_db') #1 {main} thrown in C:\examp\htdocs\process-form.php on line 21

the above comment error is shown when i;m trying to find my desired output please help llooking at the code below

@daveh

daveh commented Apr 9, 2024

Copy link
Copy Markdown
Author

@Oddball3 This is because the user and password you're using in your code to access the database are incorrect. Try using another one, or even the root account (username "root") and blank password ("").

@LOL-monkey

Copy link
Copy Markdown

屏幕截图 2024-04-27 175845
Why i will have this error?
屏幕截图 2024-04-27 180013
this is my PHP code

@daveh

daveh commented Apr 27, 2024

Copy link
Copy Markdown
Author

@LOL-monkey You're missing the mysqli_connect line, it should go at line 21 in your code:

$conn = mysqli_connect(hostname: $host,
                       username: $username,
                       password: $password,
                       database: $dbname);

@LOL-monkey

Copy link
Copy Markdown

but it did not get fix
屏幕截图 2024-04-28 003047
my new code is
屏幕截图 2024-04-28 003202

@daveh

daveh commented Apr 28, 2024

Copy link
Copy Markdown
Author

@LOL-monkey The error message refers to a variable $conn not existing on line 30, but there is no such variable on line 30 of your code - could there be multiple versions of your script perhaps or it wasn't saved when you tried to run it?

@LOL-monkey

Copy link
Copy Markdown

i have already save it when i run it

@delt0m

delt0m commented May 10, 2024

Copy link
Copy Markdown

Hey Dave,

Is there any way I can have the database table on another HTML page?

if there are any YouTube videos related to this, could you provide the links?

@daveh

daveh commented May 11, 2024

Copy link
Copy Markdown
Author

@delt0m Something like this do you mean?

@zoro7sn

zoro7sn commented Jun 21, 2024

Copy link
Copy Markdown

can u tell me what wrong pls

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 2 in C:\xampp\htdocs\THE-BEST\aaa.php:31 Stack trace: #0 C:\xampp\htdocs\THE-BEST\aaa.php(31): mysqli_stmt_prepare(Object(mysqli_stmt), 'INSERT INTO inf...') #1 {main} thrown in C:\xampp\htdocs\THE-BEST\aaa.php on line 31

dsdsdsdsdsdsdsdsdsdsddsds5608
sdsdsdsdsdsdsdsdsds22222222222024-06-21 205651

@daveh

daveh commented Jun 22, 2024

Copy link
Copy Markdown
Author

@zoro7sn There's an extra comma in your SQL after the values list - it should be VALUES (?, ?, ?, ?)

@zoro7sn

zoro7sn commented Jun 22, 2024

Copy link
Copy Markdown

thank u so match it worked but can i ask u about something else pls

how can i remove the data that i sent from the html to the database
يبيبيبيبيببييييييييييييييييييييي

@daveh

daveh commented Jun 22, 2024

Copy link
Copy Markdown
Author

@zoro7sn It looks like you don't have a primary key in your table, which makes it difficult to uniquely identify individual rows. There's an article here that might help.

@paulagliano

Copy link
Copy Markdown

Here is my php code. I get the following error when I hit submit.

Parse error: syntax error, unexpected identifier "sisis" in C;\xampp\htdocs\SAR\process-timesheet.php on line 32

1 <?Php
2
3 $member = $_POST["member"];
4 $date = $_POST["date"];
5 $type = $_POST["type"];
6 $hours = $_POST["hours"];
7 $note = $_POST["note"];
8
9 $host = "localhost";
10 $dbname = "lcsar_db";
11 $username = "root";
12 $password = "";
13
14 $conn = mysqli_connect(hostname:$host,
15 username: $username,
16 password: $password,
17 database: $dbname);
18
19 if (mysqli_connect_errno()) {
20 die("Connection error: " . mysqli_connect_error());
21 }
22
23 $sql = "INSERT INTO timesheet (member, date, type, hours, note);
24 VALUES (?, ?, ?, ?, ?);
25
26 $stmt = mysqli_stmt_init($conn);
27
28 if ( ! mysqli_stmt_prepare($stmt, $sql)) {
29 die(mysqli_error($conn));
30 }
31
32 mysqli_stmt_bind_param($stmt, "sisis", $member, $date, $type, $hours, $note);
33
34 mysqli_stmt_execute($stmt);
35
36 echo "Record saved";

@daveh

daveh commented Jun 13, 2025

Copy link
Copy Markdown
Author

@paulagliano I can't see anything wrong with your code - the error says it's on line 32, perhaps with the second argument to the function there - try using single quotes around the "sisis" string to see if that makes a difference: 'sisis'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment