Displaying SQL data as forms value attribute

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hey,

I have average knowledge of PHP/SQL. I'm trying to create a CMS system for a website I'm doing. I am currently able to add a new page with content and delete the page. I'm now working on the Edit Page part of it - I can it to update the required record in my SQL table with the contents of the form. What I'd like to do now, is display the CURRENT content in the form field using the 'value' attribute, so the user can edit any typos etc. rather than copying and pasting, or typing everything again.

I dont want to use a ready made CMS - sorry :D

Can anyone help?

Cheers,
 
Just put the data in value tag as you suggested -although I've prob misunderstood you??????? E.g.

PHP:
value="<?php echo $row["something"] ?>"
 
Yeah that's what I want, although it isn't working. Is $row a special variable? I'm currently using $result after performing my select query to pull the right data...

Any ideas?
 
$row is normally assigned to a row in the result set, what function are you using to retrieve the result?
 
It looks like this:

Code:
<input name="name" type="text" id="name" <?PHP 
      if ($HTTP_GET_VARS['action'] == 'edit'){
      $pageID = $HTTP_GET_VARS['id'];
      $result = $connector->query('SELECT ID,name FROM pages WHERE id='.$pageID);
      echo 'value="'.$result['name'].'">';
      }
     ?>

It this right?

Cheers,
 
OK, similar question. How would I do the same thing for a select list? At the moment I have a list which is being built by PHP (fetching the page id's and names in a loop) for the 'parent page'. How could I pull this value from my table as I have asked above (for the same reason)?

Thanks,
 
OK, similar question. How would I do the same thing for a select list? At the moment I have a list which is being built by PHP (fetching the page id's and names in a loop) for the 'parent page'. How could I pull this value from my table as I have asked above (for the same reason)?

Thanks,

Just use PHP to generation to dynamic html - thats all you're doing really.

So you have you something like this:

PHP:
<select>
              <option value="your value from db">value from db</option>
      </select>
where the option tag is generated by a loop.
 

This is what I have already, the list is already being built using PHP looping through the pages table. What I want to do is show the current value for the page being edited, as I have done above for text fields using an SQL query for the value attribute.
 
You'll need to use the keyword 'Selected' for the appropriate <option>, and have an if statement to select the right value, i.e:

PHP:
for(blah, blah, blah) {

	$idfromdb = mysql_result($result,$i,"id");

	if ($currentid == $idfromdb) {
			echo "<option value='$someval' selected>Some Text</option>";
		}
		else {
			echo "<option value='$someval'>Some Text</option>";
		}
	}


}
 
This is what I have already, the list is already being built using PHP looping through the pages table. What I want to do is show the current value for the page being edited, as I have done above for text fields using an SQL query for the value attribute.

ah sorry, misunderstood you there ;)
 
You'll need to use the keyword 'Selected' for the appropriate <option>, and have an if statement to select the right value, i.e:

PHP:
for(blah, blah, blah) {

	$idfromdb = mysql_result($result,$i,"id");

	if ($currentid == $idfromdb) {
			echo "<option value='$someval' selected>Some Text</option>";
		}
		else {
			echo "<option value='$someval'>Some Text</option>";
		}
	}


}

OK, I've done this for a list that displays a list of pages - this is fine as all the information is in one table. I have a field in the pages table which uses the ID from another table (slideshows). How would I put the selected attribute by the chosen option for this list?

Sorry it doesn't make much sense.

Cheers.
 
lol, I don't really follow you I'm afraid. What do you mean by selected attribute? An item that you want your listbox to be selected? By the sounds of it you need to make another sql query to pull information from your slideshow table using the id from your pages table, and use that for the listbox or something?

I don't quite understand what you're trying to do lol :) Sorry, I'm not the best at understanding others ideas lol. If you still need more help with this, post up some code that you're using now and I'll have a looksee.
 
OK, I've done this for a list that displays a list of pages - this is fine as all the information is in one table. I have a field in the pages table which uses the ID from another table (slideshows). How would I put the selected attribute by the chosen option for this list?

Sorry it doesn't make much sense.

Cheers.


Sounds like you need to alter the SQL to include a join to another table. If this is case all you'd need to do is change the sql query on your page and add a reference to the new field (would probs need to use tablename.field as your using multiple tables).

If you post your table stucture I'm sure someone can give you the query you need to use.
 
Right, so i've got two tables - 'pages' and 'slideshows'. Pages holds any content for the page and also an ID for 'Parent Page' and 'Slideshow' if it has one.

Parent Page and Slideshow are both selected from a drop down list generated by PHP. This loops through the relevant table, populating the list 'options' with the ID and name from each table.

Code:
<select name="parent">
<option value="0">None</option>
<?PHP
$result = $connector->query('SELECT ID,name,parent FROM pages ORDER BY name');
while ($row = $connector->fetchArray($result)){
if ($HTTP_GET_VARS['action'] == 'edit'){
echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';}
}
}
?>
</select>

This is fine for creating a new page, however, when editing a page, I want the information already in the table to be displayed. I've achieved this for the text boxes using the value attribute and an SQL query:

Code:
<input name="name" type="text" id="name" <?PHP 
if ($HTTP_GET_VARS['action'] == 'edit'){
$pageID = $HTTP_GET_VARS['id'];
$result = $connector->query('SELECT ID,name FROM pages WHERE id='.$pageID);
$row = $connector->fetchArray($result);
echo 'value="'.$row['name'].'">';
}
?>

This looks at a http variable called 'id' and checks the row for that id, pulling the relevant field of that row.

I want to be able to do the same for the list boxes, using the selected attribute. Sorry if this is a bit heavy. Any help?

Cheers,
 
Please make sure you properly sanitise/escape anything you are putting into SQL queries, the code you've pasted is the classic SQL injection vector.
 
Back
Top Bottom