?
Solved

php and mysqli help

Posted on 2014-02-04
14
Medium Priority
?
575 Views
Last Modified: 2014-02-05
Ok i have been work on a php and mysqli project.

this si what the page looks like know
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 ?
0
Comment
Question by:armasmike
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39832602
You might want to start by deconstructing the problem into component parts that are small enough to write and test individually.  Then as each part is proven to work correctly you can assemble the application from the pre-tested pieces.  I can show you some parts of the design patterns.

How to use MySQLi, mapped against the familiar but deprecated MySQL extension.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

The essential moving parts of a basic jQuery AJAX call to a server-side script.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

An article showing the standard design pattern for a table maintenance script.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12335-PHP-and-MySQLi-Table-Maintenance.html
0
 

Author Comment

by:armasmike
ID: 39832705
ok the PHP and MySQLi Table Maintenance is a great read will work thought that.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39832713
Post back here with any question.  Especially if there is anything unclear in the article.

Cheers, ~Ray
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39832856
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

0
 

Author Comment

by:armasmike
ID: 39832959
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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39833001
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.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39833007
@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 :)
0
 

Author Closing Comment

by:armasmike
ID: 39833038
always helpful
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39833066
Thanks for using EE, and best of luck with your project, ~Ray
0
 

Author Comment

by:armasmike
ID: 39833116
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 ?
0
 

Author Comment

by:armasmike
ID: 39835999
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
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39836072
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

0
 

Author Comment

by:armasmike
ID: 39836084
damn it how did i miss that thank you
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39836096
:)
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question