Page 1 of 1

Help with confirming post

Posted: Tue Jun 01, 2010 10:16 pm
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>" ;
 



?>

Re: Help with confirming post

Posted: Tue Jun 01, 2010 11:06 pm
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.

Re: Help with confirming post

Posted: Wed Jun 02, 2010 12:08 am
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

Re: Help with confirming post

Posted: Wed Jun 02, 2010 1:14 am
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.

Re: Help with confirming post

Posted: Wed Jun 02, 2010 4:32 pm
by Direct
BUMP :D

Re: Help with confirming post

Posted: Wed Jun 02, 2010 4:51 pm
by requinix
Read the documentation for mysql_fetch_array very carefully.

Re: Help with confirming post

Posted: Wed Jun 02, 2010 8:42 pm
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