PHP_CHILD Posted January 23, 2013 Share Posted January 23, 2013 so i am doing this very basic shoutbox...Where the page refreshes, the previous data of post gets inserted to the database... How do i stop this? Thanks.. $a=isset($_POST['name']) && $_POST['name']; $b=isset($_POST['message']) && $_POST['message']; $value55=mysql_real_escape_string($_POST[name]); $value56=mysql_real_escape_string($_POST[message]); $sql="INSERT INTO shout(name,post) VALUES ('$value55','$value56')"; $c=mysql_query($sql,$con) or die(mysql_error()); if($a&&$b&&$c) { $query6="SELECT `name`, `post` FROM `shout` "; $result6=mysql_query($query6) or die(mysql_error()); while($row5=mysql_fetch_array($result6)) { $a=stripslashes($row5['name']); $b=stripslashes($row5['post']); echo "Name".$a."<br>"; echo "message".$b."<br>"; } } Link to comment https://forums.phpfreaks.com/topic/273548-post-values-gets-inserted-with-each-page-loads/ Share on other sites More sharing options...
PaulRyan Posted January 23, 2013 Share Posted January 23, 2013 <?PHP //### Start the session session_start(); //### MySQL credentials $DBhost = '127.0.0.1'; $DBuser = 'username'; $DBpass = 'password'; $DBname = 'database name'; //### Connect to the database $connect = mysql_connect($DBhost,$DBuser,$DBpass); mysql_select_db($DBname,$connect); //### If the form is submitted, process data if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Assign data $name = mysql_real_escape_string($_POST['name']); $message = mysql_real_escape_string($_POST['message']); //### Validation if(!$name) { echo 'Please enter your name'; } else if(!$message) { echo 'Please enter a message.'; } else { $addShoutQuery = "INSERT INTO `shout` (`name`, `post`) VALUES ('{$name}', '{$message}')"; $addShout = mysql_query($addShoutQuery); if(!mysql_affected_rows()) { echo 'Unable to add message'; } else { $_SESSION['message'] = 'Message has been added'; header('Location: ?'); exit; } //### End query check } //### End validation } //### End post process //### Retrieve shouts from database $selectShoutsQuery = "SELECT `name`, `post` FROM `shout` ORDER BY `id` DESC"; $selectShouts = mysql_query($selectShoutsQuery) or die(mysql_error()); if(!mysql_num_rows($selectShouts)) { echo 'No messages posted.'; } else { $shoutBox = ''; //### Display messages from database while($row = mysql_fetch_assoc($selectShouts)) { $shoutBox .= "{$row['name']}: {$row['post']} <br>".PHP_EOL; } } if(isSet($_SESSION['message'])) { echo $_SESSION['message'] .'<br>'.PHP_EOL; unset($_SESSION['message']); } ?> <form method="POST" action="index.php"> Name: <input type="text" name="name" value=""> <br> Message: <input type="text" name="message" value=""> <br> <input type="submit" value="Post Message"> </form> <br> <?PHP echo $shoutBox;?> Try that out. Should probably look into MySQLi also, instead of MySQL, not much to change in the code. Link to comment https://forums.phpfreaks.com/topic/273548-post-values-gets-inserted-with-each-page-loads/#findComment-1407757 Share on other sites More sharing options...
PHP_CHILD Posted January 27, 2013 Author Share Posted January 27, 2013 <?PHP //### Start the session session_start(); //### MySQL credentials $DBhost = '127.0.0.1'; $DBuser = 'username'; $DBpass = 'password'; $DBname = 'database name'; //### Connect to the database $connect = mysql_connect($DBhost,$DBuser,$DBpass); mysql_select_db($DBname,$connect); //### If the form is submitted, process data if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Assign data $name = mysql_real_escape_string($_POST['name']); $message = mysql_real_escape_string($_POST['message']); //### Validation if(!$name) { echo 'Please enter your name'; } else if(!$message) { echo 'Please enter a message.'; } else { $addShoutQuery = "INSERT INTO `shout` (`name`, `post`) VALUES ('{$name}', '{$message}')"; $addShout = mysql_query($addShoutQuery); if(!mysql_affected_rows()) { echo 'Unable to add message'; } else { $_SESSION['message'] = 'Message has been added'; header('Location: ?'); exit; } //### End query check } //### End validation } //### End post process //### Retrieve shouts from database $selectShoutsQuery = "SELECT `name`, `post` FROM `shout` ORDER BY `id` DESC"; $selectShouts = mysql_query($selectShoutsQuery) or die(mysql_error()); if(!mysql_num_rows($selectShouts)) { echo 'No messages posted.'; } else { $shoutBox = ''; //### Display messages from database while($row = mysql_fetch_assoc($selectShouts)) { $shoutBox .= "{$row['name']}: {$row['post']} <br>".PHP_EOL; } } if(isSet($_SESSION['message'])) { echo $_SESSION['message'] .'<br>'.PHP_EOL; unset($_SESSION['message']); } ?> <form method="POST" action="index.php"> Name: <input type="text" name="name" value=""> <br> Message: <input type="text" name="message" value=""> <br> <input type="submit" value="Post Message"> </form> <br> <?PHP echo $shoutBox;?> Try that out. Should probably look into MySQLi also, instead of MySQL, not much to change in the code. thank u.. will try Link to comment https://forums.phpfreaks.com/topic/273548-post-values-gets-inserted-with-each-page-loads/#findComment-1408547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.