/    Sign up×
Community /Pin to ProfileBookmark

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:

[code=php]<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($result, MYSQL_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>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowMay 25.2014 — 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 [url=]as the giant red warning boxes in the manual[/url] 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:
&lt;?php

try {
$db = new PDO(
'mysql:host=localhost;dbname=dbase',
'username',
'password'
);
} catch (PDOException $e) {
die('Connection failed: ' . $e-&gt;getMessage());
}

$statement = $db-&gt;query('
SELECT idt, datetime, col1, col2, col3 FROM test
');

echo '
&lt;table id="testData"&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th scope="col"&gt;idt&lt;/th&gt;
&lt;th scope="col"&gt;datetime&lt;/th&gt;
&lt;th scope="col"&gt;col1&lt;/th&gt;
&lt;th scope="col"&gt;col2&lt;/th&gt;
&lt;th scope="col"&gt;col3&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;&lt;tbody&gt;
';

function makeRow($row, $index) {
echo '
}

while ($row = $statement-&gt;fetch()) {
echo '&lt;tr&gt;';
foreach ($row as $key =&gt; $data) echo '
&lt;td&gt;
&lt;input
type="text"
name="test[', $row['idt'], '][', $index, ']"
value="', $row[$index], '"
width="25"
/&gt;
&lt;/td&gt;';
echo '
&lt;/tr&gt;';
}

echo '
&lt;/tbody&gt;
&lt;/table&gt;';

?&gt;


Though you'd have to re-factor your submit handler accordingly, it would make handling that result output far, far simpler.
×

Success!

Help @Philosophaie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...