Jump to content

Having trouble echoing out the "name" field in delete confirmation


Go to solution Solved by esteemed_squire,

Recommended Posts

Hi All,

 

Would appreciate some pointers in how I can echo out the "name" field in my delete confirmation. I understand the process on echoing the $name variable after the message but haven't been able to figure out how to define the variable that holds the name field value upon deletion.

 

Do I need to setup another mysql query to get the value from the name field in a assoc array?

 

Any help or pointers for better understanding would be very much appreciated.

 

Thanks!

 

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"];
$name = $_POST["name"];
 
// Delete data
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "DELETE FROM customers WHERE id = ?, name = ?";
$stmt = $pdo->prepare($query);
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->bindValue(2, $name, PDO::PARAM_STR);
$stmt->execute();
Database::disconnect();
header("Location: index.php");
 
}
?>
 
HTML:
<form class="form-horizontal" action="delete.php" method="post">
<input type="hidden" name="id" value="<?php echo $name; ?>" />
<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>

 

WHERE id = ?, name = ?

 

The above WHERE syntax is wrong.

 

It should be either

WHERE id = ? OR name = ?

or

 

WHERE id = ? AND name = ?

depending on the logic you want to apply

Edited by Barand

Thanks for that, but am still getting an undefined variable when I echo out $name after the delete confirmation message.

 

<p class="alert alert-error">Are you sure you want to delete?<?php echo $name; ?></p>

 

Where do I define this?

Right I have tried that, but still get the "Notice: Undefined variable: name in" message after the delete confirmation.

 

Trying to figure out where do I define the $name variable that echos out the name field from the database.

You're getting that notice because the (hidden) field that contains the name is actually called id not name

<input type="hidden" name="id" value="<?php echo $name; ?>" />

Either rename that field to name, or change $_POST['name'] to $_POST['id']

You're getting that notice because the (hidden) field that contains the name is actually called id not name

<input type="hidden" name="id" value="<?php echo $name; ?>" />

Either rename that field to name, or change $_POST['name'] to $_POST['id']

On tried your suggestion by swapping out the fieldname, still getting that notice. Can step me through the changes in the code?

I didn't look at your code properly yesterday.

 

Is the PHP code you posted from delete.php? What file is the HTML code from?

 The entire below for the CRUD app, I'm fairly certain I just need to get the value for $name and echo it out in the delete confirmation message but am not sure how to setup the code to get the value for $name in the delete section.

 

Full code below:

// 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 ?>

<!DOCTYPE HTML>
<html lang="en">
	<head>
		<meta charset="uft-8">
		<link href="http://localhost/projects/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<script src="http://localhost/projects/bootstrap/js/bootstrap.min.js"></script>
		<title></title>
	</head>
	<body>
		<h1></h1>
		<div class="container">
			<h3>PHP CRUD Grid</h3>
			<div class="row">
				<p>
					<a href="create.php" class="btn btn-success">Create</a>
				</p>
				<table class="table table-striped table-bordered">
					<thead>
						<tr>
							<th>Name</th>
							<th>Email Address</th>
							<th>Mobile Number</th>
							<th>Action</th>
						</tr>
					</thead>
					<tbody>
					<?php
					include("database.php");
					$pdo = Database::connect();
					$sql = "SELECT * FROM customers ORDER BY id DESC";
						foreach ($pdo->query($sql) as $row) {
							echo "<tr>";
							echo "<td>" . $row["name"]   . "</td>";
							echo "<td>" . $row["email"]  . "</td>";
							echo "<td>" . $row["mobile"] . "</td>";
							echo "<td width=250>";
							echo '<a class="btn" href="read.php?id='.$row["id"].'">Read</a>';
							echo " ";
							echo '<a class="btn btn-success" href="update.php?id='.$row["id"].'">Update</a>';
							echo " ";
							echo '<a class="btn btn-danger" href="delete.php?id='.$row["id"].'">Delete</a>';
							echo "</td>";
							echo "</tr>";
						}
						Database::disconnect();
					?>
					</tbody>
				</table>
			</div>
		</div>
	</body>
</html>


<?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 ?>

<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title></title>
		<meta charset="utf-8">
		<link href="http://localhost/projects/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<script src="http://localhost/projects/bootstrap/js/bootstrap.min.js"></script>
	</head>
	<body>
		<h1></h1>
		<div class="container">
			<div class="span10 offset1">
				<div class="row">
					<h3>Create a Customer</h3>
				</div>
				
				<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>
</html>


<?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();
	}

?>

<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title></title>
		<meta charset="utf-8">
		<link href="http://localhost/projects/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<script src="http://localhost/projects/bootstrap/js/bootstrap.min.js"></script>
	</head>
	<body>
		<h1></h1>
		<div class="container">

			<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>
</html>


<?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();
		}
?>

<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title></title>
		<meta charset="uft-8">
		<link href="http://localhost/projects/bootstrap/css/bootstrap.min.css" rel="stylesheet"> 
		<script src="http://localhost/projects/bootstrap/js/bootstrap.min.js"></script>
	</head>
	<body>
		<h1></h1>
		<div class="container">

			<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>
</html>


<?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");

	}
?>

<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title></title>
		<meta charset="utf-8">
		<link href="http://localhost/projects/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<script src="http://localhost/projects/bootstrap/js/bootstrap.min.js"></script>
	</head>
	<body>
		<h1></h1>
		<div class="container">

			<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>
</html>
  • Solution

Figured it out with the help from the author of the tutorial, something so simple yet couldn't get my head around it.

 

Code that goes into the delete page.

	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();
	}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.