The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

double values in table

Discussion in 'Database Discussions' started by Benko, Nov 3, 2014.

  1. Benko

    Benko Registered

    Joined:
    Oct 31, 2014
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    since 5 days I'm using cpanel.domain.net.

    I'm using a php (code see below) to store (maximum) 100 values in a table. If I write
    a value I'm using...
    mySubdomainName.domain.net/test.php?action=write&wert=72

    -When I call the table using...
    mySubdomainName.domain.net/test.php?action=read
    I notice each value in the databeas is double

    sample for a table with double values:
    Code:
    POS	WERT	ZEIT
    1	72	2014-11-03 01:19:49
    2	72	2014-11-03 01:19:49
    3	70	2014-11-03 01:06:34
    4	70	2014-11-03 01:06:33
    -Is there an error on the domain server or why are the values double ?

    Table :
    Code:
    CREATE TABLE IF NOT EXISTS `wertetabelle` ( 
      `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
      `wert` bigint(20) NOT NULL, 
      `zeit` datetime NOT NULL, 
      PRIMARY KEY (`ID`) 
    ) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
    PHP :
    Code:
    <?php
    $action = (isset($_GET['action']) && $_GET['action'] != "") ? $_GET['action'] : '';
    $wert = (isset($_GET['wert']) && $_GET['wert'] != "")? $_GET['wert'] : '';
    
    // different 'action'-URL Parameters
    switch($action){
    	// add Variable to data base
    	case 'write':
    		// if value is not EMPTY
    		if ($wert != ''){
    			// store the time
    			$zeit = date("Y-m-d H:i:s");
    			try{
    				//connect to database
    				$db = connect_db();
    				//SQL Insert-Call
    				$proc = $db->prepare("INSERT INTO wertetabelle(wert,zeit) Values(?,?)");
    				//give the values as a parameter to the calling routine and process it
    				$count = $proc->execute(array($wert,$zeit));
    				if ($count > 0){
    					//if entry was made show a text phrase
    					echo "Value '$wert' and time '$zeit' added to database";
    				}
    				
    			}catch (PDOException $ex) {
    				echo "error on calling the data: " . $ex;
    			}
    		}else{
    			// value was empty, show error phrase
    			echo "There was no value";	
    		}
    		break;
    		
    	// read data from database
    	case 'read':
    		try{
    			//connect to data base
    			$db = connect_db();
    			//Counter for table
    			$cnt = 1;
    			$rows = '';
    			// read Data from DB (line by line) 
    			foreach($db->query("SELECT * FROM wertetabelle ORDER BY zeit DESC LIMIT 100") as $row){
    				$rows .= '<tr><td>' . $cnt++ . "</td><td>" . $row['wert'] . "</td><td>" . $row['zeit'] ."</td></tr>\n";
    			}
    			//echo the table
    			echo '<table style="border:1px solid gray"><tr><th>POS</th><th>WERT</th><th>ZEIT</th></tr>';
    			echo $rows;
    			echo '</table>';
    		}catch (PDOException $ex) {
    			echo "error reading Database values: " . $ex;
    		}
    		break;
    		
    	// invalid parameters received
    	default:
    		echo 'there was no action defined!';
    		break;
    }
    
    // Function to connect the database
    function connect_db(){
    	$DB_HOST = '127.0.0.1';
    	$DB_NAME = 'NAMEDEINERDATENBANK';
    	$DB_USER = 'DBBENUTZER';
    	$DB_PASSWORD = 'GEHEIM'; 
    	$db = new PDO('mysql:host='.$DB_HOST .';dbname='.$DB_NAME.';charset=utf8', $DB_USER, $DB_PASSWORD,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    	return $db;
    }
    ?>
     
    #1 Benko, Nov 3, 2014
    Last edited by a moderator: Nov 3, 2014
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    650
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    You will likely receive more feedback on this type of question at a forum dedicated to discussions of PHP/MySQL programming. The forums here are primarily intended for discussions of the cPanel/WHM software itself.

    Thank you.
     

Share This Page