使用更新语句的未定义索引

时间:2015-03-25 15:58:00

标签: php mysqli

我试图通过添加已存在的原始数字值来更新数据库中的字段。 我有一个系统,员工可以登录并更新普通用户的余额。目前我有一个测试用户和工作人员。用户余额设置为100.我有以下代码:

<?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 '<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>';
        }
    }
}

    $newBalance="";
$newBalance = $_POST['newBalance'];

if(isset($_POST['submit_btn']) ){
    $upsql = "UPDATE users SET Balance = Balance + '$newBalance' WHERE Username='" . $obj->Username . "'";
    $stmt = $mysqli->prepare($upsql);
    $stmt->execute();
}

?>

我尝过了一些事情但是我一直收到错误说:

( ! ) Notice: Undefined index: newBalance 

我不确定我做错了什么。 知道怎么解决吗?

修改:完整代码

<?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 '<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>';
        }
    }
}

    $newBalance="";

    if(isset($_POST['submit_btn']) && !empty($_POST['newBalance']) ){

        $newBalance = $_POST['newBalance'];

        $upsql = "UPDATE users SET Balance = Balance + '$newBalance' WHERE Username='" . $obj->Username . "'";
        $stmt = $mysqli->prepare($upsql);
        $stmt->execute();
    }

?>
</body>
</html>

1 个答案:

答案 0 :(得分:6)

因为你需要在$newBalance = $_POST['newBalance'];内放置if(isset($_POST['submit_btn'])){...}并确认它不是空的(或设置),所以它会引发注意。

$newBalance="";

if(isset($_POST['submit_btn']) && !empty($_POST['newBalance']) ){

$newBalance = $_POST['newBalance'];

    $upsql = "UPDATE users SET Balance = Balance + '$newBalance' 
              WHERE Username='" . $obj->Username . "'";
    $stmt = $mysqli->prepare($upsql);
    $stmt->execute();
}

您也可以使用isset($_POST['newBalance'])代替!empty($_POST['newBalance'])


旁注:您可能希望为按钮添加提交类型。

echo '<button type="submit" name="submit_btn" class="save_order">Top Up</button>';

然而,可能不需要;如果你还遇到问题,请尝试一下。


修改

echo '<div class="book-content"><h3>Student Username: '.$obj->Username.'</h3>';

添加

echo '<input type="hidden" name="username" value = "'.$obj->Username.'"  />';

然后在

$newBalance = $_POST['newBalance'];

添加

$username = $_POST['username'];

并将您的查询修改为

$upsql = "UPDATE users SET Balance = Balance + '$newBalance' 
          WHERE Username='".$username ."'";

我的引言可能有点偏执

echo '<input type="hidden" name="username" value = "'.$obj->Username.'"  />';

您可能需要将其更改为

echo '<input type="hidden" name="username" value = '".$obj->Username."'  />';

编辑#2:

由于您已经在使用会话<?=$_SESSION['Username'];?>,因此执行此操作的另一种方法是为其分配变量并将其传递给您的查询。

$username = $_SESSION['Username'];

$upsql = "UPDATE users SET Balance = Balance + '$newBalance' 
          WHERE Username='".$username ."'";

编辑#3:

你在哪里

if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

替换为

if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$student = $_POST['search'];
$_SESSION['student'] = $student;

echo $_SESSION['student']; // see what echos here

然后在你的查询中,执行:

$upsql = "UPDATE users SET Balance = Balance + '$newBalance' 
          WHERE Username='".$student ."'";
  • 如果这不起作用,我不知道还有什么可以做的,这会有进一步的帮助。我的测试是确凿的并且有效。您的查询可能会失败,此时我没有更多想法。

基于这种情况:

$_POST['search'] = "student1";

$student = $_POST['search'];

$_SESSION['student'] = $student;

// echo $_SESSION['student'];

$student2 = $student;

echo $student2; // will echo student1