xiao Posted March 25, 2006 Share Posted March 25, 2006 Hi,I just started learning PHP a couple of days ago and right now I'm writing a script to insert tutorials into a database. Everything works fine, except that when someone visits "insert.php" it enters empty fields into the database, while it shouldn't enter anything. And when someone refreshes the page after submitting, the data will be entered again.My HTML form looks like this:[code]<html><body><form action="insert.php" method="post">Title: <input type="text" name="title"><br>Description: <input type="text" name="description"><br>Image: <input type="text" name="image"><br>URL: <input type="text" name="url"><br><input type="Submit"></form></body></html>[/code] and insert.php looks like this:[code]<html><head><title>Data inserted</title></head><body><?php$username="uname";$password="pword";$database="tutorials";$title=$_POST['title'];$description=$_POST['description'];$image=$_POST['image'];$url=$_POST['url'];}mysql_connect(localhost,$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="INSERT INTO tutorials VALUES('','$title','$description','$image','$url')";mysql_query($query);mysql_close();echo "data succesfully inserted into database";?><br><br><a href="insert.html">Enter another tutorial?</a></body></html>[/code] I figured out that I will probably have to use an IF statement and I got this:[code]if ( $title == "" ) {echo "<meta http-equiv='refresh' content='0; url=error.php'>";}[/code] But that would only cover the empty ones... :pSome help please? Quote Link to comment https://forums.phpfreaks.com/topic/5765-disallow-empty-field-in-mysql/ Share on other sites More sharing options...
firedrop84 Posted March 25, 2006 Share Posted March 25, 2006 I think your problem is with your SQL query. as usually when u want to insert anything into your database you should do like the example belowINSERT INTO `$tablename` ( `EmployeeID` , `Surname` , `GivenName` , `Username` , `Password` , `Manager`) VALUES ('','$EmployeeSurname','$EmployeeGivenName','$EmployeePreferredName','$EmployeePassword','$Manager')";just be aware that I am not using for the attributes in the tabe the single quation I mean ' I am using ` but when u enter the values use the single qutation '.I am not really good into PHP but hopefully this helped you Quote Link to comment https://forums.phpfreaks.com/topic/5765-disallow-empty-field-in-mysql/#findComment-20539 Share on other sites More sharing options...
xiao Posted March 25, 2006 Author Share Posted March 25, 2006 I'm pretty sure that's not my problem Quote Link to comment https://forums.phpfreaks.com/topic/5765-disallow-empty-field-in-mysql/#findComment-20553 Share on other sites More sharing options...
alpine Posted March 28, 2006 Share Posted March 28, 2006 <form action="insert.php" method="post">Title: <input type="text" name="title"><br>Description: <input type="text" name="description"><br>Image: <input type="text" name="image"><br>URL: <input type="text" name="url"><br><input type="Submit" [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]name="submitbutton"[!--colorc--][/span][!--/colorc--]></form><?[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]if(isset($_POST['submitbutton'])[!--colorc--][/span][!--/colorc--]{// determine all the post variables and do the query stuff inbetween here}else{print "No post variables present";}?> Quote Link to comment https://forums.phpfreaks.com/topic/5765-disallow-empty-field-in-mysql/#findComment-21550 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.