使用Ajax和PHP将数据插入MySQL时,获取有限的数据?

时间:2015-09-30 02:17:28

标签: php jquery ajax mysqli

使用Ajax和PHP将数据插入MySQL时,从富文本区域获取有限的数据,有什么问题吗?

JQuery脚本

   $('#adddesc').click(function(e)
    {
        e.preventDefault();

    var txtcategoryname=tinyMCE.get('txtcategoryname').getContent();
    var txttitle=$('#txttitle').val();
    var selectError1=$("#selectError1").val();
    var selectError2=$("#selectError2").val();
    var selectError3=$("#selectError3").val();
    //var fimage=$("#fimage").val();
    //alert($("#selectError2").val().length);
var dataString;
var err;
err=(txttitle!='' && txtcategoryname!='' &&  $("#selectError1").val()!='0' &&  $("#selectError2").val()!='0' &&  $("#selectError3").val()!='0')?'0':'1';
// var dataString1="txttitle="+txttitle+"& description="+txtcategoryname+"& catname="+selectError1+"& tags="+selectError2;
//dataString1="txttitle="+txttitle+"& description="+txtcategoryname+"& catname="+selectError1+"& tags="+selectError2+"& subcat="+selectError3;
//  alert(dataString1);

    if(err=='0')
    {
    dataString="txttitle="+txttitle+"& description="+txtcategoryname+"& catname="+selectError1+"& tags="+selectError2+"& subcat="+selectError3;
    //alert(dataString);
                $.ajax({
                               type: "POST",
                               url: "aAddDescription.php",
                               data: dataString,
                              cache: true,
                              beforeSend: function(){ $("#adddesc").val('Adding Des.....');},
            success: function(html){

     //$("#txtcategoryname").val('');
     tinyMCE.get('txtcategoryname').setContent('');
     $("#txttitle").val('');
      //$("#selectError1").get(0).selectedIndex = 0;
        //$("#error").removeClass("alert alert-error");
    $("#error").addClass("alert alert-success");
 $("#error").html("<span style='color:#cc0000'>Success:</span> Description Added Successfully. ").fadeIn().delay(3000).fadeOut();

            }
            });

HTML表单脚本

                        <form class="form-horizontal" method="POST" action="" enctype="multipart/form-data"  autocomplete="off">
                            <fieldset>
                               <div class="control-group">
                                <label class="control-label" for="selectError">Select Cateogry</label>
                                <div class="controls">
                                                    <?php
$result=mysqli_query($db,"SELECT * FROM categories ");
//$count=mysqli_num_rows($result);
$op="<option value='0'>Select Category</option>";
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$op.="<option value='".$row['id']."'>".$row['title']."</option>";
}
?>
                                  <select id="selectError1" >
                                    <?php echo $op; ?>
                                  </select>
                                </div>
                              </div>


             <div class="control-group">
                                <label class="control-label" for="selectError">Select Sub Cateogry</label>
                                <div class="controls">

                                  <select id="selectError3">
                                <option selected="selected" value="0">--Select Sub--</option>
                                  </select>
                                </div>
                              </div>                











                               <div class="control-group">
                                           <label class="control-label" for="focusedInput">Enter Title:: </label>
    <div class="controls">                                          
<input class="form-control"  type="text" id="txttitle" name="txttitle" value=""  placeholder="Enter Title"><span id="user-availability-status"></span> <img src="LoaderIcon.gif" id="loaderIcon" style="display:none;width:20px;height:20px;" />
                                       </div></div>

                                           <div class="control-group">


    <textarea rows="10" cols="20" name="content" style="width:100%; height:150px" id="txtcategoryname"></textarea>                                     

                                       </div>

                                       <div class="control-group">
                                <label class="control-label" for="selectError1">Tags(select All with Press Ctrl)</label>
                                <div class="controls">
                                    <?php
$result=mysqli_query($db,"SELECT * FROM tags ");
//$count=mysqli_num_rows($result);
$op1='';
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$op1.="<option value='".$row['title']."'>".$row['title']."</option>";
}
?>
                                  <select id="selectError2" multiple >
                                    <?php //echo $op1; ?>
                                  </select>
                                </div>
                              </div>









                              <div class="form-actions">

                                <button type="submit" class="btn btn-primary" id="adddesc">Save changes</button>
                                <button class="btn">Cancel</button>
                              </div>
                            </fieldset>
                        </form>

aAddDescription.php

<?php
include("common/db.php");
session_start();
if(isSet($_POST['description']) && isSet($_POST['txttitle']))
{
// username and password sent from Form
$description=$_POST['description']; 
$txttitle=mysqli_real_escape_string($db,$_POST['txttitle']); 
$catname=mysqli_real_escape_string($db,$_POST['catname']); 
$subcat=mysqli_real_escape_string($db,$_POST['subcat']); 
$tags=mysqli_real_escape_string($db,$_POST['tags']);
//$fimage=$_FILES['fimage']['name'] ;
$cby=$_SESSION['login_user']; 
//$result=mysqli_query($db,"SELECT * FROM categories WHERE title='$categoryname'");
//$count=mysqli_num_rows($result);
//$target_dir = "uploads/";
//$target_file = $target_dir.$_FILES['fimage']['name'];
//move_uploaded_file($_FILES['fimage']['tmp_name'],$target_file);


//$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
// If result matched $myusername and $mypassword, table row must be 1 row
/*if($count>0)
{
echo "0";
}
else
{
mysqli_query($db,"INSERT INTO  categories(title) VALUES('".$categoryname."')");
echo "1";
}*/
//date_default_timezone_set('Asia/Delhi');

mysqli_query($db,"INSERT INTO  description(title,description,cat_id,tags_id,created,modified,createdby,modifiedby,subcat_id) VALUES('".$txttitle."','".$description."','".$catname."','".$tags."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."','".$cby."','".$cby."','".$subcat."')");
$cid=mysqli_insert_id($db);
$cresult=mysqli_query($db,"SELECT * FROM counter WHERE cont_id='$cid'");
$ccount=mysqli_num_rows($cresult);

if($ccount==0)
{
mysqli_query($db,"INSERT INTO  counter(cont_id) VALUES(".$cid.")");
}
echo "1";
}
?>

请帮我解决这段代码中的任何错误?...即使我将缓存更改为false也会遇到同样的问题。如果我输入1000 /更少或更多的行数据,它总是采用有限的数据。请帮我。在此先感谢

1 个答案:

答案 0 :(得分:0)

我愿意打赌你的文字中包含干扰网址的字符,如“&amp;”,“?”,“/”,“=”等。

您应该使用{{3}}对文字进行编码,如下所示:

var txtcategoryname=encodeURIComponent(tinyMCE.get('txtcategoryname').getContent());
var txttitle=encodeURIComponent($('#txttitle').val());
var selectError1=encodeURIComponent($("#selectError1").val());
var selectError2=encodeURIComponent($("#selectError2").val());
var selectError3=encodeURIComponent($("#selectError3").val());