Welcome Guest, Not a member yet? Register   Sign In
Simple form, crazy error
#1

[eluser]RaZoR LeGaCy[/eluser]
Hi I have in my controller:
Code:
function submit()
    {
                
$rules['title']    = "trim|required|xss_clean|prep_for_form|htmlspecialchars";
$rules['date']    = "trim|required|xss_clean|prep_for_form|htmlspecialchars";
$rules['desc']    = "trim|xss_clean|prep_for_form|htmlspecialchars";
        
$this->validation->set_rules($rules);
        
$fields['title'] = 'title';
$fields['date'] = 'date';
$fields['desc'] = 'desc';

$this->validation->set_fields($fields);


        if ($this->validation->run() == FALSE)
        {

#Body View
echo '<center><p>There was an <strong>Error</strong> with your comment.</p>';
echo '<p>Please re-submit your entry!</p>';

        }
        else
        {

if (!empty($_POST['dvd'])) {
$dvd = '1';
} else {
$dvd = '0';
}

if (!empty($_POST['theater'])) {
$theater = '1';
} else {
$theater = '0';
}

if (!empty($_POST['game'])) {
$game = '1';
} else {
$game = '0';
}

        
$data = array(
               'id' => 'NULL' ,
               'title' => $this->input->post('title', TRUE) ,
               'desc' => $this->input->post("desc", TRUE) ,
               'date' => $this->input->post('date', TRUE) ,
               'dvd' =>  ''.$dvd.'' ,
               'theater' => ''.$theater.'' ,
               'game' => ''.$game.'');

$this->db->insert('hh_events', $data);

redirect('razor/events/');

}

}

In my View:
Code:
<div class="storyBar">
<h1>Events</h1>

<center>
&lt;?=$this->validation->error_string; ?&gt;

&lt;?=form_open('razor/events/submit');?&gt;

<table align="center" style="margin:2em auto;">
<tr valign="baseline">
<td colspan="2" align="center">
DVD: &lt;? echo form_checkbox('dvd', '1', FALSE); ?&gt; |
Theater: &lt;? echo form_checkbox('theater', '1', FALSE); ?&gt; |
Game: &lt;? echo form_checkbox('game', '1', FALSE); ?&gt;
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap">Title:</td>
<td>&lt;?
$data = array('name' => 'title', 'id' => 'title', 'value' => '', 'maxlength' => '128', 'size' => '50', 'style' => 'width:50%',);
echo form_input($data);
?&gt;</td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap">Date:</td>
<td>&lt;?
$data = array('name' => 'date', 'id' => 'date', 'value' => '2007-00-00', 'maxlength' => '10', 'size' => '15', 'style' => 'width:50%',);
echo form_input($data);
?&gt; YYYY-MM-DD</td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap">Desc:</td>
<td>&lt;? $data = array('name' => 'desc', 'id' => 'comment', 'cols' => '40', 'rows' => '3',);
echo form_textarea($data);?&gt;</td>
</tr>
<tr valign="baseline">
<td colspan="2" align="center">

&lt;?=form_submit('submit', 'Submit');?&gt;</td></tr>
</table>
&lt;/form&gt;
</center>

</div>

In my DB structure:
Code:
-- phpMyAdmin SQL Dump
-- version 2.10.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 27, 2007 at 02:21 PM
-- Server version: 5.0.27
-- PHP Version: 4.4.4

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `gainfina_ciHH`
--

-- --------------------------------------------------------

--
-- Table structure for table `hh_events`
--

CREATE TABLE `hh_events` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(128) collate utf8_unicode_ci NOT NULL default '',
  `desc` text collate utf8_unicode_ci,
  `date` date NOT NULL default '0000-00-00',
  `dvd` enum('1','0') collate utf8_unicode_ci NOT NULL default '0',
  `theater` enum('1','0') collate utf8_unicode_ci NOT NULL default '0',
  `game` enum('1','0') collate utf8_unicode_ci NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='release_dates' AUTO_INCREMENT=36 ;

