View Full Version : retrieving data and editting it (php mysql)
Tjobbe
February 13th, 2005, 23:42
http://forums.iwdn.net/viewtopic.php?t=855
going on from the above topic, a really handy way which I'd like to use is to retrieve given data from a db and be able to edit it inside for example a textarea.
How would I do this, I have tried to create a retrive data php script but I go horribly wrong each time, no idea why I'm not able to post the code here because in my frustration i deleted ith means to start afresh.
How would I call the specific table of a database, which stores html, into a textarea of e.g edit.php and then be able to click save to save any changes?
Marble
February 14th, 2005, 07:46
Its really quite simple...
Now this is assuming 1 big thing here... you are only getting one value. If you are looping thru more than one array, then use a while loop for the mysql_fetch_assoc and take off the limit 1 in the query.
// assuming the db link is already set up...
$id = $_GET['id'];
$sql = "SELECT * FROM some_table WHERE some_id = '$id' LIMIT 1";
$result = mysql_query($sql);
$n = mysql_num_rows($result);
then when you put the data into an array:
<?php
if ($n == 1)
{
$someArray = mysql_fetch_assoc ($result);
// you'll have an array of values from this query...
}
?>
// then you just echo the values between html...
<input .... value="<?php echo $someArray['somevalue']; ?>" />
<textarea><?php echo $someArray['anotherValue']; ?></textarea>
Then when you submit the code in the form.. you handle it like you would submitting the form except one thing, change the sql statement kind of like:
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$sql = "UPDATE some_table('someValue', 'anotherValue') VALUES('$var1', '$var2') WHERE some_id='2'";
This is simplified a bit. You'd always check the values before submitting them to the database...
Tjobbe
February 14th, 2005, 10:44
I am just getting one value, thansk for that, I shall break it down and get to grips with the code and then I'm good, thanks very much btw!
Tjobbe
February 14th, 2005, 11:04
Didn't take me long, but I'm lost.
I think I have succesfully connected to the database - so thats sorted.
Its the end of the script thats bugging me, the following line:
[syntax:6f86c0c5f9="php"]<input .... value="<?php echo $someArray['somevalue']; ?>" />
<textarea><?php echo $someArray['anotherValue']; ?></textarea>
<input name="submit" type="button">[/syntax:6f86c0c5f9]
I have no idea tbh where to start changing this to include a button and a form that will submit the data to the database.
My display.php shows the data,. but the code above doesn't.
here is my display.php:
[syntax:6f86c0c5f9="php"]<?php
$c = mysql_connect('localhost'/*,'user','pass'*/);
if (!$c) die("Crap.. not again " . mysql_error());
mysql_select_db(simple,$c) or die(" Grrrr " . mysql_error());
$get = mysql_query("SELECT * FROM stuff") or die("Mmm pie:" . mysql_error());
while ($row = mysql_fetch_row($get)) {
echo "<h4>Stuff you stored:</h4>\n";
echo "<p>$row[1]</p> ";
}
?> [/syntax:6f86c0c5f9]
shamlessly "stolen" from: http://forums.iwdn.net/viewtopic.php?t=855
sonicgroup
February 14th, 2005, 15:46
You just need to change some parts of that to get it to display a form:
[syntax:f2d8aab57e="php"]
<?php
$c = mysql_connect('localhost'/*,'user','pass'*/);
if (!$c) die("Crap.. not again " . mysql_error());
mysql_select_db(simple,$c) or die(" Grrrr " . mysql_error());
$get = mysql_query("SELECT * FROM stuff") or die("Mmm pie:" . mysql_error());
while ($row = mysql_fetch_row($get)) {
echo "<h4>Stuff you stored:</h4>\n";
echo '<form action="update.php" method="post">';
echo '<textarea>'.$row[1].'</textarea>';
echo '<input type="submit" name="update" value="Update" /></form>';
}
?>
[/syntax:f2d8aab57e]
Then you need to make your update.php file to take the POSTed data and run an UPDATE query on the database.
Tjobbe
February 14th, 2005, 15:52
I see, using a new page, called update.php, that takes $textarea and does "insert into* with some mysql code?
If thats it, I should be able to figure this out.
i hope.
sonicgroup
February 14th, 2005, 15:59
Use INSERT if you are adding a record; use UPDATE if you are editing a record (though in this case you'd probably want to add a hidden form field with the ID of the record being edited).
Tjobbe
February 14th, 2005, 16:03
- the hidden field being a simple name field? not 100% sure on this.
sonicgroup
February 14th, 2005, 17:41
Assuming you have some way to specify what record you are updating, like a variable in the URL (say: edit.php?id=3), then something like this:
[syntax:60a37c77ab="php"]
echo '<input type="hidden" name="recordID" value="'.$_GET['id'].'" />';
[/syntax:60a37c77ab]
Tjobbe
February 14th, 2005, 17:57
got ya, thanks dave!
Marble
February 14th, 2005, 20:44
To keep down on the number of pages its pretty easy to set it up to just use one page for the form... if you send me the pages I can edit them to reflect what I am talking about...
~ J
vBulletin® v3.6.8, Copyright ©2000-2010, Jelsoft Enterprises Ltd.