music_lp90 Posted May 4, 2007 Share Posted May 4, 2007 Hi, I'm having a problem getting this code to work. I've done a couple if statements before, and this code seems to make sense to me, but it doesn't work. If someone could tell me what I'm doing wrong, I would greatly appreciate it. What I want to do is prevent the php from inserting blank rows into the table. I don't want the form to require that text is entered, each box should be optional. The php: <?php$dbh=mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("dbName");$news1 = $_POST[news1];$news2 = $_POST[news2];$news3 = $_POST[news3];if ($news1 !== "")$sql="INSERT INTO news (statement)VALUES('$news1')";elseif ($news2 !== "")$sql="INSERT INTO news (statement)VALUES('$news2')";elseif ($_POST[news3] !== "")$sql="INSERT INTO news (statement)VALUES('$news3')";elseecho "No entries made";if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); }echo "News or announcement has been added. <br />";?><?php$query = mysql_query('SELECT * FROM `news`');while ($results = mysql_fetch_array($query)){echo $results['id'] ." " . $results['date']." " . $results['statement'] . '<br />';}?> The html form: <form method="post" action="insert_news.php"> Announcement 1:<br /><textarea name="news1" rows="3" cols="80"></textarea> Announcement 2:<br /><textarea name="news2" rows="3" cols="80"></textarea> Announcement 3:<br /><textarea name="news3" rows="3" cols="80"></textarea> <br /> <input type="submit" value="Add News or Announcement" /></form> Thanks for any help! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 4, 2007 Share Posted May 4, 2007 What's the problem?This won't fix anything, but you need to use quotes in this case, you're not working with constants here:$news1 = $_POST['news1'];$news2 = $_POST['news2'];$news3 = $_POST['news3']; Link to comment Share on other sites More sharing options...
ScottR Posted May 4, 2007 Share Posted May 4, 2007 It appears your if else if statement is missing the {}. All conditional statements should appear with those brackets. if (something) {code here} else if (something) {code here} Link to comment Share on other sites More sharing options...
music_lp90 Posted May 4, 2007 Author Share Posted May 4, 2007 What's the problem?This won't fix anything, but you need to use quotes in this case, you're not working with constants here:$news1 = $_POST['news1'];$news2 = $_POST['news2'];$news3 = $_POST['news3'];Sorry I forgot to include exactly what the problem was. It would only post one of the values.Thanks for pointing out that the single quotes were missing. It works the same either way, though, they both gave me the correct values.Here's another way I've tried the php. This way submits all 3 values if all three textareas have something typed in them, but if only two have something typed in them, then it inserts the value of the second textarea twice or if only one text area has been typed in, it inserts the data 3 times.<?php $dbh=mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("dbName");$news1 = $_POST['news1']; $news2 = $_POST['news2']; $news3 = $_POST['news3']; if ($news1 !== "") $sql="INSERT INTO news (statement) VALUES ('$news1')"; else echo "Hi"; {if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); } } if ($news2 !== "") $sql="INSERT INTO news (statement) VALUES ('$news2')";else echo "Hi"; {if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); } }if ($news3 !== "") $sql="INSERT INTO news (statement) VALUES ('$news3')";else echo "Hi"; {if (!mysql_query($sql,$dbh)) { die('Error: ' . mysql_error()); } }echo "News or announcement has been added. <br />"; ?> <?php $query = mysql_query('SELECT * FROM `news`'); while ($results = mysql_fetch_array($query)){ echo $results['id'] ." " . $results['date']." " . $results['statement'] . '<br />'; } ?> Basically what I'm trying to do is this.If someone typed into textarea 1 then insert that data into a mysql table. If they didn't, then don't insert any data.If someone typed into textarea 2 then insert that data into a mysql table. If they didn't, then don't insert any data.If someone typed into textarea 3 then insert that data into a mysql table. If they didn't, then don't insert any data.I hope this helps clarify my problem. Thanks for looking at it. Link to comment Share on other sites More sharing options...
ScottR Posted May 4, 2007 Share Posted May 4, 2007 If thats the case you might want to look into switch conditional statements, or try catch conditional statements. Sounds to me like that may be right up your alley. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 4, 2007 Share Posted May 4, 2007 Ok, that's easy enough. Look at what you're telling it to do: if ($news1 !== "")$sql="INSERT INTO news (statement)VALUES('$news1')";elseif ($news2 !== "")$sql="INSERT INTO news (statement)VALUES('$news2')";elseif ($_POST[news3] !== "")$sql="INSERT INTO news (statement)VALUES('$news3')";elseecho "No entries made"; That is saying this:if (news1 not empty) insertelse if (news2 not empty) insertelse if (news3 not empty) insertelse nothingYou are telling it to only insert news2 if news1 is empty, that's what the else says. So get rid of the else and move the query up, instead of just writing the SQL code to a variable. And you also need to escape the variables, because they are coming from $_POST. You can't trust anything from $_GET, $_POST, or $_COOKIE to be safe. Since this is text data, you need to use mysql_real_escape_string. if ($news1 !== "") mysql_query("INSERT INTO news (statement) VALUES ('" . mysql_real_escape_string($news1) . "')");if ($news2 !== "") mysql_query("INSERT INTO news (statement) VALUES ('" . mysql_real_escape_string($news2) . "')");if ($news3 !== "") mysql_query("INSERT INTO news (statement) VALUES ('" . mysql_real_escape_string($news3) . "')");if ($news1 == "" && $news2 == "" && $news3 == "") echo "No entries made"; It appears your if else if statement is missing the {}. All conditional statements should appear with those brackets.The brackets are only necessary if the block of statements to execute is more then one statement. Only one statement does not need brackets. This would work fine:if () for (;;) if () while () <statement> Link to comment Share on other sites More sharing options...
music_lp90 Posted May 4, 2007 Author Share Posted May 4, 2007 if () for (;;) if () while () <statement> Thanks for the reply. I'm trying to understand this, but I'm not quite getting it. Can you explain what the for(; statement is for?And I just want to make sure I'm being clear about one other thing. I want the user to be able to submit up to three announcements if neccessary, but also be able to only submit 1 or 2 if thats all they need. Link to comment Share on other sites More sharing options...
music_lp90 Posted May 4, 2007 Author Share Posted May 4, 2007 Ok, it works great with the code you provided, thanks, I really appreciate it. I'm still not sure about code on the bottom. I haven't used for before since I'm still very new to php. Link to comment Share on other sites More sharing options...
jesh Posted May 4, 2007 Share Posted May 4, 2007 Can you explain what the for(; statement is for?It's a type of loop. Check out this page for more info about loops in PHP:http://www.w3schools.com/php/php_looping.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.