--
-- Dumping data for table `hh_events`
--

INSERT INTO `hh_events` VALUES (1, 'Skinwalkers', NULL, '2007-07-27', '0', '1', '0');
INSERT INTO `hh_events` VALUES (2, 'Shadow Puppets', NULL, '2007-07-24', '1', '0', '0');
INSERT INTO `hh_events` VALUES (19, 'Perfect Creature', NULL, '2007-07-17', '1', '', '');
INSERT INTO `hh_events` VALUES (20, 'Captivity', NULL, '2007-07-13', '', '1', '');
everything looks good but I get this error when I submit
Code:
An Error Was Encountered

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, date, dvd, theater, game) VALUES ('NULL', 'the2', 'te 2\'s', '2007-08-14',' at line 1

INSERT INTO hh_events (id, title, desc, date, dvd, theater, game) VALUES ('NULL', 'the2', 'te 2\'s', '2007-08-14', '0', '1', '0')
#2

[eluser]Phil Sturgeon[/eluser]
That is a strange one. Even if it doesn't fix, simplify your code a bit.

Code:
function submit()
    {
                
$rules['title']    = "trim|required|xss_clean|prep_for_form|htmlspecialchars";
$rules['date']    = "trim|required|xss_clean|prep_for_form|htmlspecialchars";
$rules['desc']    = "trim|xss_clean|prep_for_form|htmlspecialchars";
        
$this->validation->set_rules($rules);
        
$fields['title'] = 'title';
$fields['date'] = 'date';
$fields['desc'] = 'desc';

$this->validation->set_fields($fields);


        if ($this->validation->run() == FALSE)
        {

#Body View
echo '<center><p>There was an <strong>Error</strong> with your comment.</p>';
echo '<p>Please re-submit your entry!</p>';

        }
        else
        {

$data = array(
               'title' => $this->input->post('title', TRUE) ,
               'desc' => $this->input->post("desc", TRUE) ,
               'date' => $this->input->post('date', TRUE) ,
               'dvd' =>  (!empty($this->input->post('dvd'))) ? 1 : 0,
               'theater' => (!empty($this->input->post('theater'))) ? 1 : 0,
               'game' => (!empty($this->input->post('game'))) ? 1 : 0

$this->db->insert('hh_events', $data);

if($this->db->affected_rows() > 0):
redirect('razor/events/');
else:
echo "ERROR LAME!!"
endif;

}

}

Also you might want to list all fields that are used in validation, even if the rules are only "trim".

Not sure what the actual error is though :p
#3

[eluser]RaZoR LeGaCy[/eluser]
Thanks pyro

but I now get

Fatal error: Can't use method return value in write context in /system/application/controllers/razor/events.php on line 68

line 68 starts at
Code:
'dvd' =>  (!empty($this->input->post('dvd')) ? 1 : 0),
#4

[eluser]Rick Jolly[/eluser]
A couple of things:

Code:
$data = array(
               ...
               'dvd' =>  ''.$dvd.'' ,
               'theater' => ''.$theater.'' ,
               'game' => ''.$game.'');

// do this instead
$data = array(
               ...
               'dvd' =>  $dvd ,
               'theater' => $theater ,
               'game' => $game);

You don't need to quote your data since active record will do that for you.

Also 'desc' is a key word in mysql so you might have to change that column name.
#5

[eluser]RaZoR LeGaCy[/eluser]
dude

You hit the nail on the head.

I replaced desc with text and now everything works perfectly.

Then I did the array the same as you said to and now the script works great.

Thanks all and especially Rick

Mark as solved MOD
#6

[eluser]Phil Sturgeon[/eluser]
Code:
'dvd' =>  (!empty($this->input->post('dvd')) ? 1 : 0),

should be

Code:
'dvd' =>  ($this->input->post('dvd') ? 1 : 0),

and ofc the same for the others. Sorry about that!
#7

[eluser]Iksander[/eluser]
Yeah, watch out for those keywords.




Theme © iAndrew 2016 - Forum software by © MyBB