Help with confirming post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Direct
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:11 pm

Help with confirming post

Post by Direct »

I am trying to make sure the text box has something in it before it inserts the information into the DB. What I ahve seems like it should would but I guess not.

Code: Select all

<?php
//Connect
$conn = mysql_connect('localhost' , 'root' , 'pass')or die(mysql_error()); 
$db = mysql_select_db('log_db') or die(mysql_error());




if($_POST['message'] == ""){
	 echo"insert message" ;
	}
else{
     mysql_query("INSERT INTO messages (message)
     VALUES ('{$_POST['message']}')")or die(mysql_error());
}


echo"<form method='post' action=''>
     <textarea name='message' rows='10' cols='50'>
	 </textarea>
	 <br/>
	 <input type='submit' value='Post' />
	 </form>" ;
 



?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with confirming post

Post by requinix »

Right now the textbox isn't empty because it has something in it: a newline. That is, there's more than one line in the box.

Either use

Code: Select all

<textarea name='message' rows='10' cols='50'></textarea>
or use trim.


And right now - yes, this very second - you need to start using mysql_real_escape_string.
Direct
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:11 pm

Re: Help with confirming post

Post by Direct »

Thanks so much that has been bothering me for a few days

About the escape string this is just a test but I know how important it is
Direct
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:11 pm

Re: Help with confirming post

Post by Direct »

Code: Select all

<?php
//Connect
$conn = mysql_connect('localhost' , 'root' , '')or die(mysql_error()); 
$db = mysql_select_db('log_db') or die(mysql_error());

$query = "SELECT message FROM messages" ;
$results = mysql_query($query) ;
$message = mysql_fetch_array($results) ;

if($_POST['message'] == ""){
	 echo"insert message" ;
	}
else{
     mysql_query("INSERT INTO messages (message)
     VALUES ('{$_POST['message']}')")or die(mysql_error());
}

echo"<table border=1 cellspacing=0>" ;
foreach ($message as $id=>$mess){
     echo"<tr><td>$mess</td></tr>" ;
}
echo"</table>" ;

echo"<form method='post' action=''>
     <textarea name='message' rows='10' cols='50'></textarea>
	 <br/>
	 <input type='submit' value='Post' />
	 </form>" ;
 



?>

Quick help this this as well, this echos two of the same row. There is only one row.
Direct
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:11 pm

Re: Help with confirming post

Post by Direct »

BUMP :D
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with confirming post

Post by requinix »

Read the documentation for mysql_fetch_array very carefully.
Direct
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:11 pm

Re: Help with confirming post

Post by Direct »

If somebody could explain what I am doing wrong I would appreciate it. I have been teaching myself php for like a month so I am not that adept at it
Post Reply