sigmahokies Posted October 13, 2015 Share Posted October 13, 2015 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2015 Share Posted October 13, 2015 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) = ? Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted October 13, 2015 Author Share Posted October 13, 2015 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> Quote Link to comment Share on other sites More sharing options...
Barand Posted October 13, 2015 Share Posted October 13, 2015 Your option values do not look like numbers to me. And your WHERE condition is nothing like mine. So which bit of it did you do? Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 13, 2015 Share Posted October 13, 2015 If you're storing the month name in the database. Your storing the data wrong Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted October 14, 2015 Solution Share Posted October 14, 2015 (edited) 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 October 14, 2015 by cyberRobot Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted October 14, 2015 Author Share Posted October 14, 2015 (edited) Hi CyberRobot, I will try to do that...Thank you, but it hasn't solve yet, but pretty close now. Edited October 14, 2015 by sigmahokies Quote Link to comment Share on other sites More sharing options...
hansford Posted October 15, 2015 Share Posted October 15, 2015 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.