Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tricky Question including PHP/MySQL/Forms

Status
Not open for further replies.

t5amec

Programmer
Aug 14, 2003
112
GB

What i want to do is edit information/content on my site, via my site, without having to go into the editors and stuff.

I'm just talking about content, not coding.

What I was thinking is having a page where I could log onto and it would display the contents in a form.
(e.g form fields that are pre-filled with previous, out of date data)
I could then edit what it says in these fields, click Save/Submit and then the new content would replace the old.

An example:
I have a personal page. I say I like cabbage. so for starters I put cabbage in the likes field, click submit, in the database under likes in my record it says cabbage.
a week goes by and I am turning green(or red) after eating so much cabbage, and I'm pretty much sick of it all. I decide to go on the EDIT version of my personal site, and change cabbage for tomatoes. click save, so from then on it says I like tomatoes.
(for the record I like cabbages, i think one day scientists will realise it is the actual cure for the common cold... ok thats a lie, but I do like cabbage.)

I have the codes for entering answers to a form to create a unique record, but to modify an existing record, i'm pretty much in the dark place.

Please help...

Make Sense? I hope so (-:
 
I have the codes for entering answers to a form to create a unique record, but to modify an existing record, i'm pretty much in the dark place.[/qoute]

what do you have so far? modify is not a big deal.. you replace insert with update.. what do you need.
 
The code for the inputting of the records are simple HTML to a php form post.

create.html
Code:
<html>
<head>
<title>create.html</title>
</head>
<body>
<form method="post" action="post.php">
Your name: <input type="text" name="name"><br>
Your email: <input type="text" name="email"><br>
Did you: <select name="hobby">
<option selected>eat
<option>sleep
<option>drink
</select>
Your comments:<br>
<textarea name="comments"></textarea>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

post.php
Code:
<html>
<head><title>post.php</title><head>
<body>
<?php
$db = mysql_connect("localhost","******","******");
mysql_select_db("themalloys_uk_db",$db);

{
$date=date("Y-m-d");
$sql="INSERT INTO test (name,email,hobby,comments) VALUES ('$name','$email','$hobby','$comments')";
mysql_query($sql, $db);
print("Cheers!") ;
}
?>
<a href="view.php">VIEW</a>
</body>

</html>

view.php
Code:
<html>
<head>
<title>view.php</title>

<?php
$db = mysql_connect('localhost','******','******');
mysql_select_db('themalloys_uk_db',$db);

$requete = "SELECT * FROM test";
$result = mysql_query ($requete,$db);
while($test = mysql_fetch_object($result)) {
   echo $test->name.'<br>';
   echo $test->email.'<br>';
   echo $test->hobby.'<br>';
   echo $test->comments.'<br>';
}
mysql_free_result($result); 
?>

</head>
</body>
<?php echo $test->name ?>
<br>
<?php echo $test->email ?>
<br>
<?php echo $test->hobby ?>
<br>
<?php echo $test->comments ?>
</body>
</html>

I can see how you could change INSERT for UPDATE, but how would that display the previously entered content in the form fields?

Make Sense? I hope so (-:
 
well with a mix between the view and post, I meam, with the view you get the data from DB and show the content in the form:


<textarea name="comments">
<?php echo $test->comments ?>
</textarea>

then, mae be, make some kind of validation and the post2.php should have the "UPDATE" with the $_POST['comments'] WHERE name=$test->name (or $_POST['name'])

hope this help.
 
i have created a file called edit.php which is the form post for the edited stuff.

edit.php
Code:
<html>
<head><title>edit.php</title><head>
<body>
<?php
$db = mysql_connect("localhost","******","******");
mysql_select_db("themalloys_uk_db",$db);

{
$date=date("Y-m-d");
$sql="UPDATE INTO test $_POST(name,email,hobby,comments) WHERE id=1";
mysql_query($sql, $db);
print("Updated!") ;
}
?>
<br>
<br>
<a href="view.php">VIEW</a>
</body>

</html>

the edit entry file is create.php
Code:
<head>
<title>create.html</title>
<?php
$db = mysql_connect('localhost','******','******');
mysql_select_db('themalloys_uk_db',$db);

$requete = "SELECT * FROM test WHERE id=1";
$result = mysql_query ($requete,$db);
while($test = mysql_fetch_object($result)) {
   echo $test->name.'<br>';
   echo $test->email.'<br>';
   echo $test->hobby.'<br>';
   echo $test->comments.'<br>';
}
mysql_free_result($result); 
?>
</head>
<body>
<form method="post" action="edit.php">
<?php
print '<input type="text" value="' . $name . '" name="name">';
?>
<br>
Your email: <input type="text" name="email"><br>
Did you: <select name="hobby">
<option selected>eat
<option>sleep
<option>drink
</select>
Your comments:<br>
<textarea name="comments">
<?php echo $test->comments ?>
</textarea>
<br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

but its still not coming up trumps... suggestions?

Make Sense? I hope so (-:
 
The syntax for the SQL UPDATE command is somewhat different from the INSERT INTO.

Instead of:

Code:
$sql="UPDATE INTO test $_POST(name,email,hobby,comments) WHERE id=1";

Try:

Code:
$sql="UPDATE test SET name=$_POST['name'], email=$_POST['email'], hobby=$_POST['hobby'], comments=$_POST['comments'], WHERE id=1";
 
Error Message
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /data/members/free/tripod/uk/t/h/e/themalloys/htdocs/edit.php on line 10

Make Sense? I hope so (-:
 
Sorry, I got lazy. Try this:

Code:
$name=$_POST['name'];
$email=$_POST['email'];
$hobby=$_POST['hobby'];
$comments=$_POST['comments'];
$sql="UPDATE test SET name=\"$name\", email=\"$email\", hobby=\"$hobby\", comments=\"$comments\" WHERE id=1";
 
However, you might consider implementing:

Then you dont have to know what HTML is, to edit your content. Might be nice, if you want "noobs" to update content. In theory it produces XHTML.

I know of one bug though, if you delete everything in the editor, it still contains <p></p>, which then halts the editor.

You can however fix this:
if (strip_tags($row['content']) == "") {
$content = "";
}
else {
$content = $row['content'];
}

Then you can generate the editor on the variable $content.

It's very easy, actually, and quite nice.
For optimalization, you might want to switch an $action variable.

if $action == edit
show FCKeditor
else
show content, as-is
end if

ps. that is psuedocode!

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top