HI, i have written a bookmarking page that lets me add urls to database. as i have placed both database results and new url button code in same page. i have a bit conflict. whenever i refresh the page, dot entry goes in database and table gets populated which is flaw i guess. i am looking for a way to fix this.


here is my code

<html>

<style type="text/css">
table.db-table     { border-right:1px solid #ccc; border-bottom:1px solid #ccc; }
table.db-table th  { background:#eee; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
table.db-table td  { padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
</style>

<?php

mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("delicious") or die(mysql_error()); 
 
$data = mysql_query("SELECT * FROM stacks") or die(mysql_error()); 

if(mysql_num_rows($data)) {
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>No</th><th>Name</th><th>URL</th></tr>';

while($row2 = mysql_fetch_row($data)) 
 {
echo '<tr>';
foreach($row2 as $key=>$value) 
 {
echo '<td>',$value,'</td>';
 }
echo '</tr>';
}
echo '</table><br />';
}

$Name=$_POST["Name"];
$URL=$_POST["URL"];

$query="INSERT INTO stacks (No,Name,URL) values ('NULL','.$Name','.$URL')";
mysql_query($query) or die('Error updating database');
?>

<form method="post" action="php_code.php">

<b>Name : </b>
<input type="text" name="Name" size="30" /><br/>

<b>URL : </b>
<input type="text" name="URL" size="30" /><br/>

<input type="submit" value="Add another URL"/>


</form>


</html>

Recommended Answers

All 4 Replies

Member Avatar for diafol
$query="INSERT INTO stacks (No,Name,URL) values ('NULL','.$Name','.$URL')";

Why the dots?

also shouldn't it be:

echo "<td>$value</td>";

#

and

$key=>$value

you don't use $key, so how about just:

... as $value

You have:

$Name=$_POST["Name"];
$URL=$_POST["URL"];

Will probably throw an error is post not sent:

if(isset($_POST["Name"]) && isset($_POST["URL"])){
  $Name = ...
  //and insert sql
}

Thanks. I just updated the code by removing the dot as per your suggestion.

As for

echo "<td>$value</td>";

Output is : 'url name' and 'http://sdfdf.com'. so it is the reason i added , and ' in the code. so to avoid that ' ' in output table using the , ' in code.

I used if block and managed to avoid the simple refresh bug. but this time when i refresh the browser it adds the previous entry in the text field. code is slightly changed as you pointed out. I am not sure how to avoid that previously filled data in refresh condition in browser.

Member Avatar for diafol

You should try to avoid sending the form to itself - this is a well known problem with page refresh causing the resending of form data. My preferred method is to send the data to a different page (form handler) and then redirecting via header() back to the form page.

form_handler.php

<?php
if(isset($_POST["Name"]) && isset($_POST["URL"])){
  //make simple variables and clean them with mysql_real_escape_string
  //do insert and check whether successful or not
  if(mysql_affected_rows()){
     $msg = "success";
  }else{
     $msg = "fail";
  }
}else{
   $msg = "nodata";
}
header("Location: form.php?msg=$msg");
exit;
?>

That's it - no need for any more

form.php

<?php
//do select and set up display table

$messages = array("success"=>"The record was added to the database","nodata"=>"You must add a name and a URL","fail"=>"There was a problem adding the data to the DB. Try again.");

$msg = (isset($_GET['msg']) && isset($messages[$_GET['msg']])) ? "<div>" . $messages[$_GET['msg']] . "</div>" : '';
?>
...DTD, head area etc...

<form method="post" action="form_handler.php">
<b>Name : </b>
<input type="text" name="Name" size="30" /><br/>
<b>URL : </b>
<input type="text" name="URL" size="30" /><br/>
<input type="submit" value="Add another URL"/>
</form>
<?php echo $msg;?>

...rest of page...

Just a few thoughts.

I just got feedback from diaspora on this code. Most of the users seem to be agreeing with you. USing same page for the data update using php is not good idea. THey are saying using jquery or some other jscript handler to create pop up dialog and then updating or refreshing the page is much better way. I think for now i need to create a new link that opens form in another page and database results on another.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.