Welcome to WebmasterWorld Guest from 54.147.237.64

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Updating and get value of the field

     
1:38 pm on Jul 9, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 0


hi,

i'm closed and managed to retrieve all Table field but stucked here trying to get row value data {$row['$field']} below. Could someone point me the right direction of the issue


for ($i = 0; $i < mysql_num_fields($result); ++$i) {
$field = mysql_field_name($result, $i);
$row = mysql_field_rows($result, $i);


echo "<td align=\"right\"><strong>$field:</strong></td><td><input name=\"$field\" type=\"text\" id=\"$field\" value=\"{$row['$field']}\" size=\"30\"></td></tr><tr>";

1:47 pm on July 9, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there Chrissim,

If your incrementing the value:-

for ($i = 0; $i < mysql_num_fields($result); ++$i) {

should be:-

for ($i = 0; $i < mysql_num_fields($result); $i++) {

and the concatenations need changing too (I have formatted this version):-

echo "<td align=\"right\"><strong>".$field.":</strong></td><td><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$row[$field]."\" size=\"30\"></td></tr><tr>";

At least I think that's right, typed on the fly, but applying the right escaping/concatenation logic that *should be* ok :) If I was on my laptop I could do it better, but instead I am on a break at work at the moment :)

I have bolded the one var because you don't need quotes around it when you are doing it that way...

Hope that helps,

Cheers,
MRb
2:09 pm on July 9, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi matthew1980

i changed them according to your suggestion but this time i was able to retrieve all Table field and was left with empty page and empty value \"".$row[$field]."\"

for ($i = 0; $i < mysql_num_fields($result); $i++) {
$field = mysql_field_name($result, $i);
$row = mysql_field_rows($result, $i);


echo "<td align=\"right\"><strong>".$field.":</strong></td><td><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$row[$field]."\" size=\"30\"></td></tr><tr>";
2:51 pm on July 9, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there Chrissim,

Have you made sure that the information coming from the query is correct, ie have you just echoed the values from that, without formatting the html around it? You need to make sure that there is data there before you try to echo it :)

Example:-
for ($i = 0; $i < mysql_num_fields($result); $i++) {
$field = mysql_field_name($result, $i);
$row = mysql_field_rows($result, $i);//Just checked this on php.net, as a standard function it doesn't exist
//see if the data is there as you are expecting...
print_r($field);
print_r($row);
}

Other than that I am not too sure what you are trying to achieve, would it be simpler just to use a mysql_fetch_array() to get the information you are after?

I suspect that if you were to turn error_reporting(E_ALL); on that you would get and 'undefined function' error. In this script just pop this up the top right by the opening tags:-

<?php
//set error reporting to all
error_reporting(E_ALL);
//
//Rest of your code

Cheers,
MRb
3:19 pm on July 9, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi matthew1980

ok i tried to echo it and it show all the correct array such as id , username and password data information.

The td table are able to retrieve all fields but only getting the first input value and the rest empty

Table
------------------
id - 36
username - empty
password - empty
------------------
1:24 am on July 10, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0



anyone here?
2:49 am on July 10, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi,

Basically i'm trying to update the records here with all table fields generating increment value of each field and all input value row for the specific field. The coding below was able to pull all fields with no problem but only the input value of my data that gave me the headache here.

The username and password should have all my details from my database such as myname and mypass number

Table
------------------
id - 36
username - empty row
password - empty row
------------------


for ($i = 0; $i < mysql_num_fields($result); $i++) {
$field = mysql_field_name($result, $i);
$row = mysql_fetch_array( $result);

echo "<td align=\"right\"><strong>".$field.":</strong></td><td><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$row[$field]."\" size=\"30\"></td></tr><tr>";

}
8:53 am on July 10, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


Hi there Chrissim,

Right then, You will have to post the relevant code of how & what is stored in the $result var, other wise any advice would be guesswork :)

IF the two values are empty, it means that either the query was empty, or there is nothing there to get.

Before worrying about getting the echoed string to function, lets get the DB query working first :) Ie make sure that the expected data is being asked for correctly...

Cheers,
MRb
3:18 pm on July 10, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi matthew1980

Ok here's my complete script and the db and id did captured in my script. In my Table i have three variables id, username and password. i'm trying to retrieve the specific id but the output are empty in both the username & password.
I tried to echo them out as what you suggested print_r($field);print_r($row); and everything what i wanted were showing correct information.


<?php

$db=$_GET['db'];
$id=$_GET['id'];


$query = "SELECT * FROM $db WHERE id='$id'";

$result = mysql_query($query) or die(mysql_error());

for ($i = 0; $i < mysql_num_fields($result); $i++) {
$field = mysql_field_name($result, $i);
$row = mysql_fetch_array($result);

echo "<td align=\"right\"><strong>".$field.":</strong></td><td><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$row[$field]."\" size=\"30\"></td></tr><tr>";


}

?>
3:24 pm on July 10, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi matthew1980

Ok here's my complete script and the db and id did captured in my script. In my Table i have three variables id, username and password. i'm trying to retrieve the specific id but the output are empty in both the username & password.
I tried to echo them out as what you suggested print_r($field);print_r($row); and everything what i wanted were showing correct information but the script failed to output the data inside the value=\"".$row[$field]."\" of the username and password.


<?php

$db=$_GET['db'];
$id=$_GET['id'];


$query = "SELECT * FROM $db WHERE id='$id'";

$result = mysql_query($query) or die(mysql_error());

for ($i = 0; $i < mysql_num_fields($result); $i++) {
$field = mysql_field_name($result, $i);
$row = mysql_fetch_array($result);

echo "<td align=\"right\"><strong>".$field.":</strong></td><td><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$row[$field]."\" size=\"30\"></td></tr><tr>";


}

?>
4:27 pm on July 10, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 0


Just jumping in here for this:
for ($i = 0; $i < mysql_num_fields($result); $i++) {

Bad coding: You are calling the mysql_num_fields() for every iteration of the loop.

While not quite as bad as calling mysql_num_rows() or count() in the for loop, it should still be avoided as it involves unecessary processing. Declare it as a variable before the loop, like so:

$count = mysql_num_fields($result);
for($i = 0; $i < $count; $i++) {
4:52 pm on July 10, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:938
votes: 25


This might be better:

$db=$_GET['db'];
$id=$_GET['id'];

$query = "SELECT * FROM $db WHERE id='$id'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);

foreach ($row as $field=>$value) {

echo "<td align=\"right\"><strong>".$field.":</strong></td><td><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$value."\" size=\"30\"></td></tr><tr>";

}
2:26 am on July 11, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hey rainborick you rock man. Works great and it pull all the require data :)

Thanks
1:14 pm on July 11, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi guys,

i need some help getting updating query to pull the Table field something like this below

updating string

$sql=mysql_query("UPDATE $db SET field='$field' WHERE id=$id") or die(mysql_error());
2:43 pm on July 11, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts:113
votes: 0


hi,

As for the updating the records i have the code that pull all the post array but i have no idea how to implement this code sql=mysql_query("UPDATE $db SET field='$field' WHERE id=$id") or die(mysql_error());


if ($_POST) {
foreach (array_keys($_POST) as $key) {
$$key = $_POST[$key];
if (is_array(${$key})){
foreach(${$key} as $key2){
$$key2 = $_POST[$key2];
print "$key2 was part of array $key = $key<br />";
}
}
else{
print "$key = ${$key}<br />";}

}
}
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members