Link to home
Start Free TrialLog in
Avatar of armasmike
armasmike

asked on

php and mysqli help

Ok i have been work on a php and mysqli project.

User generated image
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>

Open in new window



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."'

Open in new window

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'

Open in new window

does nothing.

How can i make this a reality ?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of armasmike
armasmike

ASKER

ok the PHP and MySQLi Table Maintenance is a great read will work thought that.
Post back here with any question.  Especially if there is anything unclear in the article.

Cheers, ~Ray
Avatar of Chris Stanyon
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>

Open in new window

The jQuery to get it would then simply be

$('.edit').click(function() {
    alert("Room ID is " + $(this).data('roomID') );
});

Open in new window

Why do you write it this way

// CREATING A QUERY TO LIST THE TEST DATA
$sql
=
"
SELECT
  id
, fname
, lname
FROM
  EE_maintenance
ORDER BY
  id
"
;

Open in new window


I fell it's harder to read why not this

$sql =" SELECT id, fname, lname FROM EE_maintenance ORDER BY id";

Open in new window

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.
@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 :)
always helpful
Thanks for using EE, and best of luck with your project, ~Ray
can you explain this more for me the

$id LIMIT 1 on line 34 of the RAY_EE_table_maintenance_delete.php

I don't under stand how this works ?
ok i give up How do you add space to something like this

$out .= '<optgroup label=You can pick one of these>';

Open in new window


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">';

Open in new window

damn it how did i miss that thank you