3

I have a php which would check for certain value if it exists in a mysql database. If the value does not exists, it would simply add the value and refresh the page once to load the page again and now it has a value in the database, would go ahead to add other values. How do I refresh page just once when it is called ?

<?php $sname = "W3 schools C# tutorials";//$_POST["sitename"]; $stype = "C#";//$_POST["sitetype"]; $saddy = "www.w3schools.com";//$_POST["siteaddress"]; $scomm = "W3 schools C# tutorials";//$_POST["sitecomment"]; $conn = mysql_connect("localhost","root","password"); if(!$conn){ die("Could not connect: ".mysql_error()); } else { mysql_select_db("bookmarks",$conn); $rs = mysql_query("select TypeId from bookmarktypes where TypeName = '$stype'"); $row = mysql_fetch_array($rs); if($row > 0 ){ //Data found, continue to add... } else { //No data... insert a valid one $rs = mysql_query("insert into bookmarktypes (TypeName) values ('$stype')"); if (!$rs){ die('Error: ' . mysql_error()); } else { //echo "inserted new type data..."; } //echo "</html>"; } } mysql_close($conn); //Refresh page once ?> 

There's the comment to refresh page below after mysql close command.

3 Answers 3

8

Refresh it right after insert with

header('Location: url here'); exit; 

Btw, read a little about sql injections

Also - mysql_close() is pointless there.

Sign up to request clarification or add additional context in comments.

3 Comments

He should use PDO instead. Also good think to learn about injections, but better to use PDO parameterized queries. Also he should not output anything else he is going to get an error when redirecting.
You should add the usual warning with header: make sure you do not output anything before using it. +1 for the SQL injection memo.
Thanks. I would get to the sql injection part when the function is running.
2
if(check=1) { echo "\"<meta http-equiv=\"refresh\" content=\"2;url=http://yourwebsite.com/\">\"\n"; } 

1 Comment

Hmmm.... I think it would be better if it's a native php method to refresh the page ? What do you think ?
0

if you need to print the data that you just have entered try this

header('Location: YourShowDataPage.php?id='.$_POST['id_dataEntered']) 

mi apologizes if is wrong , im a begginer

1 Comment

Quick notice: instantly printing a $_POST variable without sanitizing the content could lead to cross site scripting vunerabilities

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.