php无法将值传入数据库,PHP-无法将句子插入数据库

我有几句话.我必须选择包含6个以上单词的句子.然后将它们插入数据库.

require_once 'conf/conf.php';

$text = " Poetry. Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";

$sentences = explode(".", $text);

foreach ($sentences as $sentence) {

if (count(preg_split('/\s+/', $sentence)) > 6) {

$save = $sentence. ".";

$sql = mysql_query("INSERT INTO tb_name VALUES('','$save')");

}

}

?>

结果只是插入数据库=>中的第二句话. ‘你在飞行时读诗吗?许多人发现在长途飞行中阅读很轻松.而第三句也应该插入.请帮助我,谢谢:)

解决方法:

这是您要寻找的解决方案.您不能添加多行,因为您的ID值未指定,这是表的关键.由于要将句子添加到同一行,因此需要执行一个查询.

$text = " Poetry. Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";

$sentences = explode(".", $text); $save = array();

foreach ($sentences as $sentence) {

if (count(preg_split('/\s+/', $sentence)) > 6) {

$save[] = $sentence. ".";

}

}

if( count( $save) > 0) {

$sql = mysql_query("INSERT INTO tb_name VALUES('','" . implode( ' ', $save) . "')");

}

现在,两个句子将插入到数据库的同一行中,并以空格分隔.如果将第一个参数修改为implode(),则可以更改它们的分隔符.

生成的查询是这样的:

INSERT INTO tb_name VALUES('',' Do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories.')

标签:mysql,php

来源: https://codeday.me/bug/20191101/1981472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值