joe904 Posted March 13, 2018 Share Posted March 13, 2018 Hey Guys!!, I am trying to figure out how to write some php code to increment a variable. I am using php version 7.1.2. I have tried looking for sources online to figure it out, but have not been able to find a good how-to. I am trying to run a simple php application that every time it runs, starting with 0 it adds +1 to my 'count' column in my 'students' database. Any help is much appreciated. CREATE TABLE `students` ( `ID` int(11) NOT NULL, `Name` varchar(11) NOT NULL, `count` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `students` (`ID`, `Name`, `count`) VALUES (12345, 'James Smith', 0), (56789, 'Mike Sims', 0); Quote Link to comment Share on other sites More sharing options...
requinix Posted March 13, 2018 Share Posted March 13, 2018 UPDATE students SET count = count + 1Pretty sure there's something important you've not mentioned... Quote Link to comment Share on other sites More sharing options...
joe904 Posted March 13, 2018 Author Share Posted March 13, 2018 I am not sure what I am missing. I am a newbie at this. How would I incorporate this SQL stmt into PHP or HTML? Would I have to do some kind of while loop, or something else? Can you please give me an example if possible. Quote Link to comment Share on other sites More sharing options...
requinix Posted March 13, 2018 Share Posted March 13, 2018 If you want update all students then you don't need a loop or anything. You just execute the query. And you do that by however you normally execute queries in your application. Exactly how much of this application has been done so far? Quote Link to comment Share on other sites More sharing options...
joe904 Posted March 13, 2018 Author Share Posted March 13, 2018 (edited) temp.tecwarrior.com Test ID - 123456 Once an ID is inputted the student info comes up. "Student Damage" should automatically increment +1...Keeping track of damage Edited March 13, 2018 by joe904 Quote Link to comment Share on other sites More sharing options...
requinix Posted March 14, 2018 Share Posted March 14, 2018 Okay, now I'm thinking that you do not, in fact, want to update every student's damage count. You only want to update one student, and that's the student whose ID was entered. What's the code for the page that shows the student information? Quote Link to comment Share on other sites More sharing options...
joe904 Posted March 14, 2018 Author Share Posted March 14, 2018 (edited) Yes that is correct....Only the student that was entered <!DOCTYPE html> <html lang="en"> <?php include 'connection.php';?> <head> <meta charset="utf-8"> <link rel="shortcut icon" href="images/favicon1.ico"/> <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"> <script src="js/jquery-3.3.1.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://unpkg.com/sweetalert2@latest/dist/sweetalert2.all.js"></script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2"></script> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <title>Student Damage Report</title> </head> <style> body { background-image: url("images/back1.jpg"); width: 100%; height: 400px; background-repeat: no-repeat; background-size: contain; } </style> <body> <div class="title1">STUDENT DAMAGE DATABASE</div> <form method="post" id="theFormID" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <div class="form_question"> <center><label>Please Enter Student ID#</label> <input type="text" id="theFieldID" name="student_id" placeholder="" /></p> </div></center> <script type="text/javascript">theFormID.theFieldID.focus();</script></form> <?php if (isset($_POST['student_id'])) { $studentId = $_POST['student_id']; // handle case where we don't get a student id $sql = 'SELECT * FROM `students` WHERE `studentId` = ?'; $stmt = $pdo->prepare($sql); $stmt->bindParam(1, $studentId, PDO::PARAM_INT); $stmt->execute(); $student = $stmt->fetch(PDO::FETCH_ASSOC); if ($student) { ?> <p class="response"><mark>Student ID# <?= $student['studentId']?></mark><br>First Name: <?= $student['first_name']?><br>Last Name: <?= $student['last_name']?><br>Grade: <?= $student['grade']?><br>Gender: <?= $student['gender']?> <br>Student Damage: <?= $student['damage#']?></p> <?php } else { ?> <p class="notification">Student <?php echo $studentId ?> was not found in the database</p> <?php } } ?> <div id="footer"> <div class="w3-container"> <div class="w3-dropdown-click"> <button onclick="myFunction()" class="w3-button w3-black"><img src="images/print.png" width=50px height=50px></button> <div id="Demo" class="w3-dropdown-content w3-bar-block w3-card-4 w3-animate-zoom"> <a href="javascript:window.print()" class="w3-bar-item w3-button">Print Form</a> <a href="records.php" class="w3-bar-item w3-button">Print Database</a> </div> </div> </div> <script> function myFunction() { var x = document.getElementById("Demo"); if (x.className.indexOf("w3-show") == -1) { x.className += " w3-show"; } else { x.className = x.className.replace(" w3-show", ""); } } </script></div> </body> </html> Edited March 14, 2018 by joe904 Quote Link to comment Share on other sites More sharing options...
requinix Posted March 14, 2018 Share Posted March 14, 2018 So the damage and the count are two different things... You need to run another query UPDATE `students` SET `count` = `count` + 1 WHERE `studentId` = ?before the SELECT query. If you understand the code you have now then you should be able to, more or less, copy how that SELECT query is being executed and apply it to this new UPDATE. Quote Link to comment Share on other sites More sharing options...
joe904 Posted March 14, 2018 Author Share Posted March 14, 2018 No they are actually the same. The column in my database for where the damage count will be stored is named `damage#` I tried inserting the query but I keep getting an error. <?php if (isset($_POST['student_id'])) { $studentId = $_POST['student_id']; // handle case where we don't get a student id 'UPDATE `students` SET damage# = `damage#` + 1 WHERE `studentId` = ?'; $sql = 'SELECT * FROM `students` WHERE `studentId` = ?'; $stmt = $pdo->prepare($sql); $stmt->bindParam(1, $studentId, PDO::PARAM_INT); $stmt->execute(); $student = $stmt->fetch(PDO::FETCH_ASSOC); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 14, 2018 Share Posted March 14, 2018 Line 51 does not belong where it is. If you had php error checking turned on (see my signature) you would be getting notice of your mistake. Of course you may be getting that error but since you did not show us it, how are we to know? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 14, 2018 Share Posted March 14, 2018 @joe904: Where did you get this code? Do you have any experience in coding? I don't want to be rude, but it seems you really don't understand code at all. There's nothing wrong with that, there plenty of things I don't have any knowledge of. But, this is a PHP "Help" forum designed to provide help to people with code they are writing. just looking at the existing code, you should recognize that the SELECT query takes multiple lines of code to process. Just slapping some text into the middle of the code doesn't make a new query run. Just looking at the existing lines of code should provide some guidance on how to write the new query // Create a string of the query to be run - using ? as a placeholder $sql = 'SELECT * FROM `students` WHERE `studentId` = ?'; // Using the database object, create a prepared statement from the query $stmt = $pdo->prepare($sql); // Apply the value for $studentId to the placeholder in the prepared statement $stmt->bindParam(1, $studentId, PDO::PARAM_INT); // Execute the query $stmt->execute(); // "Fetch" the results of the query - not needed for an update query $student = $stmt->fetch(PDO::FETCH_ASSOC); 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.