PHP将正确的字符编码更改为未知字符编码

时间:2015-06-25 06:58:46

标签: php mysql utf-8

我正在 PHP 网站上工作,它有一个管理员面板来更新网站内容..

当我尝试将内容插入 MySql 数据库时,一切正常并且内容正确显示在我的网站中但当我尝试编辑我的内容时,每个文本都更改为未知字符,如此

“اساÛ�Û�تاÙ�Û�Ù�ک¿ï¿½Ù�دگاÙï¿” ..

我的网站使用波斯语,我使用

<meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>  

我是 PHP 的新人,我不知道为什么会发生这种情况..

这是我的编辑页面

<? ob_start(); ?>
<?php
                   session_start();
                  if($_SESSION['karbar']!=null){}
                  else{
                      header("Location:../login.php");}
                     echo "  <nav class=\"collapse navbar-collapse bs-navbar-collapse\"> " ;
echo "<ul class=\"nav navbar-nav navbar-right\">";
                                  echo "<li><a class='exit' style='text-decoration:none' href='logout.php'>خروج</a></li> ";
                                                            echo"    <li style=\"margin-top:14px;\" >   ";
                          echo $_SESSION['karbar']  ;
                          echo " </li>";
                          echo "</ul>";
                         echo " </nav> ";


             ?>
<?php
    /*
        Allows the user to both create new records and edit existing records
    */

    // connect to the database
    // server info
$server = 'localhost';
$user = 'isatisbc_user';
$pass = '**********';
$db = 'isatisbc_db';

// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);

// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);

    // creates the new/edit record form
    // since this form is used multiple times in this file, I have made it a function that is easily reusable
    function renderForm($Project_Title = '', $Project_Desc ='', $error = '', $id = '')
    { ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
        <html>
            <head>
                                  <meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="" name="description">
<meta content="" name="keywords">
<meta content="" name="author">
    <script language="javascript" type="text/javascript" src="tiny_mce/tiny_mce.js"></script>    
    <script language="javascript" type="text/javascript">
  tinyMCE.init({
    theme : "advanced",
    mode: "exact",
    elements : "TextBody",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
    + "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
    + "bullist,numlist,outdent,indent",
    theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
    +"undo,redo,cleanup,code,separator,sub,sup,charmap",
    theme_advanced_buttons3 : "",
    height:"350px",
    width:"600px"
});

</script>
    <link rel="stylesheet" href="../Admin/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/css/font-awesome.css"> 
                <title>
                    ویرایش
                </title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            </head>
            <body>

            <header class="navbar  navbar-default" id="top" role="banner">
  <div class="container">

    <div class="navbar-header">
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="../" class="navbar-brand">نمایش وب سایت</a>
    </div>
    <nav class="collapse navbar-collapse bs-navbar-collapse">
      <ul class="nav navbar-nav">


                <li>
          <a href="news.php">اخبار</a>
        </li>

        <li>
          <a href="../components/">مطالب</a>
        </li>
        <li>
          <a href="essay.php">مقالات</a>
        </li>
        <li>
          <a href="event.php">رویداد ها</a>
        </li>
        <li class="dropdown">
                        <a aria-expanded="false" role="button" data-toggle="dropdown" class="dropdown-toggle active" href="index.php">خدمات و پروژه ها <span class="caret"></span></a>
                        <ul role="menu" class="dropdown-menu ">

                            <li><a href="index.php">عناوین</a></li>
                          <li class="divider"></li>
                          <li class="active"><a href="ProjectImg.php">تصاویر</a></li>

                        </ul>
                      </li>
      </ul>
  </div>
</header>


    <div class="" id="content" tabindex="-1">
      <div class="container">
        <h1>ویرایش پروژه</h1>
        <p></p>
      </div>
    </div>

    <div class="container">


      <div class="row">
          <div class="col-md-11" role="main">
                             <form method="post" action="">  
                                                <?php if ($id != '') { ?>
                        <input type="hidden" name="id" value="<?php echo $id; ?>" />
                          <div class="row">
  <div class="col-md-4"></div>
  <div class="col-md-4"> <input id="Title" name="Title" type="text" size="95" value="<?php echo $Project_Title; ?>">  </div>
  <div class="col-md-4">عنوان خدمات :</div>
</div>
                                         <div class="row">
  <div class="col-md-4"></div>
  <div class="col-md-4"> <textarea id="TextBody" name="TextBody" rows="15" ><?php echo $Project_Desc; ?></textarea>     </div>
  <div class="col-md-4">توضیحات خدمات :</div>
</div>

                                 <input type="submit" name="submit" value="ثبت" class="btn btn-info"/>

                             </form>

          </div>



               </div>
         <div class="row">
          <div class="col-md-11" role="main">



          </div>


</div>
               </div>
              </div>
            <div class="footer">
                         </div>
         </div>
        </form>






            <script src="Admin/js/bootstrap.min.js"></script>
                <script src="../js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="Admin/js/ie10.js"></script>
            </body>
        </html>
                    <?php } ?>

    <?php }



        /*

           EDIT RECORD

        */
    // if the 'id' variable is set in the URL, we know that we need to edit a record
    if (isset($_GET['id']))
    {
        // if the form's submit button is clicked, we need to process the form
        if (isset($_POST['submit']))
        {
            // make sure the 'id' in the URL is valid
            if (is_numeric($_POST['id']))
            {
                // get variables from the URL/form
                $id = $_POST['id'];
                $Title = htmlentities($_POST['Title'], ENT_QUOTES);
                $TextBody = htmlentities($_POST['TextBody'], ENT_QUOTES);

                // check that Title and TextBody are both not empty
                if ($Title == '' || $TextBody == '')
                {
                    // if they are empty, show an error message and display the form
                    $error = 'ERROR: Please fill in all required fields!';
                    renderForm($Title, $TextBody, $error, $id);
                }
                else
                {
                    // if everything is fine, update the record in the database
                    if ($stmt = $mysqli->prepare("UPDATE table_project SET  Project_Title = ?, Project_Desc = ?
                        WHERE id=?"))
                    {
                        $stmt->bind_param("ssi", $Title, $TextBody, $id);
                        $stmt->execute();
                        $stmt->close();
                    }
                    // show an error message if the query has an error
                    else
                    {
                        echo "ERROR: could not prepare SQL statement.";
                    }

                    // redirect the user once the form is updated

                                     header("Location: index.php");


                }
            }
            // if the 'id' variable is not valid, show an error message
            else
            {
                echo "Error!";
            }
        }
        // if the form hasn't been submitted yet, get the info from the database and show the form
        else
        {
            // make sure the 'id' value is valid
            if (is_numeric($_GET['id']) && $_GET['id'] > 0)
            {
                // get 'id' from URL
                $id = $_GET['id'];

                // get the recod from the database
                if($stmt = $mysqli->prepare("SELECT * FROM table_project WHERE id=?"))
                {
                    $stmt->bind_param("i", $id);
                    $stmt->execute();

                    $stmt->bind_result($id, $Title, $TextBody);
                    $stmt->fetch();

                    // show the form
                    renderForm($Title, $TextBody, NULL, $id);

                    $stmt->close();
                }
                // show an error if the query has an error
                else
                {
                    echo "Error: could not prepare SQL statement";
                }
            }
            // if the 'id' value is not valid, redirect the user back to the view.php page
            else
            {
                header("Location: index.php");
            }
        }
    }

?>
<? ob_flush(); ?>

我正在努力成为编码完美的网站,如果有一件事对我有帮助,谢谢你们都告诉我。

0 个答案:

没有答案