Adding name of item in delete confirmation

Hello,

I am stumped on where to add include the name of the item being deleted so it’s included in the delete confirmation? (e.g., Are you sure you want to delete XXX ?)

Right now I get a undefined variable in my “Are you sure you want to delete?” delete confirmation as I’m blindly trying to define the name field in the code.

Where do I insert the name field so it displays in the confirmation?

Cheers!

Delete Code:
[php]
// Delete data
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = “DELETE FROM customers WHERE id = ?”;
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->execute();
Database::disconnect();
header(“Location: index.php”);
[/php]

HTML confirmation:
[php]

Are you sure you want to delete?

Yes No
[/php]

Give this a try…

[php]

Are you sure you want to delete <?php echo $id; ?>?

[/php]

Thanks for the tip, I’m looking to echo it the “name” field after the confirmation message. Do I need to set up another mysql query and assign that result to variable to echo out?

Yes, the ID echos out but am looking to echo out the name field instead.

Wherever you set $id you need to set a $name and populate it then.

Just tried your suggestion, dunno why it’s not working as it should be something simple to just echo out the field name in the deletion confirmation message.

You havent shown any code containing a variable holding a name. If you don’t have it you either have to includebit in an existing query, or run a new query fetching name where id = the id you’ve got

Can you show us the entire code of the delete.php without separating it.

Entire code posted below:

[php]

<?php // DATABASE CODE class Database { private static $dbName = "crud_tutorial"; private static $dbHost = "localhost"; private static $dbUsername = "root"; private static $dbPassword = ""; private static $connection = null; public function __construct() { die("Init function is not allowed"); } public static function connect() { // One connection through whole application if (null == self::$connection) { try { self::$connection = new PDO("mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbPassword); } catch(PDOException $e) { die($e->getMessage()); } } return self::$connection; } public static function disconnect() { self::$connection = null; } } ?> <?php // INDEX CODE ?>

PHP CRUD Grid

Create

