Jump to content

Recommended Posts

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

Some help please?
Link to comment
https://forums.phpfreaks.com/topic/5765-disallow-empty-field-in-mysql/
Share on other sites

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

?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.