4 Replies - 874 Views - Last Post: 06 May 2010 - 10:07 AM Rate Topic: -----

#1 KiNgY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 07-March 08

Controlling user input text

Posted 05 May 2010 - 05:11 AM

Okay so what I would like to do is create a dynamic webpage, it's pretty simple, all I want to do is display some text that is stored in a database (MySQL), and make a script to edit the content in the database that is going to get displayed. I have a big textarea box for the main content and i want to be able to keep the paragraphs that the user puts in the textarea box, i thought maybe preg_replace but all i have managed to do is replace all Enter Key Characters into </p><p class="content">. I also want to change the </p><p class="content"> back into the Enter Key Character so that when it is being edited again the user doesn't see the html coding, I dont know if i have made sense lol hopefully my code will explain more....

<?
include("db_connect.php");

if(isset($_POST['update'])) {

$update_heading = $_POST['heading'];
$update_content = $_POST['content'];

$update_content = preg_replace( '/\r\n/', '</p><p class="content">', trim($update_content) );

$query = "UPDATE contact SET heading='".$update_heading."', content='".$update_content."' WHERE id='1'";
$result = mysql_query($query);

echo "Congratulations, Daniel your an insignificant fool !";

}

$row = mysql_query("SELECT * FROM  `contact` WHERE id='1'");
$contact = mysql_fetch_assoc($row);

?>

<html>
<body style="margin-top: 40px; text-align: center;">
<form action="contact.php" method="post">
<input type="text" name="heading" style="width: 700px; height: 35px;" value="<?=$contact['heading'];?>"><br>
<textarea name="content" style="width: 700px;" rows="35"><?=$contact['content'];?></textarea>
<input type="submit" value="Update The Contact Page" name="update" style="width: 700px; height: 35px;">
</form>

</body>
</html>



Thank you to anyone who can help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Controlling user input text

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Controlling user input text

Posted 05 May 2010 - 05:59 AM

Well I don't have your database, so I just fixed the variables. In order to avoid sql injection attacks, I'd use mysql_real_escape_string() on user inputs before putting them into any queries.
<html>
<body style="margin-top: 40px; text-align: center;">
<?
//include("db_connect.php");
if(isset($_POST['update'])) {

$update_heading = $_POST['heading'];
$update_content = $_POST['content'];

$contact =  trim($update_content); //preg_replace( '/\r\n/', '</p><p class="content">', trim($update_content) );

//$query = "UPDATE contact SET heading='".$update_heading."', content='".$update_content."' WHERE id='1'";
//$result = mysql_query($query);

echo "Congratulations, Daniel you're an insignificant fool !";
}

//$row = mysql_query("SELECT * FROM  `contact` WHERE id='1'");
//$contact = mysql_fetch_assoc($row);

?>


<form action="may5.php" method="post">
<input type="text" name="heading" style="width: 700px; height: 35px;" value="<?=$update_heading;?>"><br>
<textarea name="content" style="width: 700px;" rows="35"><?=$contact;?></textarea>
<input type="submit" value="Update The Contact Page" name="update" style="width: 700px; height: 35px;">
</form>

</body>
</html>


Was This Post Helpful? 2
  • +
  • -

#3 KiNgY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 07-March 08

Re: Controlling user input text

Posted 06 May 2010 - 09:35 AM

Hey, thank you CTphpnwb for replying, I don't suppose you could explain what you have changed I don't quite understand?

thanks again, Daniel
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Controlling user input text

Posted 06 May 2010 - 09:54 AM

I've simply matched up your posted values with the output. What you need to do is use that data to update your database as well as display it. I suggest you start using functions or this will get complicated quickly.
Was This Post Helpful? 0
  • +
  • -

#5 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: Controlling user input text

Posted 06 May 2010 - 10:07 AM

Edit CTphpnwb already responded.

About keeping <p></p> could you use echo nl2br($content); to show the content?

This post has been edited by KingCuddles: 06 May 2010 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1