PDO Structure

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 1396 Views - Last Post: 08 April 2016 - 09:30 AM Rate Topic: -----

#1 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

PDO Structure

Posted 06 April 2016 - 07:44 PM

After a while of putting it off, I am finally learning PDO. I have a question regarding the structure I am using for it; basically if I am doing my format correctly. I then have another question regarding bringing up single records.

So this is the way I created my PDO file. It works the way it is. Please let me know if you add anything else to your base PDO files and/or do it a different way.

ini_set('display_errors', 1);
error_reporting(E_ALL);
try {
	$host = 'localhost';
	$name = '';
	$user = '';
	$password = '';

	$dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
	echo $e->getMessage();
	
}





Now, my next question is regarding the ability to update a certain record by choosing a record from a table and going to that selected record.

Let's say I have a table called "Products" that looks like this:

ID Name Amount

1 Shoes $10 Edit

2 Hats $5 Edit


If I click the "Edit" Button I want to go to a different page which allows me to edit all of the information for that record selected.


		<form method="POST">
			<input name="first" placeholder="First Name">
			<input name="last" placeholder="Last Name">
			<input name="product" placeholder="Product">
			<button name="add" type="submit">Add</button>
		</form>
	</div>
	<hr>
	<table>
		<thead>
			<tr>
				<th>ID</th>
				<th>Name</th>
				<th>Amount</th>
				<th></th>
			</tr>
		</thead>
		<tbody>
		
		<?php
		$stmt = $dbc->query("SELECT * FROM users");
		$stmt->setFetchMode(PDO::FETCH_ASSOC);
		
		while($row = $stmt->fetch()) {
		?>
		<form method="POST">
			<tr>
				<td><?php echo $row['id'];?></td>
				<td><?php echo $row['name'];?></td>
				<td><?php echo $row['amount'];?></td>
				<td><button name="edit" type="submit">Edit</button></td>
			</tr>
		</form>	
		<?php } ?>
		</tbody>


Here is some code I created for this example. I am unsure of what to do from this point. I know I would have to create a file with all of the input fields I need, but my question is how do I get there and send over the data info that I selected, then how do I echo out that information and allow the record to be updated?

Is This A Good Question/Topic? 0
  • +

Replies To: PDO Structure

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 12:10 AM

Quote

Please let me know if you add anything else to your base PDO files and/or do it a different way.

you have to omit the try..catch block. should you per-chance get a connection fault, you’d get an out-of-place output (the error message) and then a lot of "$dbc is not an object" errors all the way down, because a script doesn’t end at the chatch—it proceeds as if there were no issues.

therefore the rule-of-thumb for exception handling is: Catch the exception where you can handle (or fix) the problem.

since there is no way to fix a broken DB connection on runtime, you can only handle this particular exception on the most outer level of your programme, where you decide which page to show.

This post has been edited by Dormilich: 07 April 2016 - 12:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 12:56 AM

Quote

my question is how do I get there and send over the data info that I selected

all you need to send over is the primary key of the dataset. so when you load the form you can request the item data from the DB.
Was This Post Helpful? 0
  • +
  • -

#4 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 07 April 2016 - 05:51 AM

The primary key is the id. So how would I send the primary key?

This post has been edited by Dormilich: 07 April 2016 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#5 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 07 April 2016 - 05:59 AM

View PostDormilich, on 07 April 2016 - 12:10 AM, said:

you have to omit the try..catch block. should you per-chance get a connection fault, you’d get an out-of-place output (the error message) and then a lot of "$dbc is not an object" errors all the way down, because a script doesn’t end at the chatch—it proceeds as if there were no issues.

therefore the rule-of-thumb for exception handling is: Catch the exception where you can handle (or fix) the problem.

since there is no way to fix a broken DB connection on runtime, you can only handle this particular exception on the most outer level of your programme, where you decide which page to show.



I think I understand what you are saying. In a nutshell, are you saying that a try/catch isn't needed in the connection part of the call? That I should have it in other parts of the pages script that I could fix the issue?

If not, I am unsure of exactly what you mean.

This post has been edited by Dormilich: 07 April 2016 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 06:49 AM

yes & yes.
Was This Post Helpful? 0
  • +
  • -

#7 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 07 April 2016 - 08:25 AM

Thanks. Are there any other methods I should do within my connect code? I have figured out how to do prepared statements (for the most part) with PDO. What else is different with PDO? I thought at one point while I was on this forum there was a user with a PDO guide, I thought it was actually you Dormilich. I don't see anything within your signature for it now, though.

Regarding editing the record. I got it to work, but have some questions based on it. If I click on the record I want to edit, I am getting it to go to the different page and only bring up that information for it, which is what I want. I am used to seeing it show up in the page differently, though. For example my browser shows this no matter which record I click on:

