Jump to content

Increment Table Variable


joe904

Recommended Posts

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);
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by joe904
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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);
	
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

@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);
Link to comment
Share on other sites

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.