- Messages
- 1,682
- Reaction score
- 32
- Points
- 48
PHP:
<?php
define ('ROOT', '../');
include ROOT.'config.php';
if(isset($_POST['submit'])) {
$chapters = $_POST['chapters'];
$class = $_POST['class'];
$chapters = str_replace('課', '課 <br />', $chapters);
$chaptersRefined = str_replace('課', '', $chapters);
$chaptersRefined = str_replace('第', '', $chaptersRefined);
$chaptersArray = explode('<br />', $chapters);
//echo '<br />';
//echo $chaptersArray[0];
//echo '<br />';
$chaptersRefinedArray = explode('<br />', $chaptersRefined);
//echo '<br />';
//echo $chaptersRefinedArray[0];
$i = 0;
$max = (count($chaptersArray))-1;
while($i < $max) {
if($i == $max-1) {
$values .= "('".$class."', '".$chaptersRefinedArray[$i]."', '".$chaptersArray[$i]."')";
} else {
$values .= "('".$class."', '".$chaptersRefinedArray[$i]."', '".$chaptersArray[$i]."'),";
}
$i++;
}
//echo $values;
$sql = 'INSERT INTO '.dbPre.'textbook (class, sTitle, fTitle) VALUES '.$values;
//echo '<br />';
//echo $sql;
if((mysql_query($sql)) or die(mysql_error()))
echo 'Entered: <br /><br />';
echo '<br />';
echo '<br />';
echo $class;
echo '<br />';
echo '<br />';
echo $chapters;
echo '<br />';
echo $chaptersRefined;
} else {
echo '<form name="book" method="post" action="#"><input type="text" name="class" /><br /><textarea cols="50" rows="4" name="chapters"></textarea><br /><input type="submit" name="submit" value="Submit"></form>';
}
?>
here is my code. I enter in the class and chapter list, then the script cleans it all up and prepares it to enter it into the database.
It enters the information into the db, the only problem is: it enters it in as unicode and not in chinese.
my connect.php
PHP:
<?
// connect to the database server
if (!($db = mysql_pconnect($dbPort, $dbUsername , $dbPassword))){
die("Can't connect to database server.");
} else {
// set character encoding
mb_internal_encoding("UTF-8");
mysql_query('SET NAMES "utf8"');
// select a database
if (!(mysql_select_db($dbName, $db))){
die("Can't connect to database.");
}
}
?>