来自mysql_insert_id函数的PHP表单和SESSION变量

时间:2014-01-18 21:45:23

标签: php mysql session

我正在尝试从会员那里收集信息,

<?php require_once('config.php'); ?>
<?php require_once('functions/logout.php'); ?>
<?php require_once('functions/auth.php'); ?>
<?php include('functions/insert_script.php'); ?>

这是一切都在发生的形式

<form id="pro_form2" method="post" name="form1" action="<?php echo $editFormAction; ?>">`
<tr>
    <td><label>

        <span class="pro_text-form fleft">Championship :</span></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>
    </label>
    </td>
  </tr>
  <tr>
    <td><label><span class="pro_text-form">First Name :</span></td>
    <td><span id="spryFirstName">
      <input type="text" name="FirstName" value="">

      <span class="textfieldRequiredMsg">*</span></span>      </label></td>
  </tr>
  <tr>
    <td><label><span class="pro_text-form">Last Name :</span></td>
    <td><span id="spryLastName">
      <input type="text" name="LastName" value="">

      <span class="textfieldRequiredMsg">*</span></span>      </label></td>
  </tr>
  <tr>
    <td><label><span class="pro_text-form">Date of Birth : &nbsp;&nbsp;<em>hint: 1991-09-25</em></span></td>
    <td>
    <input name="DateOfBirth" type="text" id="" value="" >
          </label></td>
  </tr>
  <tr>
    <td><label><span class="pro_text-form">National Ranking :</span></td>
    <td><span id="spryNationalRanking">
    <input type="text" name="NationalRanking" value="" >
    <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>      </label></td>
  </tr>
  <tr>
    <td><label><span class="pro_text-form">Phone Number :</span></label></td>
    <td><span id="spryPhoneNumber">
    <input type="text" name="PhoneNumber" value="" >
    <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>  </td>
  </tr>
  <tr>
    <td><label><span class="pro_text-form">Email Address :</span></label></td>
    <td><span id="spryEmailAddress">
    <input type="text" name="EmailAddress" value="">
    <span class="textfieldRequiredMsg">*</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>      </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input class="pro_btn" type="submit" value="Submit"></td>
  </tr>
        <input type="hidden" name="idparticipant" value="">

            <input type="hidden" name="idUsers" value="<?php echo $_SESSION['MM_UserGroup']; ?>">
            <input type="hidden" name="created_at" value="<?php 
                                                            $now = new DateTime();
                                                            echo $now->format('Y-m-d H:i:s');?>
                                                           ">
            <input type="hidden" name="MM_insert" value="form1">
        </form>
</table>

insert_script.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, PhoneNumber, EmailAddress, idUsers, created_at) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['idparticipants'], "int"),
                       GetSQLValueString($_POST['idChampionships'], "int"),
                       GetSQLValueString(str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($_POST['FirstName'])))), "text"),
                       GetSQLValueString(str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($_POST['LastName'])))), "text"),
                       GetSQLValueString($_POST['DateOfBirth'], "date"),
                       GetSQLValueString($_POST['NationalRanking'], "int"),
                       GetSQLValueString($_POST['PhoneNumber'], "double"),
                       GetSQLValueString($_POST['EmailAddress'], "text"),
                       GetSQLValueString($_POST['idUsers'], "int"),
                       GetSQLValueString($_POST['created_at'], 'date'));


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

session_start();
$idparticipants = mysql_insert_id();
$_SESSION['idparticipants'] = $idparticipants;

// going to the list 
$insertGoTo = "entryfiles.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}


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 UserName 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);



//Find the number of entered rows per user to avoid over enteries to the database
$colname_numRows = "-1";
if (isset($_SESSION['MM_UserGroup'])) {
  $colname_numRows = $_SESSION['MM_UserGroup'];
}
mysql_select_db($database_acbs, $acbs);
$query_numRows = sprintf("SELECT * FROM participants WHERE idUsers = %s", GetSQLValueString($colname_numRows, "int"));
$numRows = mysql_query($query_numRows, $acbs) or die(mysql_error());
$row_numRows = mysql_fetch_assoc($numRows);
$totalRows_numRows = mysql_num_rows($numRows);
?>

我要做的是在此部分将mysql_insert_id();存储到$_SESSION['idparticipants'];

session_start();
$idparticipants = mysql_insert_id();
$_SESSION['idparticipants'] = $idparticipants;

并在另一个页面中使用它,我面临的问题是在另一个页面中,$_SESSION['idparticipants'];始终为零,表示没有变量存储到该会话,

我真的需要你帮助的人,我不知道我哪里出错了,今天早上运作良好,而且我改变了很多,当我再次测试它不起作用时,

任何帮助将不胜感激。

0 个答案:

没有答案