Hi guys!
I have a database which holds a table called users. in there i have a balance column which contains a numeric value.
I have the following code and for some reason it doesnt work:
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<br>
<div id="books-wrapper">
<!-- #content to center the menu -->
<div id="content">
<!-- This is the actual menu -->
<ul id="darkmenu">
<li><a href="adminHome.php">Home</a></li>
<li><a href="addBook.php">New Books</a></li>
<li><a href="adminSearch.php">Search</a></li>
<li><a href="updateBalance.php">Update Balance</a></li>
</ul>
</div>
<div id = "welcome" >
Welcome, <?=$_SESSION['Username'];?>! <br> <a href="logout.php">Logout</a>
</div>
<br><br>
<h1 id = "mainHeader" >Update a Students Balance</h1>
<br>
<div id = "balanceupdate">
<form id = "adsearch" action="updateBalance.php" method="post">
<input type="text" name ="search" placeholder="Search For a Student">
<button name="submit" value="search">Search</button>
</form>
<br>
</div>
<?php
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$result = $mysqli->query( "SELECT * FROM Users WHERE Username ='$searchq'");
if ($result){
//fetch result set as object and output HTML
if($obj = $result->fetch_object())
{
echo '<div class="booksearched">';
echo '<form method="POST" id = "books" action="">';
echo '<div class="book-content"><h3>Student Username: '.$obj->Username.'</h3>';
echo '<input type="hidden" name="username" value = "'.$obj->Username.'"" />';
echo '<br>';
echo '<div class="book-content"><i>First Name: <b>'.$obj->FirstName.'</b></i></div>';
echo '<div class="book-desc"><i>Last Name:<b> '.$obj->LastName.'</b></i></div>';
echo '<br>';
echo '<div class="book-qty"> Current Balance<b> '.$obj->Balance.'</b></div>';
echo 'New Balance: <input type="number" name="newBalance" value = "1" min = "1" />';
echo '<br><br>';
echo '<button name="submit_btn" class="save_order">Top Up</button>';
echo '</div>';
echo '</form>';
echo '</div>';
}
}
if(isset($_POST['submit_btn']) && !empty($_POST['newBalance']) ){
$newBalance = $_POST['newBalance'];
$username = $_POST['username'];
$upsql = "UPDATE users SET Balance = Balance + '$newBalance' WHERE Username='".$username ."'";
$stmt = $mysqli->prepare($upsql);
$stmt->execute();
}
}
?>
</body>
</html>
I have a textbox called newBalance and want the update statement to add the newBalance to the original balance that is in the database based on the user that has been searched for between lines 39-40.
Currently im able to search for a user and click search, this shows the users first name, last name, Userid and their current balance. The textbox which i enter the new balance is directly below. I dont have any errors showing up when i run the code and nothing is update.
I really need to get this fixed
any help will be appreciated.