homepage Welcome to WebmasterWorld Guest from 23.20.220.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Updating and get value of the field
chrissim




msg:4167176
 1:38 pm on Jul 9, 2010 (gmt 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>";


 

Matthew1980




msg:4167181
 1:47 pm on Jul 9, 2010 (gmt 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

chrissim




msg:4167192
 2:09 pm on Jul 9, 2010 (gmt 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>";

Matthew1980




msg:4167207
 2:51 pm on Jul 9, 2010 (gmt 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

chrissim




msg:4167215
 3:19 pm on Jul 9, 2010 (gmt 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
------------------

chrissim




msg:4167532
 1:24 am on Jul 10, 2010 (gmt 0)


anyone here?

chrissim




msg:4167552
 2:49 am on Jul 10, 2010 (gmt 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>";

}

Matthew1980




msg:4167617
 8:53 am on Jul 10, 2010 (gmt 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

chrissim




msg:4167722
 3:18 pm on Jul 10, 2010 (gmt 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>";


}

?>

chrissim




msg:4167725
 3:24 pm on Jul 10, 2010 (gmt 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>";


}

?>

Readie




msg:4167744
 4:27 pm on Jul 10, 2010 (gmt 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++) {

rainborick




msg:4167747
 4:52 pm on Jul 10, 2010 (gmt 0)

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>";

}

chrissim




msg:4167919
 2:26 am on Jul 11, 2010 (gmt 0)

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

Thanks

chrissim




msg:4168068
 1:14 pm on Jul 11, 2010 (gmt 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());

chrissim




msg:4168084
 2:43 pm on Jul 11, 2010 (gmt 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 />";}

}
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved