Jump to content

HTML select and option work with PHP database


sigmahokies
Go to solution Solved by cyberRobot,

Recommended Posts

Hi everyone,

 

I made script in php that working with HTML select and option, but I can't figure how to do that.

 

Is any website that can provide tutorial about select and option with php database. I mean, for example, I set up the 12 months in the select, then drop menu to pick an option, I pick July, then click on submit, then php display the list of name who have birthday on July. Get it? Please ask me if you are not sure you understand what i mean...

 

Thank you in advance time.

 

Gary

Link to comment
Share on other sites

Your options need the month as as the value and month name as the text

<select name='month'>
<option value='1'>January</option>
<option value='2'>February</option>
... etc

Process the month, which will be in $_GET['month'], and query the database with a query like

SELECT whatever FROM mytable WHERE MONTH(dateofbirth) = ?
Link to comment
Share on other sites

I did that, but I am just having little vague to make a logic to make it work, here my script in php:

 

<?php 
 
$Garydb = mysqli_connect("XXXXX","XXXXX") or die("Could not connect MySQL Database");
mysqli_select_db($Garydb, "XXXXX") or die("Could not find a Database");
 
if ($_POST['submitted']) {
$select = $_POST['birth'];
 
if ($select) {
$birthdate = "SELECT CONCAT(FirstName,' ',LastName) AS Birth FROM Members WHERE birthdate = ".$select;
$result = mysqli_query($Garydb, $birthdate);
if (mysqli_num_rows($result)) {
while($row = mysqli_fetch_assoc($result)) {
echo "<p>".$row['Birth']."</p>"; 
}
}
}
}
?>
<!doctype html>
<html>
<head>
<title>Test with option</title>
</head>
<body>
<h1>Please select the Members from RCD</h1>
<form action="option.php" method="POST">
<select name="birth">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<br />
<input type="submit" name="submitted" value="find member's birthday"></input>
</form>
</body>
<html>
Link to comment
Share on other sites

  • Solution

How is the "birthdate" field stored in your database? If it is stored as a string and only contains the month, you would need to add quotes around the value from $select.

$birthdate = "SELECT CONCAT(FirstName,' ',LastName) AS Birth FROM Members WHERE birthdate = '" . $select . "'";

Side note: you'll want to look into using prepared queries...or use mysqli_real_escape_string() to protect yourself from SQL injection attacks.

Edited by cyberRobot
Link to comment
Share on other sites

Let's try it out an example to see how it works.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>whatever title</title>
</head>
<body>
<form action="" method="get">
<select name="month">
	<option value="1" selected>January</option>
	<option value="2">February</option>
	<option value="3">March</option>
	<option value="4">April</option>
	<option value="5">May</option>
	<option value="6">June</option>
	<option value="7">July</option>
	<option value="8">August</option>
	<option value="9">September</option>
	<option value="10">October</option>
	<option value="11">November</option>
	<option value="12">December</option>
</select><br />
<input type="submit" value="submit">
</form>

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
define('br','<br />');

$sql = "CREATE TABLE IF NOT EXISTS Person(
id int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
firstname varchar(100),
lastname varchar(100),
dateofbirth DATETIME
)";

if (isset($_GET['month']))
{
    // grab the month from query string
    $month = (int)$_GET['month'];
    
    // database credentials
    $host = 'localhost';
    $user = 'db_user_name';
    $password = 'db_password';
    $database = 'db_name';
    
    // create our database connection
    $db = new mysqli($host, $user, $password, $database);
    $db->set_charset('utf8');
    
    if($db->connect_errno)
    {
        echo $db->connect_error . br;
        exit;
    }
    
    if(($db->query('DROP TABLE IF EXISTS Person')) === false)
    {
        echo $db->error . br;
        exit;
    }    
    if(($db->query($sql)) === false)
    {
        echo $db->error . br;
        exit;
    }
    date_default_timezone_set('UTC');
    $date1 = date('Y-m-d', strtotime('1969-09-04'));
    $date2 = date('Y-m-d',strtotime('1946-02-13'));
    $date3 = date('Y-m-d',strtotime('1984-07-10'));
    $sql = "INSERT INTO Person(firstname, lastname, dateofbirth) 
            VALUES('Paul','Ryan','$date1'),('Jessica','Sims','$date2'),('Gary','Kasper','$date3')";
    
    if(($db->query($sql)) === false)
    {
        echo $db->error . br;
        exit;
    }
    
    $sql = "SELECT firstname,lastname,dateofbirth FROM Person WHERE MONTH(dateofbirth) = ?";
    $stmt = $db->prepare($sql);
    $stmt->bind_param('i',$month);
    $stmt->execute();
    
    ?>
    <div style="display:block;padding:15px;">
    <?php
    
    $result = $stmt->get_result();

    if($row = $result->fetch_array(MYSQLI_ASSOC))
    {
            $birthday = date('F d, Y', strtotime($row['dateofbirth']));
            echo "{$row['firstname']} {$row['lastname']} was born on {$birthday}" . br;
    }
    else
    {
        echo "No one in database was born in that month.";
    } 
}
?>
</div>
</body>
</html>
Link to comment
Share on other sites

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.