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
Direct
Forum Newbie
Posts: 6 Joined: Tue Jun 01, 2010 10:11 pm
Post
by Direct » Tue Jun 01, 2010 10:16 pm
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>" ;
?>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Jun 01, 2010 11:06 pm
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
Post
by Direct » Wed Jun 02, 2010 12:08 am
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
Post
by Direct » Wed Jun 02, 2010 1:14 am
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
Post
by Direct » Wed Jun 02, 2010 4:32 pm
BUMP
Direct
Forum Newbie
Posts: 6 Joined: Tue Jun 01, 2010 10:11 pm
Post
by Direct » Wed Jun 02, 2010 8:42 pm
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