/edit-product


What I am use to seeing is something like this:

viewProduct.php?view_product=2


Does it matter that my different records do not show up differently within the browser?


Also the way this is structured now, when I am on the edit-product page if I hit the browser reload, I get a "Confirm Form Re submission" alert come up. Is there anyway to make this not appear and just allow the page to be refreshed when the refresh button is hit?


Here is what I did:

First page - we can call it the products page:

<div>
		<form method="POST" id="pdo-add">
			<input name="first" id="pdo-add-first" placeholder="First Name">
			<input name="last" id="pdo-add-last" placeholder="Last Name">
			<input name="product" id="pdo-add-product" placeholder="Product">
			<input name="add" type="submit" value="Add">
		</form>
	</div>
	<hr>
	<table id="tableid">
		<thead>
			<tr>
				<th>ID</th>
				<th>First Name</th>
				<th>Last Name</th>
				<th>Product</th>
				<th>Save</th>
				<th>Delete</th>
				<th></th>
			</tr>
		</thead>
		<tbody>
		
		<?php
		$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
		$stmt->setFetchMode(PDO::FETCH_ASSOC);
		
		while($row = $stmt->fetch()) {
		?>
		<form method="POST" action="edit-product">
			<tr>
				<td><?php echo $row['id'];?>"</td>
				<td><?php echo $row['first'];?></td>
				<td><?php echo $row['last'];?></td>
				<td><?php echo $row['product'];?></td>
				<input name="id" type="hidden" value="<?php echo $row['id'];?>" readonly>
				<input name="first" type="hidden" value="<?php echo $row['first'];?>">
				<input name="last" type="hidden" value="<?php echo $row['last'];?>">
				<input name="product" type="hidden" value="<?php echo $row['product'];?>">
				<td><input name="save" type="submit" value="Save"></td>
				<td><input name="delete" type="submit" value="Delete"></td>
				<td><input name="edit" type="submit" value="Edit"></td>
			</tr>
		</form>
		<?php } ?>
		</tbody>
	</table>



When they hit the the edit button, it takes them to the edit-products page:

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

$newId = filter_var($id, FILTER_SANITIZE_STRING);


try {
	$host = 'localhost';
	$name = '';
	$user = '';
	$password = '';

	$dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
	echo $e->getMessage();
	
}


    $stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
	//print_r($stmt);
	while($row = $stmt->fetch()) {
	
?>
<a href="pdo-products">Back to Products</a>

<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">

<button name="update" type="submit">Update Product Information</button>

<?php 
}



Does this look like a good way to do this? Anything you would do differently?
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 08:45 AM

Quote

I don't see anything within your signature for it now, though.

the link resides in the pinned PHP Resources thread. I never had that tutorial in my signature.

Quote

For example my browser shows this no matter which record I click on

well, itís a POST request. its data donít show up in the URL.

Quote

Is there anyway to make this not appear and just allow the page to be refreshed when the refresh button is hit?

use GET, not POST.
Was This Post Helpful? 0
  • +
  • -

#9 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 07 April 2016 - 09:19 AM

Where would I use $_GET and isn't get a less safe approach? I tried changing the variables on my edit page to GET instead of POST (I actually changed it in my real code.)

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];



Would I have to change my form method to GET as well for the products page?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 09:22 AM

Quote

Would I have to change my form method to GET as well for the products page?

if you change something in PHP and nothing in the form, would you still expect it to work?
Was This Post Helpful? 0
  • +
  • -

#11 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 07 April 2016 - 09:25 AM

That's what I figured, but is this a safe approach? I haven't used php in a while, so I am a bit rusty, but I do remember when I was learning that it seemed that the $_GET form method was always frowned upon.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 09:29 AM

it depends on what you consider "safe" and for what purpose the form is used.

Quote

The GET method requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect.

if your form does not modify data (see quote) a GET submitted form can be considered "safe".

see also: https://en.wikipedia...ol#Safe_methods

On the other hand side, you donít need a form to link to the editing page.
Was This Post Helpful? 0
  • +
  • -

#13 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 07 April 2016 - 09:34 AM

How would I link to it then and get the correct data to send with it?
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: PDO Structure

Posted 07 April 2016 - 11:35 PM

with a standard hyperlink (<a>).
Was This Post Helpful? 0
  • +
  • -

#15 pfar54  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 425
  • Joined: 30-April 15

Re: PDO Structure

Posted 08 April 2016 - 05:55 AM

Doing this will send over the data without a form??

<a><td><input name="edit" type="submit" value="Edit"></a></td>


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3