Jump to content

how to insert formatted text into MySQL database


dungpt29

Recommended Posts

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.

 

 

 

 

  • 1 month later...

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.