Skip to content

Instantly share code, notes, and snippets.

@ahmadidev
Created February 1, 2015 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadidev/13f89a918d5462001c87 to your computer and use it in GitHub Desktop.
Save ahmadidev/13f89a918d5462001c87 to your computer and use it in GitHub Desktop.
<?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