将查询结果插入表中

时间:2014-08-26 22:03:33

标签: php mysql forms mysqli

我正在尝试创建一个符号,其中写入多个表,这些表由每个表的主键链接。因此,它查询它写入第一个表的内容,以找到要插入第二个表的自动递增主键。

但是我一直在说错误:

  

解析错误:语法错误,意外T_ENCAPSED_AND_WHITESPACE,在第79行期待T_STRING或T_VARIABLE或T_NUM_STRING

但我找不到错误。

提前感谢您的帮助。

    <?php # DISPLAY COMPLETE REGISTRATION PAGE.

# Set page title and display header section.
    $page_title = 'Register' ;
    include ( 'includes/header.html' ) ;

    `enter code here`# Check form submitted.
    `enter code here`if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
    {
      # Connect to the database.
      require ('connect_db.php'); 

      # Initialize an error array.
      $errors = array();

      # Check for a username.
      if ( empty( $_POST[ 'username' ] ) )
      { $errors[] = 'Enter your username.' ; }
      else
      { $q1 = mysqli_real_escape_string( $dbc, trim( $_POST[ 'username' ] ) ) ; }


       # Check for an email address:
       if ( empty( $_POST[ 'email' ] ) )
      { $errors[] = 'Enter your email address.'; }
      else
      { $q2 = mysqli_real_escape_string( $dbc, trim( $_POST[ 'email' ] ) ) ; } 


      # Check for a unit name:
      if ( empty( $_POST[ 'unitusername' ] ) )
  { $errors[] = 'Enter your unit name.'; }
  else
  { $q4 = mysqli_real_escape_string( $dbc, trim( $_POST[ 'unitusername' ] ) ) ; }


  # Check for a password and matching input passwords.
  if ( !empty($_POST[ 'pass1' ] ) )
  {
    if ( $_POST[ 'pass1' ] != $_POST[ 'pass2' ] )
    { $errors[] = 'Passwords do not match.' ; }
    else
    { $q3 = mysqli_real_escape_string( $dbc, trim( $_POST[ 'pass1' ] ) ) ; }
  }
  else { $errors[] = 'Enter your password.' ; }

  # Check if username already registered.
  if ( empty( $errors ) )
  {
    $q = "SELECT username FROM users WHERE username='$q1'" ;
    $r = @mysqli_query ( $dbc, $q ) ;
    if ( mysqli_num_rows( $r ) != 0 ) $errors[] = 'Username already registered. <a href="login.php">Login</a>' ;
  }

    # Check if unit already registered.
  if ( empty( $errors ) )
  {
    $q = "SELECT unitusername FROM unit WHERE unitusername='$q4'" ;
    $r = @mysqli_query ( $dbc, $q ) ;
    if ( mysqli_num_rows( $r ) != 0 ) $errors[] = 'Unit already registered. <a href="login.php">Login</a>' ;
  }

  # On success register user inserting into 'unit' database table.
  if ( empty( $errors ) ) 
  {
    $q = "INSERT INTO unit (unitusername, mainuserid) VALUES ('$q4', '0' )";
    $r = @mysqli_query ( $dbc, $q ) ;}


    # Find unit id from newly registered unit 
    $q5 = mysqli_query($dbc, "SELECT unitid FROM unit WHERE unitusername = '$q4'");

    while($row5 = mysqli_fetch_array($q5))
  {

  # On success register user inserting into 'users' database table.
  if ( empty( $errors ) ) 
  {
    $q = "INSERT INTO users (username, email, password, usertype, unitid) VALUES ('$q1', '$q2', SHA1('$q3'), '1', '$row5['unitid']')";
    $r = @mysqli_query ( $dbc, $q ) ;
   }}


    # Find unit id from newley registered unit 
    $q6 = mysqli_query($dbc, "SELECT userid FROM users WHERE username = '$q1'");


    #put the main userid into the unit table
  if ( empty( $errors ) ) 
  {
    $q = "UPDATE unit SET mainuserid = '$q6' WHERE unitid = '$q5'";
    $r = @mysqli_query ( $dbc, $q ) ;
   }    

   #populate the unituser table
  if ( empty( $errors ) ) 
   {
    $q = "INSERT INTO unituser (unitid, userid) VALUES ('$q5', '$q6')";
    $r = @mysqli_query ( $dbc, $q );



  echo '<h1>Registered!</h1><p>You are now registered.</p><p><a href="login.php">Login</a></p>';
    # Close database connection.
    mysqli_close($dbc); 

    # Display footer section and quit script:
    include ('includes/footer.html'); 
    exit(); }

  # Or report errors.
  else 
  {
    echo '<h1>Error!</h1><p id="err_msg">The following error(s) occurred:<br>' ;
    foreach ( $errors as $msg )
    { echo " - $msg<br>" ; }
    echo 'Please try again.</p>';
    # Close database connection.
    mysqli_close( $dbc );
  }  
}
?>

<!-- Display body section with sticky form. -->
<h1>Register</h1>
<form action="registernewunit.php" method="post">
<p>username: <input type="text" name="username" size="20" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>"> 
Unit Name: <input type="text" name="unitusername" size="20" value="<?php if (isset($_POST['unitusername'])) echo $_POST['unitusername']; ?>"></p>
<p>Email Address: <input type="text" name="email" size="50" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"></p>
<p>Password: <input type="password" name="pass1" size="20" value="<?php if (isset($_POST['pass1'])) echo $_POST['pass1']; ?>" >
Confirm Password: <input type="password" name="pass2" size="20" value="<?php if (isset($_POST['pass2'])) echo $_POST['pass2']; ?>"></p>
<p><input type="submit" value="Register"></p>
</form>

1 个答案:

答案 0 :(得分:0)

  

解析错误:语法错误,意外T_ENCAPSED_AND_WHITESPACE,在第79行期待T_STRING或T_VARIABLE或T_NUM_STRING

源于

'$row5['unitid']'在VALUES中应为'$row5[unitid]'