SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
Aug 15, 2007, 08:53 #1
- Join Date
- Aug 2007
- Posts
- 318
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Getting data from db into form field.
I am currently working with an article manager. I am in middle of it.
Currently the sql connection are open and I have some data stored in the database table field called subject.
The data was sent to the db using the same article manager.
NOW I AM MODIFYING THE ALREADY EXISTING DATA IN DB; in doing so I want to extract the data existing in the database to get printed in the form field.
Code:<input id="subject" name="subject" type="text" size="45" value= "<?php echo stripslashes($_GET['subject']);?>" />
-
Aug 15, 2007, 09:00 #2
- Join Date
- Feb 2006
- Location
- Bel Air, Maryland.
- Posts
- 60
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Um the $_GET method is going to grab a var from your URL, typically submitted via a form from the previous page. If your looking to extract data from the DB field you'll need to query your DB and use something like mysql_fetch_array() to put the data from that row into a variable. Then you echo the corresponding array value (like $rowinfo['subject']) into your input field value.
Is this at all close to what your trying for?
-
Aug 15, 2007, 09:13 #3
- Join Date
- Feb 2007
- Location
- UK
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Heres a bit of a script I wrote recently to change articles. Change this for how you need it.
PHP Code:if (isset($_POST['content'])) {
// data has been sent
// get data and make it safe for query
$id = mysql_real_escape_string($_POST['id']);
$article_name = mysql_real_escape_string($_POST['article_name']);
$content = mysql_real_escape_string($_POST['content']);
$image = mysql_real_escape_string($_POST['image']);
$url = mysql_real_escape_string($_POST['url']);
$category = mysql_real_escape_string($_POST['category']);
$popular = mysql_real_escape_string($_POST['popular']);
$meta_keywords = mysql_real_escape_string($_POST['meta_keywords']);
// query to edit opening
$edit = "UPDATE articles SET
article_name = '$article_name',
content = '$content',
image = '$image',
url = '$url',
category = '$category',
popular = '$popular',
meta_keywords = '$meta_keywords'
WHERE id = $id";
$result = mysql_query($edit) or die (mysql_error());
// did it work?
if ($result) {
echo 'Article editted succesfully<br /><br />';
echo 'Go to <a href="/articles/'.$url.'">article</a>?';
} else {
echo 'Error! The values sent are below.<br /><br />';
echo $content;
}
} else if (isset($_GET['id'])) {
$edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";
$result = mysql_query($edit) or die (mysql_error());
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit-article.php">
<div>
<b>Title:</b><br /><input type="text" name="article_name" value="<?php echo $row['article_name'] ?>" /><br /><br />
<b>Content:</b><br /><textarea name="content" cols="60" rows="20"><?php echo $row['content'] ?></textarea><br /><br />
<b>Image:</b><br /><input type="text" name="image" value="<?php echo $row['image'] ?>" /><br /><br />
<?php
if (!empty($row['image'])) {
echo '<img src="/content-images/articles/'. urlencode($row['image']) .'" alt="" class="imgcenter" />
<br /><br />';
}
?>
<b>URL:</b><br /><input type="text" name="url" value="<?php echo $row['url'] ?>" /><br /><br />
<b>Category:</b><br /><input type="text" name="category" value="<?php echo $row['category'] ?>" /><br /><br />
<b>Popular (<?php echo $row['views']; ?> views):</b><br />
<select name="popular" style="width:100px;">
<option value="1"<?php echo ($row['popular'] == 1 ? ' selected="selected"' : ''); ?>>Yes</option>
<option value="0"<?php echo ($row['popular'] == 0 ? ' selected="selected"' : ''); ?>>No</option>
</select><br /><br />
<b>Meta Keywords:</b><br /><input type="text" name="meta_keywords" value="<?php echo $row['meta_keywords'] ?>" /><br /><br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" value="Edit" />
</div>
</form>
<?php
} else {
// show all articles
$sql = "SELECT id, article_name, views, popular FROM articles ORDER BY views DESC";
$result = mysql_query($sql) or die (mysql_error());
// display each child
while ($row = mysql_fetch_array($result)) {
echo '
<a href="edit-article.php?id='.$row['id'].'">'.$row['article_name'].'</a> ('.$row['views'].' views';
echo ($row['popular'] == 1) ? ', popular' : '';
echo ')<br />';
}
}
-
Aug 15, 2007, 09:15 #4
- Join Date
- Aug 2007
- Posts
- 318
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I did understand what you mean, but i am not able to implement it my database is mysql, the table is news and the field value which is required is "subject"
can you post the code integrating with
Code:<?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf("ID: %s Name: %s", $row["id"], $row["name"]); } mysql_free_result($result); ?>
can you write bit of the code that will work... is there any other way rather than using mysql_fetch_array()
any simpler solution???
-
Aug 15, 2007, 09:23 #5
- Join Date
- Feb 2007
- Location
- UK
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Why bother making a website if you refuse to learn and try to fix your own problems? I gave you code that took me a while to write so you can use it to learn from.
People arent going to do all your work.
-
Aug 15, 2007, 09:25 #6
- Join Date
- Aug 2007
- Posts
- 318
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Are you showing the existing content in the db before you edit the article. if yes that is what i what. How???
I mean if i have a subject field in db having data "The is the subject"
Now if you are editing the subject then the existing subject "The is the subject" should be printed on the screen. How to do it ??
-
Aug 15, 2007, 09:34 #7
- Join Date
- Aug 2007
- Posts
- 318
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I never wanted to hurt your feelings... sorry if i did..
The article you wrote gets the data which has just been posted using a form .
However i want the data which already exists in db to to extracted and shown in the form field..
For example wen u edit anything on this forum you get the data which is already in the db of the forum available in the form... Understand what i exactly want
-
Aug 15, 2007, 09:44 #8
- Join Date
- Feb 2007
- Location
- UK
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thats exactly what mine does.
Please actually look at the script fully.
-
Aug 15, 2007, 10:00 #9
- Join Date
- Feb 2006
- Location
- Bel Air, Maryland.
- Posts
- 60
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes the script from AlienDev does this exactly. Once you get the data from the row into the variable ($row) then simple print the html for the input and echo the subject value ($row['subject']) inside like this:
PHP Code:echo "<b>SUBJECT:</b><br /> <textarea name="subject" cols="60" rows="20">{$row['subject']}</textarea>";
-
Aug 15, 2007, 10:04 #10
- Join Date
- Aug 2004
- Location
- Poland
- Posts
- 274
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
krishnakhanna, listen to elders
AlienDev gave you whole script, all what you would ever need to be able to edit your articles.
This little piece of code does that:
PHP Code:} else if (isset($_GET['id'])) {
$edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";
$result = mysql_query($edit) or die (mysql_error());
$row = mysql_fetch_array($result);
All you have to do then, is just print out variables within text field.
PHP Code:<input type="text" name="article_name" value="<?php echo $row['article_name'] ?>">
My Q to AlienDev:
while selecting data from DB I always use this syntax:
$edit = "SELECT * FROM articles WHERE id = '" . $_GET['id'] . "'";
but you closed the variable within curly braces:
$edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";
what is the difference between single quotes ( ' ' ) and curly braces ( {} )? Any significant difference?
Thanks!
~g.
-
Aug 15, 2007, 10:13 #11
- Join Date
- Feb 2007
- Location
- UK
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In sql, for example...
Code:feild = $value
Code:feild = 1 this will work feild = a string of text this wont work feild = 'a string of text' this will work
The {} brackets arent sql, they are for PHP to tell whether or not the text wrapped in {} is a variable or not.
PHP Code:<?php
$value['example'] = 'bla';
echo "iuhawefuih$value['example']iasdu32r";
// php will get confused here and wont know whether or not to
// output the value of $value['example']
echo "iuhawefuih{$value['example']}iasdu32r";
// the {} force php to parce whatever is inside the {} as a variable
// so here it wont get confused.
-
Aug 15, 2007, 10:19 #12
- Join Date
- Aug 2004
- Location
- Poland
- Posts
- 274
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
gotcha!
I was actually using this other method to tell PHP what is a var and what is not, using either single or double quotes and always breaking the string to add a var:
<?php echo 'This is some text and this is a nice var ' . $var . ', the end'; ?>
Thanks for the explanation, gonna try braces, too
cheers,
Greg
Bookmarks