Weird MySQL Problem
-
Hello,
I’ve just been working on a component, that saves stuff to the database. That is working correctly, but it only saves numbers. When there is text it converts that to 0.
This is the code I use to create the database tables:
// add charset & collate like wp core
$charset_collate = '';
if ( version_compare(mysql_get_server_info(), '4.1.0', '>') ) {
if ( ! empty($wpdb->charset) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty($wpdb->collate) )
$charset_collate .= " COLLATE $wpdb->collate";
}
$sql[] = "CREATE TABLE {$bp->skools->table_name_courses} (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
group_id BIGINT(20) NOT NULL,
course_name VARCHAR(255) NOT NULL,
course_desc LONGTEXT NOT NULL,
course_price BIGINT(20) NOT NULL,
course_group VARCHAR(255) NOT NULL,
PRIMARY KEY id (id)
) {$charset_collate};";And this is the code I use to save to the database:
function skools_save_course( $groupid, $name, $desc, $price, $group )
{
$course = new DB_skool_courses;
$course->group_id = $groupid;
$course->course_name = $name;
$course->course_desc = $desc;
$course->course_price = $price;
$course->course_group = $group;
if( $course->save() )
return true;
else
return false;
}I basically started from the skeleton component and adjusted it a bit. This is the bit from the database class:
function save()
{
global $wpdb, $bp;
$this->group_id = apply_filters( 'courses_data_group_id_before_save', $this->group_id, $this->id );
$this->course_name = apply_filters( 'courses_data_course_name_before_save', $this->course_name, $this->id );
$this->course_desc = apply_filters( 'courses_data_course_desc_before_save', $this->course_desc, $this->id );
$this->course_price = apply_filters( 'courses_data_course_price_before_save', $this->course_price, $this->id );
$this->course_group = apply_filters( 'courses_data_course_group_before_save', $this->course_group, $this->id );
/* Call a before save action here */
do_action( 'courses_data_before_save', $this );
if ( $this->id ) {
// Update
$result = $wpdb->query( $wpdb->prepare(
"UPDATE {$bp->skools->table_name_courses} SET
group_id = %d,
course_name = %d,
course_desc = %d,
course_price = %d,
course_group = %d
WHERE id = %d",
$this->group_id,
$this->course_name,
$this->course_desc,
$this->course_price,
$this->course_group,
$this->id
) );
} else {
// Save
$result = $wpdb->query( $wpdb->prepare(
"INSERT INTO {$bp->skools->table_name_courses} (
group_id,
course_name,
course_desc,
course_price,
course_group
) VALUES (
%d, %d, %d, %d, %d
)",
$this->group_id,
$this->course_name,
$this->course_desc,
$this->course_price,
$this->course_group
) );
}
if ( !$result )
return false;
if ( !$this->id ) {
$this->id = $wpdb->insert_id;
}
/* Add an after save action here */
do_action( 'courses_data_after_save', $this );
return $result;
}I’m using the latest SVN of both BP and WPMU.
I could really need some help figuring out why it won’t let me save text to the database.
Cheers,
Boris
- The topic ‘Weird MySQL Problem’ is closed to new replies.