Jump to content


MySQL and PHP form


7 replies to this topic

#1 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 18 July 2005 - 06:36 PM

Ok so i have a forum i want to use and it has the php already there. What i want is when you submit it to the database you can use the "enter" and it will go down a line or 2 depending on what you want.

Here is what i would like the out come to look like


Some discription 
**Skip a line**
More discription
**skip another line**
and so on

here is the file, what would i have to add to make it do that.

<?
//change database settings
    $usr = "usr";
    $pwd = "pwd";
    $db = "db";
    $host = "localhost";

    //connect to database
    $cid = mysql_connect($host,$usr,$pwd);
    mysql_select_db($db);
    if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

?>
<?
    //this is processed when the form is submitted
    //back on to this page (POST METHOD)
    //Define variables
    if ($_SERVER['REQUEST_METHOD'] == "POST")
    {
         //escape data and set variables
$title = $_POST["title"];
$review = $_POST["review"];
$posted_by = $_POST["posted_by"];
$date = date("j, n, Y");

//setup SQL statement
$sql = " INSERT INTO product_reviews ";
$sql .= " (title, review, posted_by, date) VALUES ";
$sql .="('$title','$review','$posted_by','$date')";

        //execute SQL statement
        $result = mysql_query($sql, $cid);

        //check for error
        if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

        print "<p>Your review has been added to our database. Thanks you.</p>\n";    
        

    }

?>

<form name="addreview" action="addreview.php" method="POST" style="font-family: Verdana; font-size: 8pt">
<p align="left"><br />
Review Title:
<input type="text" name="title" size="50" style="font-family: Verdana; color: #000000; font-size: 8pt; border: 1px solid #000000" />
<br />
<br />
Product_review:
<textarea name='review' rows=20 cols=40 style="font-family: Verdana; color: #000000; font-size: 8pt; border: 1px solid #000000"></textarea>
<br />
<br>
Your Name/Username:
<input type="text" name="pasted_by" size="16" style="font-family: Verdana; color: #000000; font-size: 8pt; border: 1px solid #000000" />
<br>
<br />
</p>
<ul>
<ul>
<ul>
<p align="center">
<input type="submit" value="Add Review" style="font-family: Verdana; color: #000000; font-size: 8pt; border: 1px solid #000000; float:left" /></p>
</form>

What would i have to add to make it do that. If you understand what i mean.

Edited by mbx5nitro, 18 July 2005 - 06:37 PM.


#2 MillerTime

    Young Padawan

  • Members
  • Pip
  • 69 posts

Posted 18 July 2005 - 07:07 PM

are you talking about a text area?

like going like this

where it can skip

a line

or two?

#3 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 18 July 2005 - 07:10 PM

Yes thats what i mean

#4 softLearner

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 19 July 2005 - 04:10 AM

I'm guessing this: $review = $_POST["review"] is variable for the textarea?

All you to do is chnage it to this: $review = nl2br($_POST["review"]);

nl2br returns new lines in to line breaks.

But this will be better if u add nl2br to the variable that holds the review when being called out of the database, as then it'll be easier for editing it.

#5 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 19 July 2005 - 10:46 AM

here is the file i need to view the reviews

<?
$usr = "nitrotech";
$pwd = "rachel";
$db = "nitro_ipb";
$host = "localhost";

$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
if (!$cid) { print "ERROR: " . mysql_error() . "\n"; }

?>
<?

    //setup SQL statement
    $sql  = " SELECT * FROM product_reviews ";

    //execute SQL statement
    $rs = mysql_query($sql, $cid);
    if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }

    //display results
    while ($row = mysql_fetch_array($rs))
    {
//define variables
        $title = $row["title"];
        $review = $row["review"];
        $posted_by = $row["posted_by"];
        $date = $row["date"];

        print "$title<br /><br />$review<br /><br />Posted by: $posted_by on $date.\n";
    }

?>


so what do i cange
$review = $row["review"]; to? $review = nl2br($row["review"]); is that right?

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 19 July 2005 - 12:15 PM

You can either do it like you just said, or like this:
print "$title<br /><br />".nl2br($review)."<br /><br />...";

It's entirely up to you ;)

#7 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 19 July 2005 - 02:39 PM

ok cool, that wasnt that hard

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 19 July 2005 - 08:54 PM

Believe me, after you figure something out in php, it's never "that hard." Up untill that point you may be tempted to shoot yourself.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users