I have this code to load the month of the birthday that corresponds to the id number:
<?php
$query = "SELECT DISTINCT BIRTHDAY FROM student WHERE IDNO='".$_GET['id']."'";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select title="- Select Month -" name="mm" id="mm" class="" >
<?php
while ($row = mysql_fetch_array($result))
list($year,$month,$day)=explode("-", $row['BIRTHDAY']);
?>
<option value="<?php echo $month;?>"><?php echo $month; ?></option>\n";
And this is the form action:
$birthday = mysql_real_escape_string($_POST['mm']);
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
What should I do, when I click on the update button, it executes, but I see the undefined offset error is stored in the mysql database and not the month.
I'm just a beginner, can you give me some tips on how can I achieve updating the data
In cases like this... you will have to use 3 selects and then join them to update the database... so, in the form you have something like this:
<select name='month'>
<option value='1'>January</option>
<option value='xx'>etc</option>
</select>
<select name='day'>
<option value='1'>1</option>
<option value='xx'>etc</option>
</select>
<select name='year'>
<option value='1980'>1980</option>
<option value='xx'>etc</option>
</select>
Then... the PHP that receives that data should do something like:
$year = mysql_real_escape_string($_REQUEST['year']);
$month = mysql_real_escape_string($_REQUEST['month']);
$day = mysql_real_escape_string($_REQUEST['day']);
$birthday = $year.'-'.$month.'-'.$day;
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
Of course... you have to verify first whether all variables are set or not. You can do so by using the isset method.
Check your update query, It may be wrong in that.
mysql_query("UPDATE student SET YEAR = FIRSTNAME='$fname', BIRTHDAY='$birthday'
WHERE IDNO ='$idnum'");
See this year and firstname, in this year is assigned to null character for you.
Just assign like this,
$birthday = mysql_real_escape_string($_POST['mm']);
Related
I have a comboBox that has options: transpo, meal and medical. I want to insert the values into the column that corresponds to option that the user chooses.
Is it possible?
This is my latest query.
$conn->query("INSERT INTO expenses
SET employee_id = :emp_id,
client_id = :client_id,
CASE
WHEN :cat == 'transpo' THEN transpo_exp = :worth
WHEN :cat == 'meal' THEN meal_exp = :worth
WHEN :cat == 'medical' THEN medical_exp = :worth");
please use array [] symbol in select name.
<select name="expenses[]" multiple="multiple">
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
and get array variable.
$expenses= $_POST['expenses'];
for($i = 0; $i < count($expenses); $i++){
echo $expenses[$i];
$Qry = mysql_query("INSERT INTO expenses VALUES('$expenses[$i]]')");
}
In php:
$query ="SELECT rent FROM mydatabase WHERE rent= '$rent'";
$rs=mysqli_query($connect,$query);
if(!$rs)
{echo "<script type ='text/javascript'>alert('Cannot connect to database')</script>";
}
else
{ if(mysqli_num_rows($rs) != 0)
{die("<script type ='text/javascript'>alert('The selected room are not available')</script>");}
}
$sql = "INSERT INTO mydatabase(title,first_name,last_name,dob,email,id,password,rent) VALUES('$title','$first_name','$last_name','$dob','$email','$id','$password','$rent')";
mysqli_query($connect,$sql);
$_SESSION['first_name'] = $first_name;
$_SESSION['rent'] = $rent;
header("location: database.php");
In html:
<select name = "rent">
<option></option>
<option name= "rent" value="A1">A1</option>>
<option name= "rent" value="A10">A10</option>
<option name= "rent" value="A3">A3</option>
<option name= "rent" value="A4">A4</option>
<option name= "rent" value="A5">A5</option>
<option name= "rent" value="A15">A15</option>
<option name= "rent" value="A20">A20</option>
<option name= "rent" value="A8">A8</option>
<option name= "rent" value="A6">A6</option>
<option name= "rent" value="A12">A12</option>
</select>
Here,I want to set unique for rent value. For example, if customer had selected rent value 'A1' again he cannot select the same value. The problem is I also want to set the empty option for customer which does not want to select any option and store their data in database. But the empty option () cannot be entered two time. So what the thing can I give to overcome this. Ps. I am totally amateur and my English is not so good. Thank You :)
I think the best solution for your problem is you need to create your selection dynamically from your database, I mean get what rent didn't take or has been taken by users from your database.
i.e after user loaded the page that selection fills data from database not put it like static. for this, you can create one more table, or just simply add one more field to determines the rent, I called "IsTaken" it returns 1 for Yes, or 0 for not taken.
after user took then update "IsTaken" from 0 to 1 it means it's taken by the user, or after removed you can update from 1 to 0.
for each new record that you or your users insert, add a query to update that field. also for removing each record.
for the update:
update table t1 set rname = 1 where 'your condition to update only this record'
for the delete:
delete from t1 where 'your condition to delete only this record'
please see my solution and let me know if your problem is solved or not.
the following code is just shown selection query.
See this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackoverflow";
$conn = new mysqli($servername, $username, $password, $dbname);
//SELECT those record that not taken, be default i set that field to 0
$sql = "SELECT rname FROM t1 where istaken = 0";
$result = mysqli_query($conn,$sql);
?>
<form action= "" method= "post">
<select name = "rent">
<?php
if ($result->num_rows > 0) {
//fill options with data returns from our query..
while($row = $result->fetch_assoc()) {
echo "<option name=".$row['rname'].">" . $row['rname'] . "</option>";
}
} else {
//if there is no record...
echo "<option name='None'>None</option>";
}
?>
</select>
</form>
<?php
$conn->close();
?>
.
.
.
I am sorry if I have any grammar or spelling error, hope you understand what I am did here.
I want to get the value of the option using PHP, and depending on the chosen option i want the page to display different results depending on what they choose.
I do not have a submit button as i want the data to change when the user clicks on an option from the drop down list.
Here is my form:
<form action="#" name="viewall" id="viewall" method="post">
<label for="date" id="date">Select a decade:</label>
<select name="date">
<option value="all" id="all">View All</option>
<option value="1920" id="1920">1920's</option>
<option value="1930" id="1930">1930's</option>
<option value="1940" id="1940">1940's</option>
<option value="1950" id="1950">1950's</option>
<option value="1960" id="1960">1960's</option>
<option value="1970" id="1970">1970's</option>
<option value="1980" id="1980">1980's</option>
<option value="1990" id="1990">1990's</option>
</select>
And i would like if the user picks 1920's that it only shows records in that decade, if the user doesn't pick anything it will automatically show all records.
I have been trying to use the following code and kind of got it working using submit button, but it would only show either 1920's or 1970's and wouldn't change.
if(isset($_POST['submit'])){
//sel_val stores selected value in a variable
//mysqli_real_escape_string() function escapes special characters in a string for use in an SQL statement.
$sel_val = mysqli_real_escape_string($con, $_POST['1920']);
$sql = mysqli_query($con, "SELECT * FROM details
WHERE year < '1930' AND year > '1919' ORDER BY year ASC;");
}
elseif(isset($_POST['submit'])){
//sel_val stores selected value in a variable
//mysqli_real_escape_string() function escapes special characters in a string for use in an SQL statement.
$sel_val = mysqli_real_escape_string($con, $_POST['1970']);
$sql = mysqli_query($con, "SELECT * FROM details
WHERE year < '1980' AND year > '1969' ORDER BY year ASC;");
}
else
{
$sel_val = mysqli_real_escape_string($con, $_POST['all']);
$sql = mysqli_query($con, "SELECT * FROM details ORDER BY year ASC;") or die(mysqli_error($con));
}
Any suggestions with or without a submit button would help.
if (isset($_POST['submit'])) {
if (is_numeric($_POST['date']) {
$sel_val = mysqli_real_escape_string($con, $_POST['date']);
$sql = mysqli_query($con, "SELECT * FROM details WHERE year = " . $sel_val);
} elseif ($_POST['date'] === 'all') {
$sql = mysqli_query($con, "SELECT * FROM details ORDER BY year ASC");
}
} else {
$sql = mysqli_query($con, "SELECT * FROM details ORDER BY year ASC;") or die(mysqli_error($con));
}
The reason you only get 20's or 70's is because that is all that is coded in the case statement. If those actually worked, you just need to code a few more conditions. Change the date values by 10 years for each until you get all the decades.
You will need AJAX to do this without a submit. To learn more about AJAX please see jQuery's AJAX or if you prefer Javascript and Mozilla Documentation You can also search on the numerous posts on StackOverflow to learn what other users have gone through!
So here's my php code:
$sql1 = "SELECT year FROM reports ORDER BY year DESC";
$res1 = mysql_query($sql1);
while($row = mysql_fetch_array($res1)) {
$selectYear = "
<select id='select_year'>
<option value='".$row['year']."'>".$row['year']."</option>
</select>
";
}
What I want it to do is just output one year instead of what its doing now which is outputting every single year and there is one entry for each month of the year so there are 12 entries with the same year so my html is this:
<select id='select_year'>
<option value='2013'>2013</option>
<option value='2013'>2013</option>
<option value='2013'>2013</option>
<option value='2013'>2013</option>
<option value='2012'>2012</option>
</select>
But this is what I actually want:
<select id='select_year'>
<option value='2013'>2013</option>
<option value='2012'>2012</option>
</select>
This is to sort through all the results by year so that the user can just use the select to select which year they want to view.
There are lots of ways of doing this, but the easiest is probably:
$sql1 = "SELECT DISTINCT year FROM reports ORDER BY year DESC";
With DISTINCT, the database will only return one of each value. Then you don't have to waste any resources processing data on the PHP side.
#Thomas Kelley has given the perfect answer above.
However, if you insist on PHP solution. You can do it as follows.
$res1 = mysql_query($sql1);
$placeHolder = ",";
while($row = mysql_fetch_array($res1)) {
if ( strpos($placeHolder , $row['year']) )
continue;
$placeHolder .= $row['year'] .',';
$selectYear = "
<select id='select_year'>
<option value='".$row['year']."'>".$row['year']."</option>
</select>
";
}
I have a working mysql query that retrieves data from table1.
Now I will add every month a new table (table2, table3..etc).
Goal 1 : I would like to add a drop down menu and populate it with all tables as user options.
Goal 2 : I would like to make the query connect to the table that the user selects from the drop down menu, retrieves data and then refresh the page or just the table's div to display updated data.
my current mysql/php code :
$query = "SELECT X, Y, Z FROM **table1**";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['X'];
echo $row['Y'];
echo $row['Z'];
}
Instead of "table1" it should be a variable linked to the drop down menu that the user selects.
I feel it should be simple, but I am bit new to this and could not find a similar case.
Thanks a lot gents.
I like the comment above but here is an example not sure if that what you are looking for
<form action="process.php" method='post'>
<select name="tables">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" />
</form>
process.php file
$table=$_POST['tables'];
$query = "SELECT X, Y, Z FROM ".$table;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['X'];
echo $row['Y'];
echo $row['Z'];
}
$result = 'SHOW TABLES [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr];';
while($row = mysql_fetch_array($result))
{
echo $row['Tables_from_db_name'];
}