As the title said, I want to display the specific row of data based on the user selection of the year.
If user selected 2017, I only want to display rows of data that are created in the year of 2017.
I know there are something wrong with my query, I am trying to understand how can I write the query. Can anyone please help me? Thank you in advance!
<form action="Report1.php" method="POST">
Select Year:<select name="sortyear" id="sortyear">
<option>2017</option>
<option>2018</option>
</select>
<?php
$sortyear = $_POST['sortyear'];
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = $sortyear)";
$result = mysqli_query($db,$sql);
?>
</form>
Ignoring security flaws, you could do the following:
<?php $getYears = sqlquery() ?>
<form action="Report1.php" method="POST">
<select name="sortyear" id="sortyear">
<?php for ($i = 0; $i < $getYears.length; $i++): ?>
<option value="<?php echo $getYears[$i];?>"><?php echo $getYears[$i];?></option>
<?php endif; ?>
</select>
You can do
<?php for () {
}?>
but the for: allows you to drop back to html code and get your syntax highlighting ;)
I managed to solve it by adding a button
<input type ="submit" name="search" class="btn btn-sm btn-primary" value="Filter" style="background-color: #474747;padding: 5px 5px !important;margin-bottom:5px;">
and coded the php this way
if(isset($_POST['search']))
{
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$sortyear = $_POST['sortyear'];
$sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = $sortyear)";
$result = mysqli_query($db,$sql);
}
You could try this:
<form action="Report1.php" method="POST">
Select Year:<select name="sortyear" id="sortyear">
<option>2017</option>
<option>2018</option>
</select>
<input type="submit" name="submit" value="Get Annual Data" />
<?php
if(isset($_POST['submit'])){
$sortyear = $_POST['sortyear'];
$db = mysqli_connect('localhost','root','','customercaremodule');
date_default_timezone_set('Asia/Kuala_Lumpur');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$sql = "SELECT first_name, last_name, email, message, FC_DateTime FROM fbcomplain WHERE (YEAR(FC_DateTime) = " .$sortyear .")";
$result = mysqli_query($db,$sql);
}
?>
</form>
Note that this is mostly your code, I just modified it accordingly to this artcile. The key point is I added button, which enables user to submit form.
Related
I have a database called school with a table called persons. On my PHP/HTML form I have a selector option with a drop down. I want to pass the value of the drop down into my database.
I thought the below would work, single option is only available. Can anyone see where I am going wrong? DI feel I need to store the value somewhere but Im not sure its even been captured.
index.php
<form action="insert.php" method="post">
<div>
<div>
<div>
<div><label for="Criticality">Criticality</label><select name="critical">
<option value="Critical">Critical</option>
<option value="Important">Important</option>
<option value="Support">Support</option>
<option value="Non Critical">Non Critical</option>
</select></div>
<button type="submit" value="Submit">Submit</button>
</div>
</div>
</div>
</form>
insert.php
<?php
$link = mysqli_connect("127.0.0.1", "root", "password", "school");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$critical = mysqli_real_escape_string($link, $_REQUEST['critical']);
$sql = "INSERT INTO persons (critical) VALUES ('$critical')";
if(mysqli_query($link, $sql)){
header("Location: success.html");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Technically it accepts all the input but there no any error message that there is something wrong with my code. The insert is located at the bottom part of the code which is strangely not working because I tried my insert statement in the database and of course changing the variables with real values and it worked that way.
Tried this insert statement in my workbench and it worked:
INSERT INTO members (organization,login,password,name,title,profil,created,logout_time,timezone) VALUES ('1','Kristen','123','Kristen Crooks','Volunteer','2','2016-10-11 14:09','0','0')
Code:
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><b> Volunteer Management </b></center>
<center>
Add User
Add Skills
Assign User
</center>
</head>
<body>
<?php
$config = parse_ini_file('/config.ini');
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT skillName FROM skills";
$result = $conn->query($sql);
$num=mysqli_num_rows($result);
$conn->close();
?>
<form method="get">
Username: <br>
<input type="text" name="username"> <br>
Password: <br>
<input type="text" name="password"> <br>
Name: <br>
<input type="text" name="name"> <br>
Title: <br>
<input type="text" name="title"> <br> <br>
Permission:
<select id="permission" name="permission">
<option selected disabled>Choose here</option>
<option value="0">Admin</option>
<option value="1">Project Manager</option>
<option value="2">Volunteer</option>
<option value="6">Accounting Officer</option>
</select> <br><br>
Skills: <br>
<?php
$i=0;while ($row = mysqli_fetch_assoc($result)) {
$skillName=$row['skillName'];
?>
<input type="checkbox" name="skills" value="<?php echo $skillName; ?>" ><?php echo $skillName; ?><br>
<?php $i++;} ?>
<br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
$config = parse_ini_file('/config.ini');
$conn = new mysqli($config['servername'], $config['username'], $config['password'], $config['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$login = $_GET['username'];
$password = $_GET['password'];
$name = $_GET['name'];
$title = $_GET['title'];
$profil = $_GET['permission'];
$created = date('Y-m-d H:i:s');
$sql = "INSERT INTO members (organization,login,password,name,title,profil,created,logout_time,timezone)
VALUES ('1','$login','$password','$name','$title','$profil','$created','0','0')";
$result = $conn->query($sql) or die('Error, insert query failed');
$conn->close();
}
?>
</body>
Okay I already dissected the code and it appears that the $created = date('Y-m-d H:i:s'); is causing the error. By the time I tried not to include it in the data in the insert statement, it worked! I guess I have to get another way to get the date from the system.
Also I've checked that the reason why there is an error is because the date format is in VARCHAR and not in date and another problem is that it does not fit in the length that set. Although I want to ask why it is not detected by the server that there is an error like that
Check PHP log and mysql log, write what is there.
Put an echo before the INSERT, check if your script comes to that place.
If echo is not working- then the problem is not in INSERT
I have a select dropdown list that contains all existing usernames found in my table. I want to delete an entry from my table by selecting the corresponding username. I can't seem to find the error in my php file...I get the "Successfully deleted" message even if the entry is still there. Want to know what's wrong with my php. Thanks.
Here's the concerned portion of the View:
<form action="deleting.php" method="post">
<select id="username">
<option ng-repeat="user in users">
{{user.username}}
</option>
</select>
<input type="submit" value="Delete"/>
</form>
Here's my deleting.php file
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "abc";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$deletecontent= isset( $_POST["username"] ) ? $_POST["username"] : "null" ;
$sql = "DELETE from users where username='$deletecontent'";
if (mysqli_query($conn, $sql)) {
echo "Successfully deleted";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
You can get value in $_POST only when there is name attribute in your input. so try
<select id="username" name="username">
You have to add name attribute to select option. Then only it can be accessed using $_POST.
<select id="username" name = "username">
Set name="username" in <select>
and,
set value attribute in <option>.
<form action="deleting.php" method="post">
<select id="username" name="username">
<option ng-repeat="user in users" value="{{user.username}}">
{{user.username}}
</option>
</select>
<input type="submit" value="Delete"/>
</form>
I'm an high school Italian student, and today my professor assigned me a simple homework:
I have to create a PHP page with a basic form that allows me to introduce a new "student" into a table in MySQL, "name", "surname" and "class".
That's the PHP page:
<html>
<head>
<title> School </title>
</head>
<body>
<?php
$name="";
$surname="";
$conn=mysqli_connect("127.0.0.1","root","");
if(!$conn){ echo 'No connection.'; die;}
if(!mysqli_select_db($conn,"School")) { echo 'Err'; die;}
$result=mysqli_query($conn,"select * from classes");
if(!isset($_REQUEST["submit"])){
echo '<form action="" method="post">
<input type="text" name="name" size="26"> Name <br><br>
<input type="text" name="surname" size="26"> Surname <br><br>
<input type="submit" name="submit" value="Submit">';
if(mysqli_num_rows($result)!=0){
echo '<select name="combobox" id="combobox">
<option value="" selected="selected"> Classes list </option>';
while($combobox=mysqli_fetch_assoc($result)){
$classes=$combobox["classes"];
echo '<option value="'.$combobox["classes"].'">'.$combobox["classes"].'</option>';
}
echo '</select><br><br>';
}
echo '</form>';
}
else{
$name=$_REQUEST["name"];
$surname=$_REQUEST["surname"];
//$query='insert into Students(name,surname,classes) values("'.$name.'","'.$surname.'","'.$classes.'");';
//if(!mysqli_query($conn,$query)) { echo 'Err'; die; }
echo 'Success! <br> Name: '.$name.' <br> Surname: '.$surname.'<br> Class: '.$classes.'<br>';
}
?>
</body>
</html>
I have already created the database with all the tables I need. Now, the thing is, how do I put the "option" that I choose from the <select> into a variable so I can use it for the insert into? Everything I found was just "filling a combobox from mysql".
To get value of select give it name and after get it by it's name with $_GET['name'] or $_POST['name'] if you choose method="post", you will get the selected option value.
html:
<form method="get" action="/script.php">
<select name="name"><option>1</option></select>
<input type="submit" value="send">
</form>
php:
$value=$_GET['name'];
echo $value;
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyDB (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This is what you should follow.
Always give your form elements a name, this is how it gets sent to the backend. With <select>, the value of the selected element will be sent to your code via the name you gave it. Once you do this, you can add a variable for it at the end of your code.
I'm working on a basic web form (using PHP and smarty templates) that allows users to add and delete data from a mysql database table. The data and form display fine, but nothing happens when a user tries to add or delete records from the table(not even the "cannot delete/add record' error message displays). Here is what the code looks like:
<?php //smartytest.php
$path =$_SERVER['DOCUMENT_ROOT'];
require "$path/Smarty/Smarty.class.php";
$smarty = new Smarty();
$smarty->template_dir = "$path/temp/smarty/templates";
$smarty->compile_dir= "$path/temp/smarty/templates_c";
$smarty->cache_dir="$path/temp/smarty/cache";
$smarty->config_dir="$path/temp/smarty/configs";
require_once ("$path/phptest/login.php");
$db_server=mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die('unable to connect to MySQL: '. mysql_error());
mysql_select_db($db_database) or die("unable to select database: " . mysql_error());
if(isset($_POST['author'])&&
isset($_POST['title'])&&
isset($_POST['category'])&&
isset($_POST['year'])&&
isset($_POST['isbn']))
{
$author = get_post('author');
$title = get_post('title');
$category=get_post('category');
$year = get_post('year');
$isbn =get_post('isbn');
if (isset($_POST['delete']) && $isbn !='')
{
$query= "DELETE FROM classics WHERE isbn='$isbn'";
if (!mysql_query($query))
{
echo "DELETE failed: $query<br>". mysql_error() . "<p>";
}
}
else
{
$query = "INSERT INTO classics VALUES" . "('$author','$title', '$category', '$year', '$isbn')";
if (!mysql_query($query))
{
echo "INSERT failed: $query<br>" . mysql_error() . "<p>";
}
}
}
$query = "SELECT * FROM classics";
$result = mysql_query($query);
if (!$result) die ("Database access failed: ". mysql_error());
$rows = mysql_num_rows($result);
for ($j=0; $j < $rows; ++$j)
{
$results[] = mysql_fetch_array($result);
}
mysql_close($db_server);
$smarty->assign('results', $results);
$smarty->display("smartytest.tpl");
function get_post($var)
{
return mysql_escape_string($_POST[$var]);
}
?>
Also, here is the Smarty template file:
<html><head>
<title>Smarty Test</title>
</head><body>
<form action="/phptest/smartytest.php" method="post"><pre>
Author <input type="text" name="author">
Title<input type="text" name="title">
Category<input type="text" name="category">
Year<input type="text" name="year">
ISBN<input type="text" name="isbn">
<input type="submit" value="ADD RECORD">
</pre></form>
{section name=row loop=$results}
<form action="/phptest/smartytest.php" method="post">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="isbn" value="{$results[row].isbn}">
<pre>
Author {$results[row].author}
Title {$results[row].title}
Category {$results[row].category}
Year {$results[row].year}
ISBN {$results[row].isbn}
<input type="submit" value="DELETE RECORD"></form>
</pre>
{/section}
</body></html>
My best guess is that there is something wrong with the nested if statements (which may be why not even the error message is displaying), but I've double checked the code and it looks good (at least to me). Does anyone notice anything wrong with this? I can post a screen of what the page looks like if that will help.
You are checking for the existence of author, title, category, year, isbn with isset(..) before checking to see what type of action was taken. There variables (like $_POST['author'] are not set. Reason is that you have multiple forms on the page. Only the inputs that are within the form that is submitted are available in $_POST.
In this case, you could simply do:
if (isset($_POST['delete'])
&& isset($_POST['isbn'])
&& strlen($_POST['isbn'])) {
// Remove record code here
}
else {
// Add record code here
}