Jump to content

Update Problem(Basic CRUD Operations)


joevanni08

Recommended Posts

Good Day, I'm doing my final requirement for my project. I've been trying to construct a website, but i'm having some difficulty here in the CRUD operations. The Update Link won't display the information that I needed, as you can see on the code, there's a header location if ever it didn't get the calamityno(which is the primary key in my table, declared as int). It keeps going on the header, I cannot understand why won't it get the value on that row.


Here's my code for the update:



<?php
require 'auth.php';
?>
<?php
if(isset($_GET['calamityno']) && ctype_digit($_GET['calamityno']))
{
$calamityno = $_GET['calamityno'];
}
else
{
header("Location: select.php");
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Update name</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<?php
readfile('navigation.tmpl.html');
?>

<?php
$pass = 0;
$type = "";
$residencesaffected = 0;
$noofcasualties = 0;
$estimatedcost = 0;
if (isset($_POST['submit']))
{
$pass = 1;

if (!isset($_POST['type']) || $_POST['type'] == "")
{
$pass = 0;
}
if (!isset($_POST['residencesaffected']) ||
$_POST['residencesaffected']== "")
{
$pass = 0;
}
if (!isset($_POST['noofcasualties']) || $_POST['noofcasualties'] ==
"")
{
$pass = 0;
}
if (!isset($_POST['estimatedcost']) || $_POST['estimatedcost'] ==
"")
{
$pass = 0;
}
else
{
$type = $_POST['type'];
$residencesaffected = $_POST['residencesaffected'];
$noofcasualties = $_POST['noofcasualties'];
$estimatedcost = $_POST['estimatedcost'];
}

if ($pass == 1)
{
$db = mysqli_connect('localhost', 'root', '', 'phpb');
$sql = sprintf("UPDATE records SET type='%s',
residencesaffected=%s,noofcasualties=%s, estimatedcost=%s WHERE
calamityno=%s", $type,
$residencesaffected, $noofcasualties, $estimatedcost,
$calamityno);
mysqli_query($db, $sql);
mysqli_close($db);
printf("Record has been updated.");
}

}
else
{
$db = mysqli_connect('localhost', 'root', '', 'phpb');
$sql = sprintf("SELECT * FROM records WHERE calamityno=%s",
$calamityno);
$results = mysqli_query($db, $sql);
foreach($results AS $row)
{
$type = $row['type'];
$residencesaffected = $row['residencesaffected'];
$noofcasualties = $row['noofcasualties'];
$estimatedcost = $row['estimatedcost'];
}
mysqli_close($db);
}
?>
<form method="post" action="update.php?id=<?php echo $calamityno; ?>">
<label>Type of Calamity:</label>
<input type="text" name="type" value="<?php echo $type; ?>"><br>
<label>No. of Residences Affected: </label>
<input type="text" name="residencesaffected" value="<?php echo
$residencesaffected; ?>"><br>
<label>No. of Casualties: </label>
<input type="text" name="noofcasualties" value="<?php echo
$noofcasualties;?>"><br>
<label>Estimated Cost: </label>
<input type="text" name="estimatedcost" value="<?php echo
$estimatedcost; ?>"><br>
<input type="submit" name="submit" value="Update Record">
</form>
</body>
</html>

The thing is, when I try to echo a message on the header, It displays the message that I echoed and an error that says undefined variable calamityno, and on the foreach part, there's an error that says invalid argument supplied on the foreach. I can't seem to find the error here.


Does that mean that my second php code on the upper part doesn't work?


Any answers are highly appreciated, thank you in advance.


-Joe


Link to comment
Share on other sites

Follow Up:

<?php
	require 'auth.php';
?> 

<?php
	if (isset($_GET['id']) && ctype_digit($_GET['id'])) 
	{ 
		$id = $_GET['id']; 
	} 
	else 
	{ 
		header('Location: select.php'); 
	}
?> 

<!DOCTYPE html> 
<html> 
<head> 
	<title>Update name</title> 
	<link rel="stylesheet" type="text/css" href="css/style.css"> 
</head> 
<body> 
<?php 
	readfile('navigation.tmpl.html'); 
?> 

<?php 
	$pass =  0; 
	$name = ""; 

	if (isset($_POST['submit'])) 
	{ 
		$pass = 1; 

		if (!isset($_POST['name']) || $_POST['name'] == "") 
		{ 
			$pass = 0; 
		} 
		else 
		{ 
			$name = $_POST['name']; 
		}

		if ($pass == 1) 
		{ 
			$db = mysqli_connect('localhost', 'root', '', 'phpb'); 
			$sql = sprintf("UPDATE users SET name='%s' WHERE id=%s", $name, $id); 
			mysqli_query($db, $sql); 
			mysqli_close($db); 
			printf("Data has been updated."); 
		} 

	} 
	else 
	{ 
		$db = mysqli_connect('localhost', 'root', '', 'phpb'); 
		$sql = sprintf("SELECT * FROM users WHERE id=%s", $id); 
		$result = mysqli_query($db, $sql); 

		foreach ($result as $row) 
		{ 
			$name = $row['name']; 
		} 
		mysqli_close($db); 
	} 
?> 
<form method="post" action="update.php?id=<?php echo $id; ?>"> 
	<input type="text" name="name" value="<?php echo $name; ?>"><br> 
	<input type="submit" name="submit" value="Update name"> 
</form> 
</body> 
</html>

This is the source code that came from my instructor, I just added as well as edit some of its variables, yet I really don't understand why isn't it working.
Is there any issues with regards in the names of variables?

P.S. I tested this set of codes before making my own and it's working.

Link to comment
Share on other sites

Well sir, what I know on ISSET function is, it checks the variable whether it is set or not, and the ctype_digit checks if the variable contains numerical values. Since based from those knowledge and the condition that I gave, it should go in to the first if statement, but it won't. I can't understand why won't it get the calamityno which is the primary key on my table. Did I miss a particular thing on that statement?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.