armasmike
asked on
php and mysqli help
Ok i have been work on a php and mysqli project.
here is my code
at this time all it does is list all info from the db. as you can see i have a edit button at the end of each row. Each button has there own
was going to use the button id as a way to tell the php witch db record to update.
what i would like is when the user press the edit button. It will open a overlay box with the info from that row is input box where the user can make change and click save. The save will update the db with the new info and the main php will refresh.
I have some hidden input box's in the code i was thinking of making then not hidden and then make the edit button change to a save button. So everything stays on one page.
The
How can i make this a reality ?
here is my code
<html><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
</head>
<BODY>
<?php
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$DB_HOST = "localhost"; // PROBABLY THIS IS OK
$DB_USER = "root";
$DB_PASS = "admin";
$DB_NAME = "patient_regester";
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
// check connection
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `kfhh` ORDER BY 'Room_id' ";
$result = $conn->query($query) or die($conn->error.__LINE__);
echo "<form action='get.php' method='post'>";
echo "<br><table width='50%' border='1'>";
echo "<tr align='center'>";
echo "<td>Room Number</td>";
echo "<td>Occupancy Status</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>LOC</td>";
echo "<td>Visitors</td>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
$lname= stripslashes($row['Last_name']);
$fname= stripslashes($row['First_Name']);
$LOC= stripslashes($row['LOC']);
$Visitors= stripslashes($row['Visitors']);
$Occupancy= stripslashes($row['Occupancy']);
$Room_id= stripslashes($row['Room_id']);
echo "<tr>";
echo "<td id='RM_".$Room_id."'>" .$Room_id ."</td>";
echo "<td >" .$Occupancy ."</td>";
echo "<input name='ln_".$Room_id."_".$Occupancy."' type='hidden' value='" .$Occupancy. "' />";
if($lname == NULL)
{
echo "<td> No Name </td>";
} else
{
echo "<td >" .$lname ."</td>";
echo "<input name='ln_".$Room_id."_".$lname."' type='hidden' value='" .$lname. "' />";
}
if($fname == NULL)
{
echo "<td> No Name </td>";
} else
{
echo "<td >" .$fname ."</td>";
echo "<input name='ln_".$Room_id."_".$fname."' type='hidden' value='" .$fname. "' />";
}
if($LOC == NULL)
{
echo "<td> No LOS </td>";
} else
{
echo "<td >" .$LOC ."</td>";
echo "<input name='ln_".$Room_id."_".$LOC."' type='hidden' value='" .$LOC. "' />";
}
if($Visitors == NULL)
{
echo "<td> No Visit </td>";
} else
{
echo "<td >" .$Visitors ."</td>";
echo "<input name='ln_".$Room_id."_".$Visitors."' type='hidden' value='" .$Visitors. "' />";
}
echo "<td > <input type='button' id='bt_".$Room_id."' type='submit' value='Edit'></td>";
}
?>
</tr>
</form>
</table>
<script type="text/javascript">
$("input").click(function() {
alert(this.id);
});
</script>
<?php
mysqli_close($conn);
?>
</BODY>
</html>
at this time all it does is list all info from the db. as you can see i have a edit button at the end of each row. Each button has there own
id='bt_".$Room_id."'
then i have a jquery at the bottom that alert me of witch id the user is pressing. was going to use the button id as a way to tell the php witch db record to update.
what i would like is when the user press the edit button. It will open a overlay box with the info from that row is input box where the user can make change and click save. The save will update the db with the new info and the main php will refresh.
I have some hidden input box's in the code i was thinking of making then not hidden and then make the edit button change to a save button. So everything stays on one page.
The
action='get.php' method='post'
does nothing.How can i make this a reality ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Post back here with any question. Especially if there is anything unclear in the article.
Cheers, ~Ray
Cheers, ~Ray
Once you've got your head around the database side of things, the easiest option is to store the primary key from the database as a data attribute on the button. It will make the jQuery side of things much easier. Assuming your primary is $Room_id, then you would create each button like this:
<button class="edit" data-roomID="<?php echo $Room_id ?>">Edit</button>
The jQuery to get it would then simply be $('.edit').click(function() {
alert("Room ID is " + $(this).data('roomID') );
});
ASKER
Why do you write it this way
I fell it's harder to read why not this
// CREATING A QUERY TO LIST THE TEST DATA
$sql
=
"
SELECT
id
, fname
, lname
FROM
EE_maintenance
ORDER BY
id
"
;
I fell it's harder to read why not this
$sql =" SELECT id, fname, lname FROM EE_maintenance ORDER BY id";
I have a large code base to work with and I often find that I need to make changes in the queries. When I write queries and array notation like that, it's easier for me to make the changes without introducing syntax and parse errors. I also have automation that does things like look for a line starting with = followed by a line starting with " so it's easy for me to find my queries because I follow that coding standard.
The statements are functionally equivalent -- PHP and SQL do not care about your white space. I just try to do what I have found makes my code easier to read and debug. And for me this is a tried-and-true technique. The advantage really comes out when you get into long and complicated queries. On a short query string like this, it probably doesn't matter.
The statements are functionally equivalent -- PHP and SQL do not care about your white space. I just try to do what I have found makes my code easier to read and debug. And for me this is a tried-and-true technique. The advantage really comes out when you get into long and complicated queries. On a short query string like this, it probably doesn't matter.
@armasmike - it's personal preference how you write it - sometimes it's easier on one line, sometimes it's easier over several lines.
A short query like that is probably best over 1 line
A longer query might be easier to read (and debug) written over several:
SELECT col1, col2, col3, col4
FROM yourTable
WHERE col1 = 'some value'
ORDER BY 'col3' DESC
Write it however you prefer :)
A short query like that is probably best over 1 line
A longer query might be easier to read (and debug) written over several:
SELECT col1, col2, col3, col4
FROM yourTable
WHERE col1 = 'some value'
ORDER BY 'col3' DESC
Write it however you prefer :)
ASKER
always helpful
Thanks for using EE, and best of luck with your project, ~Ray
ASKER
can you explain this more for me the
$id LIMIT 1 on line 34 of the RAY_EE_table_maintenance_d elete.php
I don't under stand how this works ?
$id LIMIT 1 on line 34 of the RAY_EE_table_maintenance_d
I don't under stand how this works ?
ASKER
ok i give up How do you add space to something like this
It only see the You part i don't want to put _ or . as spaces
$out .= '<optgroup label=You can pick one of these>';
It only see the You part i don't want to put _ or . as spaces
Not sure what this has to do with the original question, but the answer is 'you wrap it in quotes':
$out .= '<optgroup label="You can pick one of these">';
ASKER
damn it how did i miss that thank you
:)
ASKER