0

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>&nbsp;</td>
      <td>&nbsp;</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.

Turbo
  • 13
  • 3
  • You say "no more errors appear on screen", do any errors appear in the PHP error logs? – kojow7 Mar 13 '17 at 23:05
  • just the one: [13-Mar-2017 23:50:48 Europe/Berlin] PHP Notice: Undefined index: practitioner in /Applications/MAMP/htdocs/051R4.php on line 13 – Turbo Mar 13 '17 at 23:06
  • Part of your issue may be in understanding HTML forms and the PHP (server-side) validation of them. Since the action of your HTML form is "051R4.php" this is where you need to check to see whether the value of $_POST['practitioner'] has been set or not. – kojow7 Mar 13 '17 at 23:15
  • There is also no such thing as an "EOFORMPAGE construction". A Google search suggests this is just a variable name that a textbook is using for HEREDOC syntax. There are a few different ways to intermix HTML and PHP code, the method you are using of separation is just fine. HEREDOC syntax is an alternative that would also work, but not necessary in your case. – kojow7 Mar 13 '17 at 23:18
  • thank you kojow7, I think I'm getting closer, could you check my latest edit? – Turbo Mar 13 '17 at 23:52
  • Your `practitioner` fields are named `radio` they should be `practitioner`, `radio` is the `type`. Also, PHP associative arrays are case sensitive. – imtheman Mar 13 '17 at 23:57
  • thank you! that fixes the 'undefined index' problem. unfortunately, I still can't make a guestbook entry, even though there are no more on screen errors nor are there any messages in the log file anymore. – Turbo Mar 14 '17 at 00:21
  • I finally got it working! Turns out there was a typo in the original SQL statement I used to create the table. Will start working on the 'view' page tomorrow. thanks kojow7 and imtheman. – Turbo Mar 14 '17 at 02:52

0 Answers0