Hi all,
This is my first attempt at anything to do with php/mysql so any help will be greatly appreciated.
I have been set a challenge to come up with a web app to enable staff to log on to the page, select their name in a drop down box and then set their current location and time due to leave that location. Then press submit to update their record on staff database.
So far I have gotten any names in database loaded into a drop down box and have 2 further text fields to enter in location and time details. Finding a time function is for another day!
here's my code: -
<html>
-
<?php
-
$connection = mysql_connect("localhost", "root", "");
-
if(!$connection)
-
{
-
die("database failed " . mysql_error());
-
}
-
$db_select = mysql_select_db("staff_status", $connection);
-
if(!$db_select)
-
{
-
die("database selection failed " .mysql_error());
-
}
-
echo $db_select;
-
?>
-
-
<form>
-
<select>
-
<?php
-
$sql="SELECT id,staff_name FROM status_staff";
-
$result =mysql_query($sql);
-
while ($data=mysql_fetch_assoc($result))
-
{
-
echo ("<option value=".$data['id'].">". $data['staff_name']."</option>");
-
?>
-
<?php } ?>
-
</select>
-
</form>
-
<form action="drop.php" method="post">
-
-
-
<br>
-
Location:
-
<input type="text" name="location">
-
<br>
-
Time Leaving:
-
<input type="text" name="time">
-
<br>
-
<input type="Submit">
-
-
</html>
-
So far so good but for the life of me I can't find a tutorial to show me how to handle the selection. What I want this app to do is update the record of the person selected with the inputted location and time overiding the last input they did.
Any pointers please?
20 6344
Ok, the MySQL will need to be an UPDATE query. You will also need to know how to get the selected value of the dropdown (I will explain that below). However, currently, looking at your form, there is a problem: you <form> has no action or method. Not a huge issue, but it is good practice to tell your form what you want it to do. I expect the form is posting to the same page, but I don't know whether you plan on using GET or POST (or if you even know about those?). I'll assume you will use POST. Make your <form> into this: -
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
-
Now when the form is submitted, all the values of the form will be in the POST array, which we can access by using the name attribute of the form elements as the array key, like so: -
<?php
-
-
echo $_POST['element_name'];
-
-
?>
-
Overlooking your code again, I see your <select> has no name attribute - this is necessary. Otherwise, the element's value won't be available in the POST array. So add name="user" to your <select>. Note: you will have to have a name attribute for every form element.
Check this tutorial out, for help with forms & php: PHP Tutorial - Forms @Markus
it's a POST to "drop.php", line 28. there's just one <form> too many.
btw. "action" is a required attribute
so is the code so bad that it can't be made to work?
if you apply Markus' improvements, then certainly not.
Thanks Dormilich for swift reply
Here is code with suggestions added: -
<html>
-
<?php
-
$connection = mysql_connect("localhost", "root", "");
-
if(!$connection)
-
{
-
die("database failed " . mysql_error());
-
}
-
$db_select = mysql_select_db("staff_status", $connection);
-
if(!$db_select)
-
{
-
die("database selection failed " .mysql_error());
-
}
-
echo $db_select;
-
?>
-
-
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
-
<select name="user">
-
<?php
-
$sql="SELECT id,staff_name FROM status_staff";
-
$result =mysql_query($sql);
-
while ($data=mysql_fetch_assoc($result))
-
{
-
echo ("<option value=".$data['id'].">". $data['staff_name']."</option>");
-
?>
-
<?php } ?>
-
<?php
-
-
echo $_POST['staff_name'];
-
-
?>
-
</select>
-
</form>
-
<form action="drop.php" method="post">
-
-
-
<br>
-
Location:
-
<input type="text" name="location">
-
<br>
-
Time Leaving:
-
<input type="text" name="time">
-
<br>
-
<input type="Submit">
-
-
</html>
-
-
I know I'm out of my depth here but if I was to get it working I could understand/learn from reading the working code.
Any further instructions please?
there's still work to do...
minor details:
- line 23, you should html-quote the attribute values - echo '... value="' . $var . '" ...';
-
// or
-
echo "... value=\"" . $var . "\" ...";
-
// giving
-
... value="content_of_$var" ...
- line 24–26, replace by } (the <?php and ?> remove themselves)
major details:
- the values from the first form are never sent (there's no submit button for that form). you should make only one <form> which will process the results. I guess it's the second one. remember, only the values from inside the <form> are submitted (you may have multiple form elements on a side, but only the one whose submit is pressed actually sends data).
thanks again but just don't understand what you are saying to do.
I have never asked anyone on the site to show me with my variables, but I just don't think ill get it any other way.
If you can't do that for me, thanks again, I'll just stick to java!
in the end, the html code should look like this (more or less): - <html>
-
-
<form action="drop.php" method="post">
-
<select name="user">
-
<option value="ID1">NAME1</option>
-
<option value="ID2">NAME2</option>
-
<option value="ID3">NAME3</option>
-
// ...
-
</select>
-
<br>
-
Location:
-
<input type="text" name="location">
-
<br>
-
Time Leaving:
-
<input type="text" name="time">
-
<br>
-
<input type="Submit" value="send">
-
</form>
-
-
</html>
in the final version, drop lines 13 and 28 (this is nothing for the eyes of the user, only for debugging)
the file "drop.php" has to handle the database update.
EDIT: the problem of your code does not come from the PHP side, it's the HTML code that's not right.
Dormilich, thank you! I now know what the meaning of the word post means!
So after a bit of copying and pasting (and learning) I have 2 files which look like they should do what im trying.
here's insert1.php -
<html>
-
<?php
-
$connection = mysql_connect("localhost", "root", "");
-
if(!$connection)
-
{
-
die("database failed " . mysql_error());
-
}
-
$db_select = mysql_select_db("staff_status", $connection);
-
if(!$db_select)
-
{
-
die("database selection failed " .mysql_error());
-
}
-
echo $db_select;
-
?>
-
-
<form action="insert2.php" method="post">
-
<select name="user">
-
<?php
-
$sql="SELECT id,staff_name FROM status_staff";
-
$result =mysql_query($sql);
-
while ($data=mysql_fetch_assoc($result))
-
{
-
echo ("<option value=".$data['id'].">". $data['staff_name']."</option>");
-
?>
-
</select>
-
<br>
-
Location: <input type="text" name="location">
-
Time Leaving: <input type="text" name="time">
-
-
<input type="Submit">
-
-
-
-
</form>
-
</html>
-
I have tried to stay with importing the names from database to show in dropdown box as this was working and has been my only success today:(
Here's insert2.php -
-
<?php
-
-
$connection = mysql_connect("localhost", "root", "");
-
if(!$connection)
-
{
-
die("database failed " . mysql_error());
-
}
-
$db_select = mysql_select_db("staff_status", $connection);
-
if(!$db_select)
-
{
-
die("database selection failed " .mysql_error());
-
}
-
echo $db_select;
-
-
$name = $_POST['staff_name'];
-
$location = $_POST['location'];
-
$time = $_POST['time'];
-
$query2 = "INSERT INTO status_staff VALUES ('$name','$location','$time')";
-
mysql_query($query2);
-
mysql_close();
-
?>
-
Am i getting closer to the concept of html/php/mysql?
Edit:
@brendanmcdonagh
yes @brendanmcdonagh
this could be improved to - echo "<option value=\"", $data['id'], "\">", $data['staff_name'], "</option>";
(printing " to the html too and executing a bit faster) @brendanmcdonagh
which is line 30, the display is currently a bit strange here....
EDIT: fixed now, I'll have a second look
sorry, ignore line 30 error, i was looking at wrong page, doh!
I can't thank you enough, I have it doing what it should!!!!!!!!!!
I go out to walk the dog, and I come back to another happy customer.
Brendan, just a note, you should really check out these sites (they helped me from n00b to being able to basic-intermediate stuff). Introduction to PHP PHP Tutorial - Introduction
thanks for them sites, i 'll get some more education I just sometimes go into stuff feet first!
I have one more request for help on this thread... -
<form action="insert2.php" method="post">
-
<select name="user">
-
<?php
-
$sql="SELECT id,staff_name FROM status_staff";
-
$result =mysql_query($sql);
-
while ($data=mysql_fetch_assoc($result))
-
{
-
echo "<option value=\"", $data['id'], "\">", $data['staff_name'], "</option>";
-
}
-
?>
-
<br>
-
Location: <input type="text" name="location">
-
<br>
-
Time Leaving: <input type="text" name="time">
-
-
<input type="Submit">
-
my understanding is the above form is sending 3 variables to.... -
$name = $_POST['user'];
-
$location = $_POST['location'];
-
$time = $_POST['time'];
-
$query2 = "update status_staff set staff_location = $location, set time_leaving = $time where staff_name = $name";
-
-
mysql_query($query2);
-
But it's not adding the input. I know it's talking to database because before i tried update i had insert working but don't want duplicates.
Anyone got any time left to help me finish this last thing>?
When it comes to writing queries, the best way to do it (for readability's sake) we capitalise any reserved words (UPDATE, SET, INSERT, etc), use back-ticks (`) on table names, column names, etc.
Like this: -
SELECT
-
`Column_Name`
-
FROM
-
`Table_Name`
-
WHERE
-
`Column_4` = '{$var}'
-
Off-topic, but I think it deserves a mention.
@Markus: backtick is unicode number 96 (U+0060) ?
done some troubleshooting and i can see what is causing the problem but can't see the solution. - while ($data=mysql_fetch_assoc($result))
-
{
-
echo "<option value=\"", $data['id'], "\">", $data['staff_name'], "</option>";
I ve done an echo of the $name variable once the following has been assigned - $name = $_POST['staff_name'];
and it's showing as empty but the other variables posted are being sent across??
done it guys, thanks for all your help.
Brendan
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jonnytansey2 |
last post by:
Can anyone out there give me a pointer regarding creating a
dynamically-generated drop-down list connected to an array?
And is that question as clear as chocolate spread?
Here's what I've got....
|
by: brino |
last post by:
hi all !
i'm kind of new to MYSQL so am still learning basics.
my question is how do you create a drop down list in a field in an SQL
database ?
thanks
brino
|
by: ashok893 |
last post by:
I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list...
|
by: tusaar |
last post by:
Hi all
I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from...
|
by: ponyeyes |
last post by:
Hi There,
I am a bit of a newbie to PHP programming and I would like to know how I can place a selected drop down option into a PHP variable and then produce an sql query which incorporates this...
|
by: Boujii |
last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
|
by: mramsay |
last post by:
HI,
I'm having alot of problems trying to figure out how to create drop down links from a hyperlink on my homepage.
I have a hyperlink called Programs (this is for a community centre)
When...
|
by: darkenroyce |
last post by:
Hi Guys
I am creating a sports database and one of the features involves create drop down dialogue boxes that retrieve data from MySQL tables and provides them as <option> within...
|
by: student2008 |
last post by:
Sorry about the title its a tricky one.
I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |