将数据插入MySQL数据库但字段为空

时间:2014-01-03 19:05:14

标签: php mysql mysqli

您好,我目前正在学习PHP的过程中,通过以下关于php的一些教程和我收到一个错误,其中通过网站插入数据库的数据在Mysql数据库中给出一个空白行,并且没有出现在网站即可。我正在使用phpmyadmin检查数据是否已插入。我搜索了网站上的任何答案,但除了要求更改为我已经做过的“mysqli”之外无法获得任何答案。如果有人能帮助我,我将不胜感激。 谢谢。这是我下面的代码。 enter image description here enter image description here

这是来自create_newsubject.php页面的代码:

    <?php include("includes/functions.php"); ?>
<?php include("includes/connection.php"); ?>
<?php
$errors = array();
//form validation 
$required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
            $errors[] = $fieldname;
        }
    }

$fields_with_lengths = array('menu_name' => 30);
foreach($fields_with_lengths as $fieldname => $maxlength) {
    if (strlen(trim(mysqli_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname; }   
    }   




if (!empty($errors)) {
        header("Location: new_subject.php");
        exit;
}
?>
<?php 
    $menu_name = mysqli_prep($_POST['menu_name']);
    $position = mysqli_prep($_POST['position']);  
    $visible = mysqli_prep($_POST['visible']);
?>
<?php
    $query = "INSERT INTO subjects (
                menu_name, position, visible
            ) VALUES (
                '{$menu_name}', '{$position}', '{$visible}'
            )";
            $result = mysqli_query($connection, $query);
    if ($result) {
            //Success
            header("Location: content.php");
            exit;
    } else {
            //Display error message
            echo "<p>Subject creation failed </p>";
            echo "<p>" . mysqli_error($connection) . "</p>";
    }
?>
<?php mysqli_close($connection); ?>

**
  • 这是来自function.php页面的代码:

**

    <?php
    function mysqli_prep( $value ) {
            $magic_quotes_active = get_magic_quotes_gpc();
    $new_enough_php = function_exists("mysqli_real_escape_string");
    //i.e. php >= v4.3.0
            if( $new_enough_php ) { //php v4.3.0  or higher
                //undo any magic quote effects so mysql can do the work
        if( $magic_quotes_active ) {$value = stripslashes( $value ); }
            $value = mysqli_real_escape_string( $value );
        } else { //before php v4.3.0
            // if magic quotes arnt already on then add slashes manually
        if(!$magic_quotes_active) { $value = addslashes( $value ); }
           //if magic quotes are active then the slashes already exist
         }

        return $value;

    }
    function redirect_to( $location = NULL ) {
        if ($location != NULL) {
            header("Location: {$location}");
            exit;
        }
    }
    function confirm_query($result_set) {
            global $connection;
            if(!$result_set) {
                    die("Database query failed: " . 
                    mysqli_error($connection));
            }

    }
    function get_all_subjects() {
        global $connection;
        $query = "SELECT * 
                 FROM subjects
                 ORDER BY position ASC";
        $subject_set = mysqli_query($connection, $query);
        confirm_query($subject_set);
        return $subject_set;

    }
    function get_all_pages($subject_id) {
        global $connection;
        $query = "SELECT * 
                  FROM pages 
                  WHERE subject_id = {$subject_id}
                  ORDER BY position ASC";
        $page_set = mysqli_query($connection, $query);
        confirm_query($page_set);
        return $page_set;
    }
    function get_subject_by_id($subject_id) {
        global $connection;
        $query = "SELECT * ";
        $query.= "FROM subjects "; 
        $query.= " WHERE id='" . $subject_id ."' ";
        $query .= " LIMIT 1";       
        $result_set = mysqli_query($connection, $query);
        confirm_query($result_set);
        if ($subject = mysqli_fetch_array($result_set)) {
            return $subject;
    } else {
            return NULL;
    }
}
    function get_page_by_id($page_id) {
            global $connection;
            $query = "SELECT * ";
            $query .= " FROM pages ";
            $query .= " WHERE id= '" . $page_id ."' ";
            $query .= " LIMIT 1";
            $result_set = mysqli_query($connection, $query);
            confirm_query($result_set);
            if ($page = mysqli_fetch_array($result_set)) {
                return $page;
            } else {
                return NULL;
            }

    }
    function find_selected_page() {
        global $sel_subject;
        global $sel_page;
      if (isset($_GET['subj'])) {
            $sel_subject = get_subject_by_id($_GET['subj']);
            $sel_page = NULL;
    } elseif (isset($_GET['page'])) {
            $sel_subject = NULL;
            $sel_page = get_page_by_id($_GET['page']);
    } else {
        $sel_subject = NULL;
        $sel_page = NULL;
    }
}
    function navigation($sel_subject, $sel_page){
        $output = "<ul class=\"subjects\">";
        //3.Perform database query
        $subject_set = get_all_subjects();          
        //4. Use returned data
        while ($subject = mysqli_fetch_array($subject_set)) {
                $output .= "<li";
                if ($subject["id"] == $sel_subj['id']) 
                { $output .= " class=\"selected\""; }
                $output .= "><a href=\"edit_subject.php?subj=" . 
                urlencode($subject["id"]) . "\">
                {$subject["menu_name"]}</a></li>";
                $page_set = get_all_pages($subject["id"]);

        $output .= "<ul class=\"pages\">";
        while ($page = mysqli_fetch_array($page_set)) {
                $output .= "<li";
                if ($page["id"] == $sel_page['id']) 
                { $output .= " class=\"selected\""; }
                $output .= "><a href=\"content.php?page=" . 
                urlencode($page["id"]) . "\">{$page["menu_name"]}
                </a></li>";
        }
        $output .= "</ul>"; 
        }       
        $output .= "</ul>";
        return $output;

    }
?>

连接页面::

<?php
//1. Create database connection
$connection = mysqli_connect("localhost","root","Password");
if (!$connection) {
    die("Database connection failed: " . mysqli_error($connection));        
}
//2.Select a database to use
$db_select = mysqli_select_db($connection, "widget_corp");
if (!$db_select) {
    die("Database selection failed:" . mysqli_error($connection));
}
?>

HTML Page ::

<?php include("includes/connection.php");?>
<?php include("includes/header.php");?>
<?php include("includes/functions.php");?>
<?php find_selected_page();?>

<table id="structure">
    <tr>
        <td id="navigation">
            <?php echo navigation($sel_subject, $sel_page); ?>

        </td>
        <td id="page">
            <h2>Add Subject</h2>
            <form action="create_newsubject.php" method="post">
    <p>Subject name: <input type="text" name="menu_name" value=""
    id="menu_name" /></p>
    <p> Position:
        <select name="position">    
            <?php
                $subject_set = get_all_subjects();
                $subject_count = mysqli_num_rows($subject_set);
                //subject count +1 because we are adding a subject
                for($count=1; $count <= $subject_count+1; $count++) {
                    echo "<option value=\"{$count}\">{$count}</option>";
                }
            ?>
        </select>   
    </p>
    <p>Visible:
       <input type="radio" name="visible" value"0" /> No
       &nbsp;
       <input type="radio" name="visible" value="1" /> Yes
    </p>
    <input type="submit" value="Add Subject" /> 
</form>
            <br />
            <a href="content.php">Cancel</a>
        </td>
    </tr>
</table>
<?php require("includes/footer.php");?> 

1 个答案:

答案 0 :(得分:2)

您应该使用预准备语句,但代码中的一个问题是您使用mysqli_real_escape_string。正如您在manual中看到的,第一个参数是数据库连接。