kelliethile Posted April 8, 2010 Share Posted April 8, 2010 Hi guys, I'm doing a To-Do-List management kind of project for school. I'm a newbie at PHP - programming in general. I was wondering, everytime I refreshes/F5 the page, the last item would be added automatically. How to change that? Here's my code (just try typing in an item, and refreshing it, you'll see what I mean) <html> <head><title>Add To List</title> <body> <form method="post" action="todolist.php"> <label for="item">Add item: </label> <input type="text" name="item" id="item" size="30"> <br /> <label for="tag">Add tag: </label> <select name="tag"> <option value="Personal" selected="selected">Personal</option> <option value="School">School</option> <option value="Selling">Selling</option> </select> <br /> <input type="submit" value="Add to list" /> </form> <?php // Connecting, selecting database $link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error()); echo ''; mysql_select_db('productivity') or die('Could not select database'); // Inserting data if (isset($_POST['item'])) { $item = mysql_real_escape_string($_POST['item']); $tag = mysql_real_escape_string($_POST['tag']); // Performing SQL query $query = "INSERT INTO todo_table (item, tag) VALUES ('$item', '$tag')"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); } ?> <h2>Things To Do</h2> <?php // Selecting the data $resultselect = mysql_query('SELECT * FROM todo_table'); // Fetching and displaying data while($row = mysql_fetch_array($resultselect)) { ?> <input type="checkbox" name="done[]" id="<?php echo $row['id'] ?>" value ="<?php echo $row['id'] ?>" /> <label for="<?php echo $row['id'] ?>"> <?php echo $row['item'] . ' - tagged ' . $row['tag'];?> </label> <br /> <?php } ?> <input type="submit" value="Mark Items Complete" /> </form> </body> </head> Please help, thanks (: Link to comment https://forums.phpfreaks.com/topic/197959-rowitem-add-on-previous-item-once-i-refreshs-page/ Share on other sites More sharing options...
Pikachu2000 Posted April 8, 2010 Share Posted April 8, 2010 Check to se if the db INSERT query was successful, then reinitialize the $_POST array if( mysql_affected_rows() > 0 ) { $_POST = array(); } Link to comment https://forums.phpfreaks.com/topic/197959-rowitem-add-on-previous-item-once-i-refreshs-page/#findComment-1038788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.