Jump to content


Photo

Disallow empty field in mysql


  • Please log in to reply
3 replies to this topic

#1 xiao

xiao
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 25 March 2006 - 01:07 PM

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:
<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>


and insert.php looks like this:
<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>


I figured out that I will probably have to use an IF statement and I got this:
if ( $title == "" ) {
echo "<meta http-equiv='refresh' content='0; url=error.php'>";
}


But that would only cover the empty ones... :P

Some help please?

#2 firedrop84

firedrop84
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 25 March 2006 - 02:02 PM

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 below

INSERT 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

#3 xiao

xiao
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 25 March 2006 - 03:33 PM

I'm pretty sure that's not my problem

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 March 2006 - 12:48 PM

<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";
}

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users