Solved

php and mysqli help

Posted on 2014-02-04
14
547 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
  • 6
  • 4
  • 4
14 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 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 108

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
 
LVL 42

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 108

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 42

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:armasmike
ID: 39833038
always helpful
0
 
LVL 108

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 42

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 42

Expert Comment

by:Chris Stanyon
ID: 39836096
:)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now