www.webdeveloper.com
Results 1 to 2 of 2

Thread: Print out the MySQL data into textboxes

  1. #1
    Join Date
    Sep 2010
    Posts
    66

    Print out the MySQL data into textboxes

    I have table (test) in database (dbase) that has 5 columns: idt(primary), datetime, col1, col2 and col3.

    I would like to print out the data into textboxes <input type="text" />.

    This is what I have so far:

    PHP Code:
    <body>
        <?php
            $conn 
    mysql_connect(connection string works);
            if (!
    $conn)
               {
                       die(
    'Could not connect: ' mysql_error());
               } 
               else 
               {
                    
    mysql_select_db('dbase');
                    
    $sql "SELECT * FROM test;";
                    
    $result mysql_query($sql$conn);
                                    echo 
    '<center><table><tr><th>idt</th><th>datetime</th><th>col1</th><th>col2</th><th>col3</th></tr>';
                    
    $n=0;
                    while ((
    $data  mysql_fetch_array($resultMYSQL_ASSOC)) !== null) { 
                        
    $n=$n+1;
                        echo 
    '<tr><td>''<input name="Text' $n '" type="text" value= "' $data['idt'] & '" width="25" />''</td>';
                        
    $n=$n+1
                        echo 
    '<td>''<input name="Text' $n '" type="text" value= "' $data['datetime'] & '" width="25" />''</td>';
                        
    $n=$n+1;
                        echo 
    '<td>''<input name="Text' $n '" type="text" value= "' $data['col1'] & '" width="25" />''</td>';
                        
    $n=$n+1;
                        echo 
    '<td>''<input name="Text' $n '" type="text" value= "' $data['col2'] & '" width="25" />''</td>';
                        
    $n=$n+1;
                        echo 
    '<td>''<input name="Text' $n '" type="text" value= "' $data['col3'] & '" width="25" />''</td></tr>';
                    } 
                    
    mysqli_free_result($resulti);
                    echo 
    '</table></center></div>';
            }
            
    mysql_close($conn);
        
    ?>

    </body>

  2. #2
    Join Date
    May 2014
    Posts
    936
    Uhm... why are you doing binary 'and' instead of string addition or comma delimits? Replace all those & with commas.

    NOT that being 2014 and not 1997 you have any business with a CENTER tag in your markup, nor being 2014 instead of 2004 should you be using mysql_ functions as we've been told since PHP 5 dropped and as the giant red warning boxes in the manual have been telling us for a couple years. It's also probably not a great idea to be using multiple echo statements to do the job of one, or to have TH that are obviously column headers lacking scope attributes or a THEAD around them. Much less all those bizzare opening and closing of strings for nothing...

    Of course, I'd probably also have the names the same as they are in the database, but with the number index first... and updating your number index per TD is also probably part of your problem, which is why I'd update them per row, not per TD... that or I'd use a parent field -- like that row ID (at least that's what I assume idt is... is that a unique row ID in the table?)

    Something like:
    Code:
    <?php 
    
    try {
    	$db = new PDO(
    		'mysql:host=localhost;dbname=dbase',
    		'username',
    		'password'
    	);
    } catch (PDOException $e) {
    	die('Connection failed: ' . $e->getMessage());
    }
    
    $statement = $db->query('
    	SELECT idt, datetime, col1, col2, col3 FROM test
    ');
    
    echo '
    	<table id="testData">
    		<thead>
    			<tr>
    				<th scope="col">idt</th>
    				<th scope="col">datetime</th>
    				<th scope="col">col1</th>
    				<th scope="col">col2</th>
    				<th scope="col">col3</th>
    			</tr>
    		</thead><tbody>
    			'; 
    			
    function makeRow($row, $index) {
    	echo '
    }			
    			
    while ($row = $statement->fetch()) {
    	echo '<tr>';
    	foreach ($row as $key => $data) echo '
    				<td>
    					<input
    						type="text"
    						name="test[', $row['idt'], '][', $index, ']" 
    						value="', $row[$index], '"
    						width="25"
    					/>
    				</td>';
    	echo '
    			</tr>';
    }
    
    echo '
    		</tbody>
    	</table>';
    			
    ?>
    Though you'd have to re-factor your submit handler accordingly, it would make handling that result output far, far simpler.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles