我在mysqli
插入时遇到了奇怪的行为。在 windows(localhost 开发 PC)上它工作正常,但在 Linux(共享主机)上,它没有按预期工作。
我将字段height
定义为VARCHAR(10)
. 使用SELECT
语句读取数据时:
5' 6"
显示为5\' 6\"
表结构:
CREATE TABLE IF NOT EXISTS `user_details` (
`uname` varchar(20) NOT NULL,
`height` varchar(10) NOT NULL,
`body_type` varchar(20) NOT NULL,
`hair_color` varchar(20) NOT NULL,
`eye_color` varchar(20) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`uname`),
UNIQUE KEY `uname` (`uname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
读取数据的代码
$statement = $mysqli->prepare("SELECT * from user_details WHERE uname = ?");
$statement->bind_param("s", $uname);
$statement->execute();
if ($statement->bind_result($uname, $height, $body, $hair, $eye, $desc) && $statement->fetch())
{
// work with data.
}
用于 INSERT 的代码
保存数据.php
$statement = $mysqli->prepare("INSERT INTO user_details VALUES(?, ?, ?, ?, ?, ?)");
$statement->bind_param("ssssss", $_POST['uname'], $_POST['height'], $_POST['body'], $_POST['hair'], $_POST['eye'], $_POST['description']);
if ($statement->execute())
{
echo "<div>Username successfully added</div>";
}
保存数据.html
<form method="POST" enctype="multipart/form-data">
<table>
<tr><td>Name (Must be unique) : </td><td><input type="text" name="uname"></td></tr>
<tr><td>Profile Image : </td><td><input type="file" name="pic"></td></tr>
<tr><td>Height : </td><td><input type="text" name="height"></td></tr>
<tr><td>Body Type: </td><td><input type="text" name="body"></td></tr>
<tr><td>Hair Color: </td><td><input type="text" name="hair"></td></tr>
<tr><td>Eye Color: </td><td><input type="text" name="eye"></td></tr>
<tr><td>Description: </td><td><textarea name="description" rows="5"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" value="Save New User"></td></tr>
</table>
</form>
在 Linux 与 Windows 上显示
Linux
视窗