FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » display data from mysql db in text box??
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
display data from mysql db in text box?? [message #169374] Wed, 08 September 2010 19:12 Go to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
I want to create a simple page to edit a record, and when submit is
clicked I want to update the record.

First I want to display all the data for the record, based on ID, on
the page
Here is what I am doing:

<?php
$txtid= $_POST[txtid];

$sql = "SELECT date, user1, user2, user3, role, id
FROM myuserdata
WHERE id= $txtid
ORDER BY id";

And so then I have text boxes on the page and I want to fill those in
based on the current ID

I tried to do this:

<input type="text" name="txtdate" id="txtdate" value=<?php echo
[date];?> />

<input type="text" name="txtuser1" id="txtuser1" value=<?php echo
[user1];?> />

this did not work. How would I display the actual db fields in the
textboxes?
Re: display data from mysql db in text box?? [message #169377 is a reply to message #169374] Wed, 08 September 2010 20:04 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
PAkerly wrote:
> I want to create a simple page to edit a record, and when submit is
> clicked I want to update the record.
>
> First I want to display all the data for the record, based on ID, on
> the page
> Here is what I am doing:
>
> <?php
> $txtid= $_POST[txtid];
>
> $sql = "SELECT date, user1, user2, user3, role, id
> FROM myuserdata
> WHERE id= $txtid
> ORDER BY id";
>
> And so then I have text boxes on the page and I want to fill those in
> based on the current ID
>
> I tried to do this:
>
> <input type="text" name="txtdate" id="txtdate" value=<?php echo
> [date];?> />
>
> <input type="text" name="txtuser1" id="txtuser1" value=<?php echo
> [user1];?> />
>
> this did not work. How would I display the actual db fields in the
> textboxes?

$sql = "SELECT date, user1, user2, user3, role, id
FROM myuserdata
WHERE id= $txtid
ORDER BY id";
/* add these bits */

$result=mysql_query($sql);

$date=mysql_result($result,'date',0);
$user1=mysql_result($result,'user1',0);
....etc.

Then
<input type="text" name="txtdate" id="txtdate" value=<?php echo
> $date;?> />

etc..
Re: display data from mysql db in text box?? [message #169378 is a reply to message #169377] Wed, 08 September 2010 20:20 Go to previous messageGo to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
On Sep 8, 4:04 pm, The Natural Philosopher <t...@invalid.invalid>
wrote:
> PAkerly wrote:
>> I want to create a simple page to edit a record, and when submit is
>> clicked I want to update the record.
>
>> First I want to display all the data for the record, based on ID, on
>> the page
>> Here is what I am doing:
>
>> <?php
>> $txtid= $_POST[txtid];
>
>> $sql = "SELECT date, user1, user2, user3, role, id
>>            FROM myuserdata
>>            WHERE id= $txtid
>>            ORDER BY id";
>
>> And so then I have text boxes on the page and I want to fill those in
>> based on the current ID
>
>> I tried to do this:
>
>> <input type="text" name="txtdate" id="txtdate" value=<?php echo
>> [date];?> />
>
>> <input type="text" name="txtuser1" id="txtuser1" value=<?php echo
>> [user1];?> />
>
>> this did not work.  How would I display the actual db fields in the
>> textboxes?
>
> $sql = "SELECT date, user1, user2, user3, role, id
>                 FROM myuserdata
>                 WHERE id= $txtid
>                 ORDER BY id";
> /* add these bits */
>
> $result=mysql_query($sql);
>
> $date=mysql_result($result,'date',0);
> $user1=mysql_result($result,'user1',0);
> ...etc.
>
> Then
> <input type="text" name="txtdate" id="txtdate" value=<?php echo
>  > $date;?> />
>
> etc..

Added this:

<input type="text" name="txtdate" id="txtdate" value=<?php echo> >
$date; ?> />

and the other things...

$result=mysql_query($sql);

$date=mysql_result($result,'date',0);
$user1=mysql_result($result,'user1',0);
$user2=mysql_result($result,'user2',0);

and I get an error:
Parse error: syntax error, unexpected '>'
Re: display data from mysql db in text box?? [message #169380 is a reply to message #169378] Wed, 08 September 2010 20:32 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
PAkerly wrote:
> On Sep 8, 4:04 pm, The Natural Philosopher <t...@invalid.invalid>
> wrote:
>> PAkerly wrote:
>>> I want to create a simple page to edit a record, and when submit is
>>> clicked I want to update the record.
>>> First I want to display all the data for the record, based on ID, on
>>> the page
>>> Here is what I am doing:
>>> <?php
>>> $txtid= $_POST[txtid];
>>> $sql = "SELECT date, user1, user2, user3, role, id
>>> FROM myuserdata
>>> WHERE id= $txtid
>>> ORDER BY id";
>>> And so then I have text boxes on the page and I want to fill those in
>>> based on the current ID
>>> I tried to do this:
>>> <input type="text" name="txtdate" id="txtdate" value=<?php echo
>>> [date];?> />
>>> <input type="text" name="txtuser1" id="txtuser1" value=<?php echo
>>> [user1];?> />
>>> this did not work. How would I display the actual db fields in the
>>> textboxes?
>> $sql = "SELECT date, user1, user2, user3, role, id
>> FROM myuserdata
>> WHERE id= $txtid
>> ORDER BY id";
>> /* add these bits */
>>
>> $result=mysql_query($sql);
>>
>> $date=mysql_result($result,'date',0);
>> $user1=mysql_result($result,'user1',0);
>> ...etc.
>>
>> Then
>> <input type="text" name="txtdate" id="txtdate" value=<?php echo
>>> $date;?> />
>>
>> etc..
>
> Added this:
>
> <input type="text" name="txtdate" id="txtdate" value=<?php echo> >
> $date; ?> />
>
> and the other things...
>
> $result=mysql_query($sql);
>
> $date=mysql_result($result,'date',0);
> $user1=mysql_result($result,'user1',0);
> $user2=mysql_result($result,'user2',0);
>
> and I get an error:
> Parse error: syntax error, unexpected '>'

well that could be anywhere.

I am not convinced the select statement is properly formed either.
Re: display data from mysql db in text box?? [message #169381 is a reply to message #169378] Wed, 08 September 2010 23:01 Go to previous messageGo to next message
rf is currently offline  rf
Messages: 19
Registered: September 2010
Karma: 0
Junior Member
"PAkerly" <pakerly(at)gmail(dot)com> wrote in message
news:359db7d3-eadf-4b79-85b6-b8a44b7e9bc3(at)x42g2000yqx(dot)googlegroups(dot)com...
On Sep 8, 4:04 pm, The Natural Philosopher <t...@invalid.invalid>
wrote:

> Added this:

> <input type="text" name="txtdate" id="txtdate" value=<?php echo> >
> $date; ?> />

> and I get an error:
> Parse error: syntax error, unexpected '>'

When the parser says it has found an unexpeced > it's best to start looking
for a > where it shouldn't be. I see one after the word echo.
Re: display data from mysql db in text box?? [message #169382 is a reply to message #169377] Wed, 08 September 2010 23:06 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(The Natural Philosopher)

> PAkerly wrote:
>> I want to create a simple page to edit a record, and when submit is
>> clicked I want to update the record.
>>
>> First I want to display all the data for the record, based on ID, on
>> the page
>> Here is what I am doing:
>>
>> <?php
>> $txtid= $_POST[txtid];
>>
>> $sql = "SELECT date, user1, user2, user3, role, id
>> FROM myuserdata
>> WHERE id= $txtid
>> ORDER BY id";
>>
>> And so then I have text boxes on the page and I want to fill those in
>> based on the current ID
>>
>> I tried to do this:
>>
>> <input type="text" name="txtdate" id="txtdate" value=<?php echo
>> [date];?> />
>>
>> <input type="text" name="txtuser1" id="txtuser1" value=<?php echo
>> [user1];?> />
>>
>> this did not work. How would I display the actual db fields in the
>> textboxes?
>
> $sql = "SELECT date, user1, user2, user3, role, id
> FROM myuserdata
> WHERE id= $txtid
> ORDER BY id";
> /* add these bits */
>
> $result=mysql_query($sql);
>
> $date=mysql_result($result,'date',0);
> $user1=mysql_result($result,'user1',0);
> ...etc.

mysql_fetch_assoc() would be much more appropriate here, easier and more
efficient. Additionally some input validation will be needed in order to
prevent SQL injection.

Micha
Re: display data from mysql db in text box?? [message #169389 is a reply to message #169374] Thu, 09 September 2010 07:20 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 08/09/2010 21:12, PAkerly escribió/wrote:
> I want to create a simple page to edit a record, and when submit is
> clicked I want to update the record.
>
> First I want to display all the data for the record, based on ID, on
> the page
> Here is what I am doing:
>
> <?php
> $txtid= $_POST[txtid];

You are (inadvertently) using an undefined constant. This should trigger
a notice. Try this instead:

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);

$txtid = isset($_POST['txtid']) ? $_POST['txtid'] : NULL;

>
> $sql = "SELECT date, user1, user2, user3, role, id
> FROM myuserdata
> WHERE id= $txtid
> ORDER BY id";

You are basically taking raw random input from an untrusted source,
injecting it into a SQL statement and running the resulting query
against your database server. It's not very different from publishing
your password in the front page :)

You don't say what DB library you are using but, in this case, you can
probably do something like this:

if( !is_null($txtid) ){
$sql = 'SELECT date, user1, user2, user3, role, id
FROM myuserdata
WHERE id=' . (int)$txtid;
// ...
}

Please note I've removed the ORDER BY statement: there's no point in
sorting a result set than can contain at most one row.


> And so then I have text boxes on the page and I want to fill those in
> based on the current ID
>
> I tried to do this:
>
> <input type="text" name="txtdate" id="txtdate" value=<?php echo
> [date];?> />
>
> <input type="text" name="txtuser1" id="txtuser1" value=<?php echo
> [user1];?> />
>
> this did not work. How would I display the actual db fields in the
> textboxes?

Perhaps you mean this:

<input type="text" name="txtuser1" id="txtuser1" value=<?php echo
htmlspecialchars($a_variable_name); ?> />

Or this:

<input type="text" name="txtuser1" id="txtuser1" value=<?php echo
htmlspecialchars($an_array_name['user1']); ?> />


PHP is very picky: you must use the PHP syntax to write PHP code; you
cannot invent your own.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: display data from mysql db in text box?? [message #169395 is a reply to message #169374] Thu, 09 September 2010 12:18 Go to previous message
Ajayendra is currently offline  Ajayendra
Messages: 1
Registered: September 2010
Karma: 0
Junior Member
On Sep 9, 12:12 am, PAkerly <pake...@gmail.com> wrote:
> I want to create a simple page to edit a record, and when submit is
> clicked I want to update the record.
>
> First I want to display all the data for the record, based on ID, on
> the page
> Here is what I am doing:
>
> <?php
> $txtid= $_POST[txtid];
>
> $sql = "SELECT date, user1, user2, user3, role, id
>                 FROM myuserdata
>                 WHERE id= $txtid
>                 ORDER BY id";
>
> And so then I have text boxes on the page and I want to fill those in
> based on the current ID
>
> I tried to do this:
>
> <input type="text" name="txtdate" id="txtdate" value=<?php echo
> [date];?> />
>
> <input type="text" name="txtuser1" id="txtuser1" value=<?php echo
> [user1];?> />
>
> this did not work.  How would I display the actual db fields in the
> textboxes?


<!-- Code Below -->
$sql = "SELECT date, user1, user2, user3, role, id
FROM myuserdata
WHERE id= $txtid
ORDER BY id";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
<input type="text" name="txtdate id="txtdate" value="<?php echo
$row['date']?>" />

etc...
enjoy!!!!!!


Ajayendra Nath Tripathi
E-Mail- ajayendra(dot)tripathi1(at)gmail(dot)com
web- http://www.ajspark.co.cc
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Php code for ajax encoding
Next Topic: Re: get_object_vars ($this) and member visibility
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 04:06:40 GMT 2024

Total time taken to generate the page: 0.02484 seconds