我遇到了一个大麻烦。我有一个控制器方法接收以JSON格式化的POST请求。
当我保存JSON数据时,MySQL中的一个长文本字段不能保存。但是如果我写短文,比如" Hello World"它节省了DB。我不知道这里有什么问题。
这是我的Javascript代码:
$http({
url: 'http://customblahblahblah',
method: 'POST',
data: "user_id="+ user_id +
"&token="+ token +
"&object="+angular.toJson(myobject),....
这是我的PHP代码:
public function save(){
if($this->request->is('post')){
if($this->checkUser($this->request->data['user_id'], $this->request->data['token'])){
$myObject = json_decode($this->request->data['object'], true);
if($this->MyObject->save($myObject)){
.....
}
....
}
}
}
这里有什么问题?
因为如果在属性中设置了类似" Hello World"的文本。它仍然保存,但如果我发送长文本,如报纸文章似乎保存(因为没有抛出异常,而Model-> save()返回true)但数据不是保存。
答案 0 :(得分:0)
您正在将json字符串解码为对象并尝试在文本字段中保存对象。我认为您需要将其作为JSON字符串存储在数据库中,然后在阅读时对其进行解码。
尝试:
<?php
$servername = "localhost";
$username = "root";
$password = "cornwall";
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Type of Activity</th>
<th>Employer</th>
<th>Date</th>
<th>Time</th>
<th>Amount (GBP)</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["typeofactivity"]."</td>
<td>".$row["employer"]."</td>
<td>".$row["date"]."</td>
<td>".$row["time"]."</td>
<td>".$row["amount"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$con->close();
?>
如果您仍然遇到问题,可能是数据库数据类型。在过去,我只使用if($this->MyObject->save(json_encode($myObject))){
.....
}
作为json字符串的数据类型。
答案 1 :(得分:0)
我已经过去了! 该列必须为LONGTEXT或LONGBLOB(可以使用的最大尺寸)
如果使用迁移,则需要创建
$table = $this->table('your_table');
$table->changeColumn('your_column', 'text', ['null' => true, 'limit' => MysqlAdapter::TEXT_LONG]);
$table->update();
您可以在这里看到:http://docs.phinx.org/en/latest/migrations.html#limit-option-and-mysql