Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to auto fill empty values of mysql with php?
Old 10-27-2010, 03:57 PM how to auto fill empty values of mysql with php?
Average Talker

Posts: 24
Trades: 0
Hi,

I have mysql table that i want to auto fill the empty values with php code, is it possible?

the table:

PHP Code:
CREATE TABLE IF NOT EXISTS `Countries` (
  `
num_idint(11NOT NULL auto_increment,
  `
titlevarchar(32collate utf8_bin NOT NULL default '',
  `
codevarchar(3collate utf8_bin NOT NULL default '',
  `
topchar(1collate utf8_bin NOT NULL,
  `
bottomchar(1NOT NULL,
  
PRIMARY KEY  (`num_id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1; ;

INSERT INTO `Countries` (`num_id`, `title`, `code`, `top`, `bottom`) VALUES
(1'''CAN'''''),
(
2'''ESP'''''),
(
3'''USA'''''); 
E.g.

if code = CAN then fill the title = Canada and top = 1
if code = ESP then fill the title = Spayol and top = 1
if code = USA then fill the title = United State and bottom = 1

so finally will be look like:

PHP Code:
CREATE TABLE IF NOT EXISTS `Countries` (
  `
num_idint(11NOT NULL auto_increment,
  `
titlevarchar(32collate utf8_bin NOT NULL default '',
  `
codevarchar(3collate utf8_bin NOT NULL default '',
  `
topchar(1collate utf8_bin NOT NULL default '',
  `
bottomchar(1NOT NULL default '',
  
PRIMARY KEY  (`num_id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1; ;

INSERT INTO `Countries` (`num_id`, `title`, `code`, `top`, `bottom`) VALUES
(1'Canada''CAN''1'''),
(
2'Spayol''ESP''1'''),
(
3'United State''USA''''1'); 
your help and pointer / example appreciated and thanks in advance

Last edited by jones; 10-27-2010 at 04:01 PM..
jones is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-27-2010, 04:08 PM Re: how to auto fill empty values of mysql with php?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Just populate the empty fields prior to executing the insert query:
PHP Code:
$title '';
$top '';
$bottom '';
if(
$code == 'CAN')
{
     
$title 'Canada';
     
$top 1;
}
elseif(
$code == 'ESP')
{
     
$title 'Spayol';
     
$top 1;
}
elseif(
$code == 'USA')
{
     
$title 'United States';
     
$bottom 1;
}

$query "INSERT INTO `Countries` (`num_id`, `title`, `code`, `top`, `bottom`) VALUES (1, '$title', '$code', '$top', '$bottom')"
Edit:
As always make sure you validate and sanitize any user data prior to inserting it into a query.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-27-2010 at 04:09 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-28-2010, 04:58 AM Re: how to auto fill empty values of mysql with php?
Average Talker

Posts: 24
Trades: 0
I appreciated your help NullPointer,

then if i have php code:

PHP Code:
   function downloadValueCountries() {   
      
$code_domain substr($this->xml_file,0,strpos($this->xml_file,"/"));
      
$code_file substr($this->xml_file,strpos($this->xml_file,"/"));
      
$fp = @fsockopen($code_domain80$errno$errstr10);
      if(
$fp) {
         
$out "GET ".$code_file." HTTP/1.1\r\n";
         
$out .= "Host: ".$code_domain."\r\n";
         
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5\r\n";
         
$out .= "Connection: Close\r\n\r\n";
         
fwrite($fp$out);
         while (!
feof($fp)) {
            
$buffer .= fgets($fp128);
         }
         
fclose($fp);
 
         
$pattern "{<Cube\s*code='(\w*)'\s*temperature='([\d\.]*)'/>}is";
         
preg_match_all($pattern,$buffer,$xml_count);
         
array_shift($xml_count);
 
         for(
$i=0;$i<count($xml_count[0]);$i++) {
            
$exchange_temperature[$xml_count[0][$i]] = $xml_count[1][$i];
         }
 
         
$conn mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
         
$rs mysql_select_db($this->mysql_db,$conn);
            
         foreach(
$exchange_temperature as $code=>$temperature) {
            if((
is_numeric($temperature)) && ($temperature != 0)) {
               
$sql "SELECT * FROM ".$this->mysql_table." WHERE code='".$code."'";
               
$rs =  mysql_query($sql,$conn) or die(mysql_error());
               if(
mysql_num_rows($rs) > 0) {
                  
$sql "UPDATE ".$this->mysql_table." SET temperature=".$temperature.",title=".$title.",bottom='".$bottom."',top='".$top."' WHERE code='".$code."'";
               } else {
                  
$sql "INSERT INTO ".$this->mysql_table." VALUES(NULL,'".$title."','".$code."',".$temperature.",'".$bottom."','".$top."')";
               }
               
$rs =  mysql_query($sql,$conn) or die(mysql_error());
            }
         }   
      }
   }

   
/* Create the Countries table */
   
function createTable() {
      
$conn mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass);
      
$rs mysql_select_db($this->mysql_db,$conn);
       
$sql "CREATE TABLE ".$this->mysql_table." ( `num_id` int(11) NOT NULL auto_increment, `title` varchar(32) collate utf8_bin NOT NULL default '', `code` char(3) NOT NULL default '', `temperature` float(15,8) NOT NULL, `bottom` char(1) collate utf8_bin NOT NULL default '',`top` char(1) collate utf8_bin NOT NULL default '', PRIMARY KEY(num_id) ) ENGINE=MyISAM";
      
$rs =  mysql_query($sql,$conn) or die(mysql_error());
      
$sql "INSERT INTO ".$this->mysql_table." VALUES(1,'','USA',22,'','')";
      
$rs =  mysql_query($sql,$conn) or die(mysql_error());
      
$this->downloadValueCountries();
   } 
then where i should place your code, i was try several ways to put your code then not work properly

Many thanks for your help.

Last edited by jones; 10-28-2010 at 05:04 AM..
jones is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how to auto fill empty values of mysql with php?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.40166 seconds with 12 queries