Jump to content


Photo

how to insert formatted text into MySQL database


  • Please log in to reply
2 replies to this topic

#1 dungpt29

dungpt29

    Member

  • Members
  • PipPip
  • 29 posts

Posted 28 March 2013 - 03:55 AM

I am trying to build a website based on:

1) MySQL 5.5.27

2) PHP 5.4.7

3) Zend Framework 1.12.0

 

I am buiding a form that contains a rich text editor YUI (Yahoo User Interface) as the following:

 

<form method="post"  action="<?php echo $this->baseUrl() . '/administration/index/?activemnu=mnuCon&subatmnu=mnuShoes'?>" id="frmAddNewShoes" name="frmAddNewShoes" enctype="multipart/form-data">
<table>
<tr>											             <td width="120" align="right" nowrap="nowrap" valign="top"><strong>Description:</strong></td>
    <td><textarea id="txtdescription" name="txtdescription"></textarea></td>    
</tr>    
<tr>
    <td align="center" valign="middle">
       <input type="button" id="btnAddNew" name="btnAddNew" value="Add New" onclick="btnAddNew_onclick();"/>
    </td>
</tr>
</table>
</form>

 

 

<script language="javascript" type="text/javascript">
//<![CDATA[

(function() { 
	    //Setup some private variables 
	    var Dom = YAHOO.util.Dom, 
	        Event = YAHOO.util.Event; 
	 
	        //The SimpleEditor config 
	        var myConfig = { 
	            height: '200px', 
	            width: '400px', 
	            dompath: true, 
	            focusAtStart: true,				 
		    animate: true,
		    handleSubmit: true
	        }; 
	 
	    //Now let's load the SimpleEditor.. 
	    var myEditor = new YAHOO.widget.SimpleEditor('txtdescription', myConfig); 
		myEditor._defaultToolbar.buttonType = 'basic';
	    myEditor.render(); 
	})(); 

function btnAddNew_onclick()
{
    $("#frmAddNewShoes").submit();
}
//]]>
</script>

 

 

Connecting to database to insert description into table as the following:

 

class Obj_connectDB
{
	protected static function ketnoiDB()
	{

		$options = array(Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE  => true);
		$params  = array(
    			'host'           =>  'localhost',
                        'username'       =>  'root',
                        'password'       =>  '',
                        'dbname'         =>  'zensr',
                        'options'        =>  $options);
		
		$db = Zend_Db::factory('Pdo_Mysql', $params);
		return $db;
	}

	public static function insertSubAvatar($param1)
	{
		$db = self::ketnoiDB();
		$stmt = new Zend_Db_Statement_Pdo($db,"call sp_insert_subproduct(:v_description)");                                                              															   																	     																		 																		 																		 																	$stmt->bindParam(":v_description", $param1, PDO::PARAM_LOB);
		$stmt->execute();
		$db->closeConnection();		
	}
}

 
class Application_Model_Admin
{
    public function insertSubAvatar($param1)
    {
        Obj_connectDB::insertSubAvatar($param1);
    }
}

// In AdministrationController.php
public function indexAction()
{ 
    $admin = new Application_Model_Admin();

    if ($this->_request->isPost())
    { 
        $txtdescription = $this->_request->getPost("txtdescription");
        $admin->insertSubAvatar($txtdescription);
    }
}

 

 

The table in database is as the following:

 

CREATE TABLE `zensr`.`shoes` (
  `description` BLOB NOT NULL
)
ENGINE = InnoDB;

 

 

The stored procedure is as the following:

 

DELIMITER $$

DROP PROCEDURE IF EXISTS `zensr`.`sp_insert_subproduct` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_insert_subproduct`(IN v_description BLOB)

BEGIN

  insert into shoes (description) values (v_description);

END $$

DELIMITER ;

 

 

Running the code, I find that description field is empty meaning it contains 0 Bytes when I input something to insert value of rich text editor into description field.

How do I edit PHP code to insert successfully?

Please help me solve this problem.

 

 

 

 



#2 annaharris

annaharris

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts

Posted 02 May 2013 - 07:11 AM

I don't know any accurate solution for this.
Just try this
<TEXTAREA WRAP="OFF" NAME="DESC" ROWS="15" COLS="60"></TEXTAREA>
(I don't change the formatting at all when writing to MySQL)

OUTPUT SCREEN:
print "<PRE>" . $db->row[0] . "</PRE>n";

Where $db->row[0] is what is returned from my query for DESC

I hope it may work



#3 dungpt29

dungpt29

    Member

  • Members
  • PipPip
  • 29 posts

Posted 02 May 2013 - 08:32 AM

Hi annaharris,

I found out the solution for this problem by myself weeks ago. And it works fine.

Thank you very much for your care, annaharris!

By the way, anyone who has the same problem can reply to me and I am ready to share my experience.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com