简体   繁体   中英

PHP code with variables not UPDATING MySQL db

I am a newbie-ish to MySQL/PHP (using XAMPP). I have tried every recommendation I have seen here for code but nothing seems to work....

I have a 3-column, 19 row table of textboxes (row1Field1, Row1, field2, row1Field3, etc.)

I am trying to UPDATE my database for those records where there will be records for each row (almost)

I KNOW THERE IS NO REAL SQL INJECTION PROTECTION. That is not an issue at this point. This is all opn my personal laptop & most likely will never see the Internet.

Here is my coe, 99% works everytime. The UPDATE does not.

<?php require '../connect/connect.php';  //connects the db (10 pages so far - all works
// Start the session
session_start();
$wk2Use = $_SESSION["wk2Use"];
?>

<html>
    <body>
        <?php

        $f="";
        $s=0;
        $p=0;

        for($i=1; $i <= 19; $i++) {
            $f = "gm" . $i . "Fav";
            $s = "gm" . $i . "Sprd";
            $p = "gm" . $i . "Pts";
            $f = htmlspecialchars($_POST[$f]);
            $s = htmlspecialchars($_POST[$s]);
            $p = htmlspecialchars($_POST[$p]);

            if (!empty($f)) {
                //This displays the correct data!
                echo 'Wk-' . $wk2Use . '  ' . $f . ' ' . $s . ' ' . $p . '<br>';            


                //This does NOT work
                $sql = ("USE bbschdefs; UPDATE bbschdefs SET sSchFav='".$f."', sSchSprd=$s, sSchOUPts=$p 
                        WHERE ((sSchVTAbrv='".$f."' OR sSchHTAbrv='".$f."') AND schWk=$wk2Use )");
                $result = mysqli_query($dbconn,$sql);           
            }
        }   
        $i=0;

        include '../predictions/predict.php' 

        ?>

    </body>


</html>

I'd have to guess it's because your variable $wk2Use is not properly being added to your where clause

WHERE ((sSchVTAbrv='".$f."' OR sSchHTAbrv='".$f."') AND schWk=$wk2Use )

Should be

WHERE ((sSchVTAbrv='".$f."' OR sSchHTAbrv='".$f."') AND schWk='{$wk2Use}' )

OR

WHERE ((sSchVTAbrv='".$f."' OR sSchHTAbrv='".$f."') AND schWk='".$wk2Use."' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM