需要有关Sqli数据的PHP表单的帮助

时间:2014-01-04 08:49:35

标签: php mysql forms

我不确定为什么,但我的表格似乎没有通过。它包含MySQL lite数据。

new_subject将信息传递给create_subject以创建新主题。我已将表单设置为将manage_content重定向到成功,如果成功,则保留在new_subject。由于我卡在new_subject.php上,这可能意味着PHP表单在某处失败了。

功能

<?php

  function confirm_query($result_set) {
    if (!$result_set) { 
      die("Database query failed: ".
    mysqli_connect_error() .
    " (" . mysqli_connect_errno(). ")"
    );
}
  }

  function redirect_to($new_location) {
    header("Location: " . $new_location);  
    exit;  
  }

  function mysql_prep($string) {
      global $connection;

      $escaped_string = mysqli_real_escape_string($connection, $string);
      return $escaped_string;
  }

  function find_all_subjects() {
      global $connection;

      $query = "SELECT * "; 
      $query .= "FROM subjects ";
      $query .= "ORDER BY position ASC";
      $subject_set = mysqli_query($connection, $query);

      confirm_query($subject_set);
      return $subject_set;
  }

  function find_all_pages_for_subject($subject_id) {
      global $connection;

      $safe_subject_id = mysqli_real_escape_string($connection,
     $subject_id);

      $query = "SELECT * "; 
      $query .= "FROM pages ";
      $query .= "WHERE subject_id = {$safe_subject_id} ";
      $query .= "ORDER BY position ASC";
      $page_set = mysqli_query($connection, $query);

      confirm_query($page_set);
      return $page_set;  
  }

 function find_subject_by_id($subject_id) {
      global $connection;

     $safe_subject_id = mysqli_real_escape_string($connection,
     $subject_id);

      $query = "SELECT * "; 
      $query .= "FROM pages ";
      $query .= "WHERE id = {$subject_id} ";
      $query .= "LIMIT 1";
      $subject_set = mysqli_query($connection, $query);
      confirm_query($subject_set);
      if($subject = mysqli_fetch_assoc($subject_set)) {
      return $subject;   
 } else {
     return null;
    }
 }




  function find_page_by_id($page_id) {
      global $connection;

     $safe_page_id = mysqli_real_escape_string($connection,
     $page_id);

      $query = "SELECT * "; 
      $query .= "FROM pages ";
      $query .= "WHERE id = {$page_id} ";
      $query .= "LIMIT 1";
      $page_set = mysqli_query($connection, $query);
      confirm_query($page_set);
      if($page = mysqli_fetch_assoc($page_set)) {
      return $page;   
 } else {
     return null;
    }
 }

  function find_selected_page() {
      global $current_subject;
      global $current_page;

  if (isset($_GET["subject"])) {
    $current_subject = find_subject_by_id($_GET["subject"]);
    $current_page = null;
} elseif (isset($_GET["page"])) {
    $current_subject = null;
    $current_page = find_page_by_id($_GET["page"]); 
} else {
    $current_subject = null;
    $current_page = null;
}
  }

  //navigation takes 2 arguements
  // the currently subject array or null
  //the currently selected page array or null
  function navigation($subject_array, $page_array) {
    $output = "<ul class=\"subjects\">";
//2. Perform database query to find all subjects through
// functions page
 $subject_set = find_all_subjects();

//3 Use returned data (if any)
while($subject = mysqli_fetch_assoc($subject_set)) {

   $output .= "<li";
   if ($subject_array && $subject["id"] == $subject["id"]) {
       $output .= " class=\"selected\"";       
   }
    $output .= ">";
    $output .= "<a href=\"manage_content.php?subject=";
    $output .= urlencode($subject["id"]);
    $output .= "\">";
    $output .= $subject["menu_name"];
    $output .= "</a>";   

//2. Perform database query by going to functions and
// finding all pages
$page_set = find_all_pages_for_subject($subject["id"]);

    $output .= "<ul class=\"pages\">";

//3 Use returned data (if any)
while($page = mysqli_fetch_assoc($page_set)) {

   $output .= "<li";
   if ($page_array && $page["id"] == $page_array) {
      $output .= " class=\"selected\"";    
   }
   $output .= ">";
   $output .= "<a href=\"manage_content.php?page=";   
   $output .= urlencode($page["id"]);
   $output .="\">";
   $output .= $page["menu_name"];
   $output .= "</a></li>";
}

//4. Release returned data
mysqli_free_result($page_set);
$output .= "</ul></li>";
}

//4. Release returned data
mysqli_free_result($subject_set);
$output .= "</ul>"; 
return $output; 
}

?>

manage_content

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("header.php");?>
<?php find_selected_page(); ?>

<div id ="main">
  <div id ="navigation">
  <?php
   // parse the navigation section that i sent into functions.php
   echo navigation($current_subject, $current_page); ?>




<?php 
/*

   <ul class="subjects">
<?php
//2. Perform database query to find all subjects through
// functions page
 $subject_set = find_all_subjects(); ?>

<?php
//3 Use returned data (if any)
while($subject = mysqli_fetch_assoc($subject_set)) {
?>  
   <?php 
   echo "<li";
   if ($subject["id"] == $selected_subject_id) {
       echo " class=\"selected\"";     
   }
   echo ">";
    ?>
    <a href="manage_content.php?subject=<?php echo
urlencode($subject["id"]); ?>"><?php echo $subject["menu_name"]; ?> </a>   
<?php
//2. Perform database query by going to functions and
// finding all pages
$page_set = find_all_pages_for_subject($subject["id"]); ?>
<ul class="pages">
<?php
//3 Use returned data (if any)
while($page = mysqli_fetch_assoc($page_set)) {
?>


   <?php 
   echo "<li";
   if ($page["id"] == $selected_page_id) {
       echo " class=\"selected\"";     
   }
   echo ">";
    ?>

   <a href="manage_content.php?page=<?php echo
    urlencode($page["id"]); ?>"><?php echo $page["menu_name"]; ?></a>
</li>
<?php
}
?>
<?php
//4. Release returned data
mysqli_free_result($page_set);?>
</ul>

</li>

   <?php
}
?>
<?php
//4. Release returned data
mysqli_free_result($subject_set);?>
</ul>


*/

?>
<br />
<a href="new_subject.php">+ Add a subject</a>

</div>
<div id="page">
<?php echo message();?>


<?php if ($current_subject) { ?>
<h2> Manage Subject</h2>
<?php /*
the current_subject parse in the selected subject ID to the
subject id values inside the functions 
next echo out the content of each page
*/?>

Content: <?php echo $current_subject["content"]; ?><br />

<?php } elseif ($current_page) { ?>
<h2>Manage Page</h2>

Content: <?php echo $current_page["content"]; ?><br />
<?php } else { ?>
Please selected a subject or a page.
<?php }?>



</div>
</div>
<?php include("includes/footer.php");?>

new_subject

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("header.php");?>
<?php find_selected_page(); ?>

<div id ="main">
  <div id ="navigation">
  <?php
   // parse the navigation section that i sent into functions.php
   echo navigation($current_subject, $current_page); ?>




<?php 
/*

   <ul class="subjects">
<?php
//2. Perform database query to find all subjects through
// functions page
 $subject_set = find_all_subjects(); ?>

<?php
//3 Use returned data (if any)
while($subject = mysqli_fetch_assoc($subject_set)) {
?>  
   <?php 
   echo "<li";
   if ($subject["id"] == $selected_subject_id) {
       echo " class=\"selected\"";     
   }
   echo ">";
    ?>
    <a href="manage_content.php?subject=<?php echo
urlencode($subject["id"]); ?>"><?php echo $subject["menu_name"]; ?> </a>   
<?php
//2. Perform database query by going to functions and
// finding all pages
$page_set = find_all_pages_for_subject($subject["id"]); ?>
<ul class="pages">
<?php
//3 Use returned data (if any)
while($page = mysqli_fetch_assoc($page_set)) {
?>


   <?php 
   echo "<li";
   if ($page["id"] == $selected_page_id) {
       echo " class=\"selected\"";     
   }
   echo ">";
    ?>

   <a href="manage_content.php?page=<?php echo
    urlencode($page["id"]); ?>"><?php echo $page["menu_name"]; ?></a>
</li>
<?php
}
?>
<?php
//4. Release returned data
mysqli_free_result($page_set);?>
</ul>

</li>

   <?php
}
?>
<?php
//4. Release returned data
mysqli_free_result($subject_set);?>
</ul>


*/

?>


</div>
<div id="page">

<?php echo message();?>

<h2>Create Subject</h2>

<form action="create_subject.php" method="post">
<p>Subject name:
<input type="text" name="menu_name" value="" />
</p>
<p>Position:
<select name="position">
<?php
$subject_set = find_all_subjects();
$subject_count = mysqli_num_rows($subject_set);

 for($count=1; $count <= ($subject_count + 1); $count++) {
    echo "<option value=\"{$count}\">{$count}</option>";     
 }
?>
</select>
</p>
<input type="submit" value="Create Subject" />
</form>
<br />
<a href="manage_content.php">Cancel</a>


</div>
</div>
<?php include("includes/footer.php");?>

create_subject

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>

<?php
if (isset($_POST['submit'])) {
    // Process the form

    $menu_name = mysql_prep($_POST["menu_name"]);
    $position = (int) $_POST["position"];

    // 2. Perform database query
    $query .= "INSERT INTO subjects (";
    $query .= " menu_name, position";
    $query .= ") VALUES (";
    $query .= " '{$menu_name}', {$position}";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
        // Success
        $_SESSION["message"] = "Subject created.";
        redirect_to("manage_content.php");
    } else {
        // Failure
        $_SESSION["message"] = "Subject creation failed.";
        redirect_to("new_subject.php");


    }


} else {
    // This is probably a GET request
    redirect_to("new_subject.php");
}
?>


<?php
   if (isset($connection)) { mysqli_close($connection); }
?>

1 个答案:

答案 0 :(得分:1)

好的,我发现了错误。我关注的视频忘了把名字=“提交”放在表格中,这就是为什么它没有提交。它现在正常运作。谢谢大家的投入,它帮助了我=)。