how to auto fill empty values of mysql with php?
10-27-2010, 03:57 PM
|
how to auto fill empty values of mysql with php?
|
Posts: 24
|
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_id` int(11) NOT NULL auto_increment, `title` varchar(32) collate utf8_bin NOT NULL default '', `code` varchar(3) collate utf8_bin NOT NULL default '', `top` char(1) collate utf8_bin NOT NULL, `bottom` char(1) NOT 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_id` int(11) NOT NULL auto_increment, `title` varchar(32) collate utf8_bin NOT NULL default '', `code` varchar(3) collate utf8_bin NOT NULL default '', `top` char(1) collate utf8_bin NOT NULL default '', `bottom` char(1) NOT 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..
|
|
|
|
10-27-2010, 04:08 PM
|
Re: how to auto fill empty values of mysql with php?
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
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.
Last edited by NullPointer; 10-27-2010 at 04:09 PM..
|
|
|
|
10-28-2010, 04:58 AM
|
Re: how to auto fill empty values of mysql with php?
|
Posts: 24
|
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_domain, 80, $errno, $errstr, 10); 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($fp, 128); } 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..
|
|
|
|
|
« Reply to how to auto fill empty values of mysql with php?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|