retrieve mysql data - php

I have this code for listing mysql records and putting it into a table according to the address inputted. My problem is, how to do just the same but this time, making use of textboxes to project the contents of the record corresponding to the same data inputted.
I'm just a beginner with no talent. Maybe you could give me some idea on how to do it.
mysql_select_db("hospital", $con);
$result = mysql_query("SELECT * FROM t2 WHERE ADDRESS='{$_POST["address"]}'");
echo "<table border='1'>
<tr>
<th>HospNum</th>
<th>RoomNum</th>
<th>LastName</th>
<th>FirstName</th>
<th>MidName</th>
<th>Address</th>
<th>TelNum</th>
<th>Nurse</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['HOSPNUM'] . "</td>";
echo "<td>" . $row['ROOMNUM'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "<td>" . $row['FIRSTNAME'] . "</td>";
echo "<td>" . $row['MIDNAME'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "<td>" . $row['TELNUM'] . "</td>";
echo "<td>" . $row['NURSE'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

As a suggestion, I do not think displaying the values inside of a textbox is the best idea. With that being said, you achieve your results by performing the following
while($row = mysql_fetch_array($result)) {
echo "<input type=\"text\" value='" . $row['HOSPNUM'] . "'><br />";
echo "<input type=\"text\" value='" . $row['ROOMNUM'] . "'><br />";
....
}
You would need to escape the " inside of the text boxes by using PHP's escape special character \

You need to search from your present tables and use AJAX to notify the user.
I would suggest you look into a framework in PHP which would help you a LOT since you are just starting your projects. List of frameworks can be found at http://www.phpframeworks.com/

As far as i could understand your question, you want to retrieve data from mysql based on the input of the text boxes. This is how you can go about it:
<form action="yourpage.php">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="text" name="text3">
</form>
Now you can retrieve data from mysql using where clause.
select * from table where text1 = 'text1' and text2 = 'text2' and text3 = 'text3'
Note: You need to change the names in form and query as per your requirement.

Related

insert input time content in mysql type time

this is my table in html php, i get the names of employees from DB
i need to insert in the database the content of input type=time
this is my php code
<?php
$nom='';
$prénom='';
$output='';
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
$sql="SELECT * FROM employés;";
$result = mysqli_query($connect, $sql);
echo "<table>
<tr>
<th>nom</th>
<th>prénom</th>
<th>Entrée matin</th>
<th>Sortie matin</th>
<th>Entrée soir</th>
<th>Sortie soir</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "<td>" . "<input type='time' " . "</td>";
echo "</tr>";
}
$output="</table>";
echo $output;
?>
i have no idea how to insert the content of input type='time' in mysql DB
neither with type varchar or time.
can someone help me please?
Depending on the type of your field in your database. Assuming it's a DATETIME, you could format it with a date().
echo "<td>" . "<input type='time' value='" . date("H:i", strtotime($row['entree_matin'])) . "' /></td>";
insert your time inside the 'value' parameter in the time tag
exemple :''
<input id="time" type="time" value="12:13">
but first you have to insert the right format : hh:mm

Assigning ID's to fields that are echoed using PHP inside a while loop

I currently have two dropdown menus that a user will select the course and a golfer which will load a scorecard.
What I am trying to do now is when a user enters a value into the "Score" field I want the "points" to be automatically shown ("Points" is a read only input field). To do this I am assuming that I will have to give each table row for score and points a specific ID.
echo "<div class='scorecardTable'>
<table 'id=scorecardTable'>
<tr>
<th>HoleNumber</th>
<th>Par</th>
<th>Stroke Index</th>
<th>Score</th>
<th>Points</th>
</tr>'";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['holeNumber'] . "</td>";
echo "<td>" . $row['par'] . "</td>";
echo "<td>" . $row['strokeIndex'] . "</td>";
echo "<td> <input type='text' maxlength='2' id='score' /></td>";
echo "<td> <input type='text' id='points' readonly></td>";
echo "</tr>";
}
echo "</table>";
From the above code I am using PHP to print the information from my database but I was just wondering if anyone would know how to assign a unique ID to each of the score and points input fields so I can apply the calculation to each user input.
You could use a variable to count the rows and set the id.
$rowIndex = 0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['holeNumber'] . "</td>";
echo "<td>" . $row['par'] . "</td>";
echo "<td>" . $row['strokeIndex'] . "</td>";
echo "<td> <input type='text' maxlength='2' id='score$rowIndex' /></td>";
echo "<td> <input type='text' id='points$rowIndex' readonly></td>";
echo "</tr>";
$rowIndex++;
}
You may also use
id='score[$rowIndex]'

How to get the ID of all the checked boxes on displayed data base QUICKFIX?

I have displayed a table of my data from the data base with check boxes to the left. I want to find a way to link the check boxes to the question number (ID). when I hit submit I want the selected id's to be echoed. pretty much I want someone to be able to select the questions they want and then display them.
<?php
$con=mysqli_connect("####","####","#####","###");
$result = mysqli_query($con,"SELECT * FROM q_and_a ");
echo "<table border='1'>
<tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" name="questions[]" value="$id"></td>';
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
submit button
<form method="POST" action="makeTest.php">
<input type="submit" name="make" value="Make Test">
</form>
Make some edits to your code then it will work.
1. Change your query by adding fields not * (to ensure performance and display order)
$result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");
then before while block open form tag(HTML)
<?php
//above codes will be there as you show before
echo '<form method="POST" action="makeTest.php">';
while($row = mysqli_fetch_array($result)){
{ $id=$row['id']; // initialize your id here, so as to pass it in checkbox too
// then continue with your code
}
?>
<input type="submit" name="make" value="Make Test">
</form>
in maketest.php yo can hande ckeckbox using foreach, see below
foreach($_POST['questions'] as $questions){
//do your job
}

Delete button for each table row

I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

List records in mysql using php

This is the last thing I'm gonna do, after I can make this work, I can produce to designing which might be a lot easier. Thanks for all your help.
I want to list a record from mysql using php by typing a query in the text box. Then the program will list all the records that has that element, for example address.
Here is my code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("koro", $con);
\\what will I put in the WHERE ADDRESS="?
$result = mysql_query("SELECT * FROM students WHERE ADDRESS=");
echo "<table border='1'>
<tr><th>Firstname</th>
<th>Lastname</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['IDNUMBER'] . "</td>";
echo "<td>" . $row['LNAME'] . "</td>";
echo "<td>" . $row['FNAME'] . "</td>";
echo "<td>" . $row['MNAME'] . "</td>";
echo "<td>" . $row['GRADEYR'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The listform.html:
<html>
<head>
<form action="list.php" method="post">
address:<input type="text" name="grup" value="" /><br/>
<input type="submit" name="List" value="" /><br/>
</form>
</head>
</html>
\what will I put in the WHERE ADDRESS="?thanks
You will find the value in the $_POST[] array.
$result = mysql_query("SELECT * FROM students WHERE ADDRESS = '{$_POST["grup"]}'");
This approach can be dangerous though - you'll want to also sanitize your $_POST[] data against SQL injection attacks - of which there are plenty of tutorials available.
$_POST['grup'] will contain the value in the field when posted.
Then do something like:
$result = mysql_query(sprintf("SELECT * FROM students WHERE ADDRESS='%s'", mysql_real_escape_string($_POST['grup'], $con)));
If you dont escape the POST value with sprintf and mysql_real_escape_string you can be targeted by SQL injection attacks.
http://en.wikipedia.org/wiki/SQL_injection

Categories