Search code examples
phpmysqlrelational-databaserecordupdating

PHP & MySQL, a php form to Insert to 1 table while having from 2 coulmns from 2 different tables that are foreign keys


I am new to PHP, using DreamWeaver.

However, I'm looking for someone to help me finish this.

I have 3 tables in MYSQL that are

Championships

  • idChampionships,
  • Name
  • Details

Users

  • idUsers
  • UserName
  • Password

participants

  • idparticipants
  • idChampionships
  • FirstName
  • LastName
  • DateOfBirth
  • NationalRanking
  • Photo
  • PhoneNumber
  • EmailAddress
  • IdUsers

What i'm trying to do is taking 2 existed ID's in Championships, Users and insert them to the participants table on submitting the form to identify which championship he's participating in and who is that user. to avoid duplications.

It appears i have a parsing error that i cannot figure it out, which is indicated in the last line.

Please help!

Here is the code

<?php require_once('Connections/ACBSEntrySystem.php'); ?>
   <?php
       if (!function_exists("GetSQLValueString")) {
         function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 

{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch ($theType) {
      case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
      case "long":
      case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
      case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
      case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
      case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
  }
 }

      $editFormAction = $_SERVER['PHP_SELF'];
      if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO participants (idparticipants, idChampionships, FirstName, LastName, DateOfBirth, NationalRanking, Photo, PhoneNumber, EmailAddress, IdUsers) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['idparticipants'], "int"),
                       GetSQLValueString($_POST['idChampionships'], "int"),
                       GetSQLValueString($_POST['FirstName'], "text"),
                       GetSQLValueString($_POST['LastName'], "text"),
                       GetSQLValueString($_POST['DateOfBirth'], "date"),
                       GetSQLValueString($_POST['NationalRanking'], "int"),
                       GetSQLValueString($_POST['Photo'], "text"),
                       GetSQLValueString($_POST['PhoneNumber'], "double"),
                       GetSQLValueString($_POST['EmailAddress'], "text"),
                       GetSQLValueString($_POST['idUsers'], "int"));

  mysql_select_db($database_ACBSEntrySystem, $ACBSEntrySystem);
  $Result1 = mysql_query($insertSQL, $ACBSEntrySystem) or die(mysql_error());
}

 mysql_select_db($database_ACBSEntrySystem, $ACBSEntrySystem);
 $query_AvailableChampionships = "SELECT idChampionships, Name FROM Championships ORDER BY Name ASC";
 $AvailableChampionships = mysql_query($query_AvailableChampionships, $ACBSEntrySystem) or die(mysql_error());
 $row_AvailableChampionships = mysql_fetch_assoc($AvailableChampionships);
 $totalRows_AvailableChampionships = mysql_num_rows($AvailableChampionships);



 mysql_select_db($database_ACBSEntrySystem, $ACBSEntrySystem);
 $query_ActiveUser = "SELECT idUsers FROM Users";
$ActiveUser = mysql_query($query_ActiveUser, $ACBSEntrySystem) or die(mysql_error());
$row_ActiveUser = mysql_fetch_assoc($ActiveUser);
$totalRows_ActiveUser = mysql_num_rows($ActiveUser);

mysql_select_db($database_ACBSEntrySystem, $ACBSEntrySystem);
$query_InsertingARecord = "SELECT * FROM participants";
$InsertingARecord = mysql_query($query_InsertingARecord, $ACBSEntrySystem) or die(mysql_error());
$row_InsertingARecord = mysql_fetch_assoc($InsertingARecord);
$totalRows_InsertingARecord = mysql_num_rows($InsertingARecord);
?>

The form starts from here, in the body of the HTML

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
              <table align="center">
                <tr valign="baseline">
                  <td nowrap align="right">IdChampionships:</td>
                  <td><select name="idChampionships">
                    <?php 
do {  
?>
                    <option value="<?php echo $row_AvailableChampionships['idChampionships']?>" ><?php echo $row_AvailableChampionships['Name']?></option>
                    <?php
} while ($row_AvailableChampionships = mysql_fetch_assoc($AvailableChampionships));
?>
                  </select></td>
                <tr>
                <tr valign="baseline">
                  <td nowrap align="right">FirstName:</td>
                  <td><input type="text" name="FirstName" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">LastName:</td>
                  <td><input type="text" name="LastName" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">DateOfBirth:</td>
                  <td><input type="text" name="DateOfBirth" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">NationalRanking:</td>
                  <td><input type="text" name="NationalRanking" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">Photo:</td>
                  <td><input type="text" name="Photo" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">PhoneNumber:</td>
                  <td><input type="text" name="PhoneNumber" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">EmailAddress:</td>
                  <td><input type="text" name="EmailAddress" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">&nbsp;</td>
                  <td><input type="submit" value="Insert record"></td>
                </tr>
              </table>
              

A loop for the Users starts here to echo the idUser

<input type="hidden" name="idparticipants" value="">
                  <input type="hidden" name="IdUsers" value="<?php do {echo $row_ActiveUser['idUsers'];while ($row_ActiveUser = mysql_fetch_assoc($ActiveUser));?>">
                  <input type="hidden" name="MM_insert" value="form1">
                </form>
                <p>&nbsp;</p>
              </div>
            </div>
          </div>
        </section>
        
        <!--==============================footer================================-->
       <?php include("../include/footer.php");?>
      </div>
    </div>
    </body>
    
    </html>
    <?php
    mysql_free_result($AvailableChampionships);
    
    mysql_free_result($ActiveUser);
    
    mysql_free_result($InsertingARecord);
    ?>

There error am getting is

Parse error: syntax error, unexpected $end in /Applications/XAMPP/xamppfiles/htdocs/entryform.php on line 191


Solution

  • Your do-while loop is missing a closing curly brace.

    `value="<?php do {echo $row_ActiveUser['idUsers'];while ($row_ActiveUser = mysql_fetch_assoc($ActiveUser));?>">`
    

    should be

    value="<?php
    do {
        echo $row_ActiveUser['idUsers'];
    } while ($row_ActiveUser = mysql_fetch_assoc($ActiveUser));
    ?>">