<?php include("database.php"); $pdo = Database::connect(); $sql = "SELECT * FROM customers ORDER BY id DESC"; foreach ($pdo->query($sql) as $row) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } Database::disconnect(); ?>
Name Email Address Mobile Number Action
" . $row["name"] . "" . $row["email"] . "" . $row["mobile"] . ""; echo 'Read'; echo " "; echo 'Update'; echo " "; echo 'Delete'; echo "
<?php require("database.php"); if (!empty($_POST)) { // Keep track of validation errors $nameError = null; $emailError = null; $mobileError = null; // Keep track post values $name = $_POST["name"]; $email = $_POST["email"]; $mobile = $_POST["mobile"]; // Validate input $valid = true; if (empty($name)) { $nameError = "Please enter Name"; $valid = false; } if (empty($email)) { $emailError = "Please enter Email Address"; $valid = false; } if (empty($mobile)) { $mobileError = "Please enter Mobile Number"; $valid = false; } // Insert data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "INSERT INTO customers (name, email, mobile) values (?, ?, ?)"; $stmt = $pdo->prepare($query); $stmt->bindValue(1, $name, PDO::PARAM_STR); $stmt->bindValue(2, $email, PDO::PARAM_STR); $stmt->bindValue(3, $mobile, PDO::PARAM_INT); $stmt->execute(); Database::disconnect(); header("Location: index.php"); } } ?> <?php // CREATE CODE ?>

Create a Customer

			<form class="form=horizontal" action="create.php" method="post">
				<div class="control-group <?php echo !empty($nameError) ? "error" : ""; ?>">
					<label class="control-label">Name</label>
					<div class="controls">
						<input type="text" name="name" placeholder="Name" value="<?php 
						echo !empty($name) ? $name : ""; ?>" />
						<?php if (!empty($nameError)): ?>
							<span class="help-inline"><?php echo $nameError; ?></span>
						<?php endif; ?>
					</div>
				</div>
				
				<div class="control-group <?php echo !empty($emailError) ? "error" : ""; ?>">
					<label class="control-label">Email Address</label>
					<div class="controls">
						<input type="text" name="email" placeholder="Email Address" value="<?php
						echo !empty($email) ? $email : ""; ?>" />
						<?php if (!empty($emailError)): ?>
							<span class="help-inline"><?php echo $emailError; ?></span>
						<?php endif; ?>
					</div>
				</div>
				
				<div class="control-group <?php echo !empty($mobileError) ? "error" : ""; ?>">
					<label class="control-label">Mobile Number</label>
					<div class="controls">
						<input type="text" name="mobile" placeholder="Mobile Number" value="<?php
						echo !empty($mobile) ? $mobile : ""; ?>" />
						<?php if (!empty($mobileError)): ?>
							<span class="help-inline"><?php echo $mobileError; ?></span>
						<?php endif; ?>
					</div>
				</div>

					<div class="form-actions">
						<button type="submit" class="btn btn-success">Create</button>
						<a class="btn" href="index.php">Back</a>
					</div>
			</form>
		</div>
	</div> <!-- container end -->
</body>
<?php // READ CODE ?> <?php require("database.php"); $id = null; if (!empty($_GET["id"])) { $id = $_REQUEST["id"]; } if (null == $id) { header("Location: index.php"); } else { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "SELECT * FROM customers where id = ?"; $stmt = $pdo->prepare($query); $stmt->bindValue(1, $id, PDO::PARAM_INT); $stmt->execute(); $data = $stmt->fetch(PDO::FETCH_ASSOC); Database::disconnect(); } ?>

		<div class="span10 offset1">
			<div class="row">
				<h3>Read a Customer</h3>
			</div>

			<div class="form-horizontal">
				<div class="control-group">
					<label class="control-label">Name</label>
					<div class="controls">
						<label class="checkbox">
							<?php echo $data["name"]; ?>
						</label>
					</div>
				</div>

				<div class="control-group">
					<label class="control-label">Email Address</label>
					<div class="controls">
						<label class="checkbox">
							<?php echo $data["email"] ?>
						</label>
					</div>
				</div>

				<div class="control-group">
					<label class="control-label">Mobile Number</label>
					<div class="controls">
						<label class="checkbox">
							<?php echo $data["mobile"] ?>
						</label>
					</div>
				</div>
					
					<div class="form-actions">
							<a class="btn" href="index.php">Back</a>
					</div>
			</div>
		</div>
	</div> <!-- end of container -->
</body>
<?php // UPDATE CODE ?> <?php require("database.php"); $id = null; if (!empty($_GET['id'])) { $id = $_REQUEST["id"]; } if (null == $id) { header("Location: index.php"); } if (!empty($_POST)) { // Keep track of validation errors $nameError = null; $emailError = null; $mobileError = null; // Keep track post values $name = $_POST["name"]; $email = $_POST["email"]; $mobile = $_POST["mobile"]; // Validate input $valid = true; if (empty($name)) { $nameError = "Please enter Name"; $valid = false; } if (empty($email)) { $emailError = "Please enter Email Address"; $valid = false; } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailError = "Please enter a valid Email Address"; $valid = false; } if (empty($mobile)) { $mobileError = "Please enter Mobile Number"; $valid = false; } // Update data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "UPDATE customers set name = ?, email = ?, mobile = ? WHERE id = ?"; $stmt = $pdo->prepare($query); $stmt->bindValue(1, $name, PDO::PARAM_STR); $stmt->bindValue(2, $email, PDO::PARAM_STR); $stmt->bindValue(3, $mobile, PDO::PARAM_INT); $stmt->bindValue(4, $id, PDO::PARAM_INT); $stmt->execute(); Database::disconnect(); header("Location: index.php"); } } else { // For fields that are empty use the default values $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "SELECT * FROM customers WHERE id = ?"; $stmt = $pdo->prepare($query); $stmt->bindValue(1, $id, PDO::PARAM_INT); $stmt->execute(); $data = $stmt->fetch(PDO::FETCH_ASSOC); $name = $data["name"]; $email = $data["email"]; $mobile = $data["mobile"]; Database::disconnect(); } ?>

		<div class="span10 offset1">
			<div class="row">
				<h3>Update a customer</h3>
			</div>

			<form class="form-horizontal" action="update.php?id=<?php echo $id ?>" method="post">
				
				<div class="control-group <?php echo !empty($nameError) ? "error" : ""; ?>">
					<label class="control-label">Name</label>
					<div class="controls">
						<input type="text" name="name" placeholder="Name" value="<?php echo !empty($name) ? $name : ""; ?>">
						<?php if (!empty($nameError)): ?>
							<span class="help-inline"><?php echo $nameError; ?></span>
						<?php endif; ?>
					</div>
				</div>

				<div class="control-group <?php echo !empty($emailError) ? "error" : ""; ?>">
					<label class="control-label">Email Address</label>
					<div class="controls">
						<input type="text" name="email" placeholder="Email Address" value="<?php echo !empty($email) ? $email : ""; ?>" />
						<?php if (!empty($emailError)): ?>
							<span class="help-inline"><?php echo $emailError; ?></span>
						<?php endif; ?>
					</div>
				</div>

				<div class="control-group <?php echo !empty($mobileError) ? "error" : ""; ?>">
					<label class="control-label">Mobile Number</label>
					<div class="controls">
						<input type="text" name="mobile" placeholder="Mobile Number" value="<?php echo !empty($mobile) ? $mobile : ""; ?>" />
						<?php if (!empty($mobileError)): ?>
							<span class="help-inline"><?php echo $mobileError; ?></span>
						<?php endif; ?>
					</div>
				</div>

				<div class="form actions">
					<button type="submit" class="btn btn-success">Update</button>
					<a class="btn" href="index.php">Back</a>
				</div>
			</form>
		</div>
	</div> <!-- container end -->
</body>
<?php // DELETE CODE ?> <?php require("database.php"); $id = 0; if (!empty($_GET["id"])) { $id = $_REQUEST["id"]; } if (!empty($_POST)) { // Keep track of post values $id = $_POST["id"]; // Delete data $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = "DELETE FROM customers WHERE id = ?"; $stmt = $pdo->prepare($query); $stmt->bindValue(1, $id, PDO::PARAM_INT); $stmt->execute(); Database::disconnect(); header("Location: index.php"); } ?>

		<div class="span10 offset1">
			<div class="row">
				<h3>Delete a Customer</h3>
			</div>

			<form class="form-horizontal" action="delete.php" method="post">
				<input type="hidden" name="id" value="<?php echo $id; ?>" />
				<p class="alert alert-error">Are you sure you want to delete?</p>
				<div class="form-actions">
					<button type="submit" class="btn btn-danger">Yes</button>
					<a class="btn" href="index.php">No</a>
				</div>
			</form>
		</div>
	</div> <!-- container end -->
</body>
[/php]

I understand what I need to do but am unsure how to do it.

I need to get the value of name and then echo it out in the delete confirmation message. But how do I get the value in code?

I figured it out with the help of the author of the tutorial. Here is the code I was missing, something simple yet couldn’t get my head around it.

[php]
if (!empty($_GET[“id”])) {
$id = $_REQUEST[“id”];

	$pdo = Database::connect();
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$sql = "SELECT * FROM customers where id = ?";
	$q = $pdo->prepare($sql);
	$q->execute(array($id));
	$data = $q->fetch(PDO::FETCH_ASSOC);
	
	//get $name value
	$name = $data['name'];
	Database::disconnect();
}

[/php]

There’s that SELECT statement we were looking for that was not part of your original post, then he assigned the $name variable which is pulled from the SELECT statement.

Awesome you got it working!

Sponsor our Newsletter | Privacy Policy | Terms of Service