| 664 | | return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')"); |
| | 673 | |
| | 674 | $insertfields = $insertfieldtypes = $prepbits = array(); |
| | 675 | |
| | 676 | $types = $this->prepare_columns($table, $keys = array_keys($data)); |
| | 677 | |
| | 678 | foreach( (array)$data as $field => $piece ) { |
| | 679 | $insertfields[] = $field; |
| | 680 | $insertfieldtypes[] = $types[$field]; |
| | 681 | $prepbits[] = $piece; |
| | 682 | } |
| | 683 | |
| | 684 | $sql = 'INSERT INTO ' . $table; |
| | 685 | $sql .= ' (`' . implode('`,`', $insertfields) . '`)'; |
| | 686 | $sql .= ' VALUES (' . implode(',', $insertfieldtypes) . ')'; |
| | 687 | |
| | 688 | $sql = call_user_func_array(array(&$this, 'prepare'), array_merge(array($sql), $prepbits)); |
| | 689 | |
| | 690 | return $this->query( $sql ); |
| 689 | | return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) ); |
| | 709 | $sql = "UPDATE $table SET "; |
| | 710 | |
| | 711 | $types = $this->prepare_columns($table, array_merge(array_keys($data), array_keys($where)) ); |
| | 712 | |
| | 713 | foreach ( array_keys($data) as $k ) { |
| | 714 | $bits[] = "`$k` = " . $types[$k]; |
| | 715 | $prepbits[] = $data[$k]; |
| | 716 | } |
| | 717 | |
| | 718 | $sql .= implode( ', ', $bits ); |
| | 719 | |
| | 720 | $sql .= ' WHERE '; |
| | 721 | |
| | 722 | foreach ( array_keys($where) as $k ) { |
| | 723 | $wheres[] = "`$k` = " . $types[$k]; |
| | 724 | $prepbits[] = $where[$k]; |
| | 725 | } |
| | 726 | $sql .= implode( ' AND ', $wheres ); |
| | 727 | |
| | 728 | $sql = call_user_func_array(array(&$this, 'prepare'), array_merge(array($sql), $prepbits)); |
| | 729 | |
| | 730 | return $this->query( $sql ); |
| | 734 | * Determines a table column type sprintf()-style token |
| | 735 | * |
| | 736 | * Uses values cached from the previous queries, |
| | 737 | * Loads any column types with a simple SELECT .. LIMIT 1 of any fields which are not allready loaded. |
| | 738 | * |
| | 739 | * @param string $table WARNING: not sanitized! |
| | 740 | * @param array $cols array of columns to prepare. WARNING: not sanitized! |
| | 741 | * @return array of key => value pairs of field => token |
| | 742 | */ |
| | 743 | function prepare_columns($table, $cols) { |
| | 744 | //Load any columns which are not allready cached. |
| | 745 | $load_cols = array(); |
| | 746 | foreach ( (array) $cols as $col ) |
| | 747 | if ( ! isset($this->column_types[$table][$col]) ) |
| | 748 | $load_cols[] = $col; |
| | 749 | |
| | 750 | if ( !empty($load_cols) ) { |
| | 751 | $load_cols = implode('`,`', $load_cols); |
| | 752 | $this->query("SELECT `$load_cols` FROM $table LIMIT 1"); |
| | 753 | } |
| | 754 | |
| | 755 | $return = array(); |
| | 756 | foreach ( (array) $cols as $col ) { |
| | 757 | switch ( $this->column_types[$table][$col] ) { |
| | 758 | case 'int': |
| | 759 | $return[$col] = '%d'; |
| | 760 | break; |
| | 761 | case 'real': |
| | 762 | $return[$col] = '%f'; |
| | 763 | break; |
| | 764 | case 'date': |
| | 765 | case 'datetime': |
| | 766 | case 'string': |
| | 767 | case 'blob': |
| | 768 | default: |
| | 769 | $return[$col] = '%s'; |
| | 770 | break; |
| | 771 | } |
| | 772 | } |
| | 773 | return $return; |
| | 774 | } |
| | 775 | |
| | 776 | /** |