Click here to Skip to main content
15,886,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
WOULD JUST LIKE TO START OFF THAT I AM USING PLESK AND MYSQL AND THESE ARE THE SET PLATFORMS I HAVE TO USE FOR THIS PROJECT

I have double checked form names, everything is correct yet I am still getting the error messages. This is my PHP code:

<?php
include 'db.inc.php';
echo "The details sent down are: <br>";
echo "Title : " . $_POST['title'] . "<br>";
echo "Provider : " . $_POST['provider'] . "<br>";
echo "Description : " . $_POST['desc'] . "<br>";
echo "Fee : " . $_POST['fee'] . "<br>";
echo "Venue : " . $_POST['ven'] . "<br>";
echo "Available Places : " . $_POST['avPlaces'] . "<br>";
echo "Remaining Places : " . $_POST['rePlaces'] . "<br>";
echo "Start Date : " . $_POST['date'] . "<br>";
echo "Days : " . $_POST['days'] . "<br>";
echo "Start Time : " . $_POST['start'] . "<br>";
echo "End Time :  " . $_POST['end'] . "<br>";

$sql = "Insert into courses (Title, Provider, Description,  Fee, Venue, AvPlaces, RePlaces, StartD, Days, StartT, EndT) Values ('$_POST[title]','$_POST[provider]','$_POST[desc]','$_POST[fee]','$_POST[ven]','$_POST[avPlaces]','$_POST[rePlaces]',$_POST[date]','$_POST[days]','$_POST[start]','$_POST[end]')";
if (!mysqli_query($con, $sql))
{
	die ("An error in the sql query: " . mysqli_error($con));
}

echo "<br>A record has been added for " . $_POST['Title'] . " " ;
header("refesh:3; url=addCourse.html");

mysqli_close($con);
?>
``` 


and this is my HTML code accompanying it:
```
<div class="contents">
		<form action="addCourse.php" method="post">
			<label for="title" class= "label" >Title:</label><br><br>
			<input type="text" class="field" id="title" name="title"><br><br>
			<label for="provider" class= "label" >Provider:</label><br><br>
			<input type="text" class= "field" id="provider" name="provider"><br><br>
			<label for="desc" class= "label" >Description:</label><br><br>
			<input type="text" class= "field" id="desc" name="desc"><br><br>
			<label for="fee" class= "label" >Fee:</label><br><br>
			<input type="number" class= "field" id="fee" name="fee"><br><br>
			<label for="ven" class= "label" >Venue:</label><br><br>
			<input type="text" class= "field" id="ven" name="ven"><br><br>
			<label for="avPlaces" class ="label">Available Places:</label><br><br>
			<input type="number" class= "field" id="avPlaces" name="avPlaces"><br><br>
			<label for="rePlaces" class="label">Remaining Places:</label><br><br>
			<input type="number" class="field" id="rePlaces" name="rePlaces"><br><br>
			<label for="date" class="label">Start Date:</label><br><br>
			<input type="date" class= "field" id="date" name="date"><br><br>
			<label for="days" class= "label">Days:</label><br><br>
			<input type="text" class= "field" id="day" name="day"><br><br>
			<label for="start" class= "label">Start Time:</label><br><br>
			<input type="time" class= "field" id="start" name="start"><br><br>
			<label for="end" class= "label">End Time:</label><br><br>
			<input type="time" class= "field" id="end" name="end"><br><br>
			<input type="submit" id ="add" name="add"><br><br>
			<input type="reset" id = "reset" name="reset"><br><br>
			</form>
			</div>


What I have tried:

I have tried as much as I can figure for now
Posted
Updated 26-Aug-21 6:16am
Comments
Richard MacCutchan 26-Aug-21 11:36am    
What is the complete text of the error message? Also have you checked the ouptut of all the echo statements?
Megan Crean 26-Aug-21 11:43am    
I have ran my addCourse.html page to see what would happen and this was the output:
The details sent down are:
Title : Test
Provider : Test
Description : Test
Fee : 000
Venue : Test
Available Places : 00
Remaining Places : 00
Start Date : 2021-07-20
Notice: Undefined index: days in /var/www/vhosts/C00249687.candept.com/httpdocs/Summer Project 2021/addCourse.php on line 12 Days :
Start Time : 00:00
End Time : 00:00
Notice: Undefined index: days in /var/www/vhosts/C00249687.candept.com/httpdocs/Summer Project 2021/addCourse.php on line 16 An error in the sql query: 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 '','','00:00','00:00')' at line 1


I checked my database to see if anything went in and no data is stored there.
Richard Deeming 26-Aug-21 11:45am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

You have a spelling mistake: in the PHP code you are using 'days' ...
PHP
echo "Days : " . $_POST['days'] . "<br>";


... but in the HTML you are using 'day'
HTML
<label for="days" class= "label">Days:</label><br><br>
<input type="text" class= "field" id="day" name="day"><br><br>
 
Share this answer
 
Comments
Megan Crean 26-Aug-21 11:55am    
Hey, thank you for this. It resolved the Undefined Index for the days section I appreciate that. However I still have the An error in the sql query: 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 '','Test','00:00','00:00')' at line 1" error
Richard MacCutchan 26-Aug-21 12:05pm    
Check your database schema to see what type is expected in the start time and end time fields. You should also be using a DateTime type for the start date, not a string.
PHP
$sql = "Insert into courses (Title, Provider, Description,  Fee, Venue, AvPlaces, RePlaces, StartD, Days, StartT, EndT) Values ('$_POST[title]','$_POST[provider]','$_POST[desc]','$_POST[fee]','$_POST[ven]','$_POST[avPlaces]','$_POST[rePlaces]',$_POST[date]','$_POST[days]','$_POST[start]','$_POST[end]')";

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900