joevanni08 Posted October 5, 2017 Share Posted October 5, 2017 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 5, 2017 Share Posted October 5, 2017 Compare: if(isset($_GET['calamityno']) && ctype_digit($_GET['calamityno'])) Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 Any elaboration sir? Did try to change them into post or get, still no response when i'm clicking the update link. Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2017 Share Posted October 5, 2017 Ask yourself what do those lines do? Then ask yourself why don't they do it? Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2017 Share Posted October 5, 2017 The answer to the first of my questions is "Sends data in the query string and the other line GETs that data". So why doesn't it? Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 There's an error on my syntax? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2017 Share Posted October 5, 2017 Syntax is fine. What are you sending and what are you attempting to GET? Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 I'm trying to get an integer, and trying to send a string, and 3 integers. Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 OH, I think I already figured it out... wait let me check. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2017 Share Posted October 5, 2017 In reply #2 you send, and attempt to receive, 1 value. What are you sending and what are you trying to receive? Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 (edited) Oh, so does that mean that when I received a single value, I can only send 1 value as well? Edited October 5, 2017 by joevanni08 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2017 Share Posted October 5, 2017 In those two lines (reply #2) the first line id GETting a value. The second line is sending a value in the query string (after the "?"). Whaat is the name of the item you are sending and what is the name of the item you are trying to receive? Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 I think I've already got it, regarding the names on my form, since I named one as "type", which ended up being a confusion to the syntax of my codes, is that right? Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 Oh, if you're referring to this one, $_POST['name'], sorry for the confusion, that is the name of the row on the sample table of our instructor. Quote Link to comment Share on other sites More sharing options...
joevanni08 Posted October 5, 2017 Author Share Posted October 5, 2017 (edited) Already solved the problem. Thank you for the help Good Sir @requinix, specially Sir @Barand. Sorry if I gave you a hard time, been awake for more than 24 hours so my mind is quite in disarray.Problem has been solved. Edited October 5, 2017 by joevanni08 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
× 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.