I'm working on an assignment for a PHP beginner course.
I need to create a simple guestbook with three php files: one for the input and processing, one for showing data, and one for the database connection.
with some help from this community, I've managed to get many of the features working.
Now, I want to implement an error message when the name or input field is left empty. I added this code at the very beginning:
if (isset($_POST['submit']) &&
$_POST['submit'] == 'Submit')
{
if(!isset($_POST['name']) ||
$_POST['name'] == ""
{
$msg = '<P>There is a problem. Did you enter a name? </P>';
echo $msg;
} elseif
(!isset($_POST['message']) ||
$_POST['message'] == ""
{
$msg = '<P>There is a problem. Did you enter a message? </P>';
echo $msg;
}else{
Which gives me this error:
Parse error: syntax error, unexpected ';' in /Applications/MAMP/htdocs/051R4.php on line 10
Can someone point me in the right direction?
here's the full code:
vars.inc
<?php
$hostname = "localhost";
$username = (blanked)
$password = (blanked)
$database = "dbLOI";
$table = "guestbook";
?>
file 1. 051R4.php
<?php
include 'vars2.inc';
if (isset($_POST['submit']) &&
$_POST['submit'] == 'Submit')
{
if(!isset($_POST['name']) ||
$_POST['name'] == ""
{
$msg = '<P>There is a problem. Did you enter a name? </P>';
echo $msg;
} elseif
(!isset($_POST['message']) ||
$_POST['message'] == ""
{
$msg = '<P>There is a problem. Did you enter a message? </P>';
echo $msg;
}else{
$link = mysqli_connect($hostname, $username, $password, $database);
if (!$link)
die ('Could not connect: ' . mysqli_connect_error());
$name = mysqli_real_escape_string($link, $_POST["name"]);
$message = mysqli_real_escape_string($link, $_POST["message"]);
$date = date("y-m-d h:i:s"); //date time
$sport = mysqli_real_escape_string($link, $_POST["sport"]);
if(isset($_POST['submit'])) {
//Radio button has been set to "true"
if(isset($_POST['practitioner']) && $_POST['practitioner'] == 'true') $_POST['practitioner'] = TRUE;
//Radio button has been set to "false" or a value was not selected
else $_POST['practitioner'] = FALSE;
}
$practitioner = mysqli_real_escape_string($link, $_POST['practitioner']);
$query="INSERT INTO Guestbook(name, message, date, sport, practitioner)VALUES('$name', '$message', '$date', '$sport', $practitioner)";
$result=mysqli_query($link, $query);
//check if query successful
if($result){
echo "Successful";
echo "<BR>";
// link to view guestbook page
echo "<a href='051R4.view.php'>View guestbook</a>";
}
else {
echo "ERROR";
}
mysqli_close($link);
require "051R4.view.php";
require "051R4.sign.php";
?>
file 2. 051R4.view.php
<!DOCTYPE html>
<html lang=nl>
<head>
<meta charset=utf-8>
<meta name=description content="Guestbook">
<meta name=keywords content="Guestbook, LOI">
<title>Inzendopdracht 051R4</title>
</head>
<body>
<table>
<tr>
<td><strong>View Guestbook | <a href="051R4.sign.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>
<?php
include 'vars2.inc';
$link = mysqli_connect($hostname, $username, $password, $database);
if (!$link)
die ('Could not connect: ' . mysqli_connect_error());
$tbl_name="Guestbook"; // Table name
$sql="SELECT * FROM $tbl_name ORDER BY ID desc";
$result=mysqli_query($link, $sql);
while($rows=mysqli_fetch_array($result)){
?>
<table>
<tr>
<td><table>
<tr>
<td>Name</td>
<td>:</td>
<td><? echo $rows['Name']; ?></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><? echo $rows['Message']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['Date']; ?></td>
</tr>
</table></td>
</tr>
</table>
<?php
}
mysqli_close($link); //close database
?>
</body>
</html>
file 3. 051R4.sign.php
<!DOCTYPE html>
<html lang=nl>
<head>
<meta charset=utf-8>
<meta name=description content="Guestbook">
<meta name=keywords content="Guestbook, LOI">
<title>Inzendopdracht 051R4</title>
</head>
<body>
<table>
<tr>
<td><strong>Sign Guestbook </strong></td>
</tr>
</table>
<table>
<tr>
<form id="form1" name="form1" method="post" action="051R4.php">
<td>
<table>
<tr>
<td>Name</td>
<td>:</td>
<td ><input name="name" type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><textarea name="message" cols="40" rows="3" id="message"></textarea></td>
</tr>
<tr>
<td>Sport</td>
<td>:</td>
<td><select name="sport" id="sport">
<option value=tennis>tennis</option>
<option value=voetbal>voetbal</option>
<option value=running>running</option>
<option value=tafeltennis>tafeltennis</option>
<option value=squash>squash</option>
<option value=wielrennen>wielrennen</option>
<option value=boksen>boksen</option>
</select></td>
</tr>
<tr>
<td>Practitioner</td>
<td>:</td>
<td><input type="radio" name="practitioner" value=true id=practitioner>yes<br>
<input type="radio" name="practitioner" value=false checked id=practitioner>no<br>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table>
<tr>
<td><strong><a href="051R4.view.php">View Guestbook</a> </strong></td>
</tr>
</table>
</body>
</html>
edit: It seems like the main issue at the moment is somewhere in the $practitioner variable.
edit 2:
I made a few edits to the radio button and I've added this code:
if(isset($_POST['submit'])) {
//Radio button has been set to "true"
if(isset($_POST['Practitioner']) && $_POST['Practitioner'] == 'true') $_POST['Practitioner'] = TRUE;
//Radio button has been set to "false" or a value was not selected
else $_POST['Practitioner'] = FALSE;
}
I thought this would convert the Practitioner radio button value to a boolean, but I can't seem to get rid of this error:
PHP Notice: Undefined index: Practitioner in /Applications/MAMP/htdocs/051R4.php on line 21
edit 3: The radio button should be working now, there are no more on-screen or log file errors (except the ERROR that indicates there is no $result), still no guestbook entry is being created.
edit 4: new question about error handling. I posted it right at the top.