Created
February 1, 2015 18:31
-
-
Save ahmadidev/13f89a918d5462001c87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function addItem($itemValue) | |
{ | |
$servername = "localhost"; | |
$username = "username"; | |
$password = "password"; | |
$dbname = "test"; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
$sql = "INSERT INTO Items(ItemName) VALUES('$itemValue')"; | |
if ($conn->query($sql) === TRUE) { | |
echo "New record created successfully"; | |
} else { | |
echo "Error: " . $sql . "<br>" . $conn->error; | |
} | |
$conn->close(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$method = $_SERVER["REQUEST_METHOD"]; | |
if($method == "POST") | |
{ | |
$username = $_POST["username"]; | |
$length = strlen($username); | |
if($length > 4 && $length < 10) | |
{ | |
addItem($username); | |
$message = "the user $username successfully added to database."; | |
//Clear username to prepare for next entry | |
$username = ""; | |
} | |
else | |
{ | |
$message = "Username should be between 4 and 10 characters"; | |
} | |
} | |
?> | |
<p> | |
<?php echo $message ?> | |
</p> | |
<form method="post"> | |
<input type="text" name="username" value="<?php echo $username ?>"/> | |
<input type="submit" value="Submit" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment