How to update mysql column using php after submitting html form?

Hi guys!

I would like to ask help on this. I just recently learned coding in html and php.
The one I’m struggling to solve is that I can’t get to update the x and y columns after I click the ‘Submit’ button.

The code I’ve been working on are as follows:

`<!--display image after queried from db buildings table-->  
        <section class="row placeholders">
          <div class="map-header col-md-3" style="margin-left: 30px; margin-top: 50px; ">
            <!--call $img query to display image-->
            <?php if(isset($img) && mysqli_num_rows($img)) : ?>
              <?php while($row = mysqli_fetch_assoc($img))
                {
                  //the variable for the map_image_filepath stored in database
                  $filepath = $row['map_image_filepath'];
               
                  //stores x and y coordinates when image is clicked
                  $foo_x = isset ($_POST['foo_x']) ? $_POST['foo_x'] : '';
                  $foo_y = isset ($_POST['foo_y']) ? $_POST['foo_y'] : '';

                  //checks if file path queried exists
                  if(file_exists($filepath))
                  {
                    //displays the image
                    echo "<input type='image' src='{$filepath}'
                            name='foo' style='cursor:crosshair; border-right: #000000 2px outset;  border-bottom: #000000 2px outset; border-left: #000000 2px outset; border-top: #000000 2px outset;' height='300px' width='400px'>";
                    //for x 
                    echo"<div class='form-group form-inline' style='margin-top: 35px;'>";
                    echo"  <label class='form-inlineinput' for='inlineinput1'> X </label>";
                    //x textbox
                    echo"<input class='form-control form-control-sm' type='text' id='inlineinput' value='$foo_x' name='forx' size='10px'>";
                    echo"</div>";

                    //for y 
                    echo"<div class='form-group form-inline'>";
                    echo"  <label class='form-inlineinput' for='inlineinput1'> Y </label>";
                    //y textbox
                    echo"<input class='form-control form-control-sm' type='text' id='inlineinput' value='$foo_y' name='fory' size='10px'>";
                    echo"</div>";
                  }
                  else
                  {
                    //default image to display if no image is found
                    echo "<img src='../img/no map.jpg' height='300' width='400'>";
                  }
                }
              ?>

          <?php
            //query location table for the dropdown
            $lquery = mysqli_query($con, "
            SELECT location_ID, location_name FROM location
            WHERE location.building_ID = {$selectedbldg}
            ");

            //declare variable to store selected location option
            $selectedloctn = null;

            //if the form for location dropdown list was submitted
            if(!empty($_POST['loctn']))
            {
              //store selected location_ID
              $selectedloctn = $_POST['loctn'];

              mysqli_query($con, "
              UPDATE location
              SET x='$foo_x', y='$foo_y'
              WHERE location_ID={$selectedloctn}
              ");         
            }   
          ?>
          <!--location dropdown list-->
          <select name="loctn" class="dropdown" style="margin-left: 30px; margin-top: 35px;">
            <option value="">Choose Location</option>;
              <?php while ($row = mysqli_fetch_assoc($lquery)) : ?>
                <option value="<?= $row['location_ID'] ?>" <?= $row['location_ID'] == $selectedloctn ? 'selected="selected"' : '' ?>>
                <?= $row['location_name'] ?></option>
              <?php endwhile ?>
          </select>
            <input type="submit" name="submit2" value="Save" class="btn btn-primary btn-xs" />
          <?php endif?>
        </form>
        </div>
      </section>`

The x and y values to update are obtained from the $foo_x and $foo_y variables.

Any help would be very much appreciated and I would really love to learn in the process.

Thanks a lot in advanced!

You are missing to ask for errors: mysqli_error(). If it does not show up, any of your conditions would prevent the query to be executed, use var_dump() to look at what the conditions give back and what the variables contain.

1 Like

Hi @chorn! Thanks for replying.
I am sorry if I were to ask which part of the code should I add those? :frowning:

After querying the DB.

1 Like

Thanks for the help @chorn! :slight_smile:

However since it all got mixed up, I decided to change my code and the updating function worked!

<!--display image after queried from db buildings table-->  
        <section class="row placeholders">
          <div class="map-header col-md-3" style="margin-left: 30px; margin-top: 50px; ">
            <!--call $img query to display image-->
            <?php if(isset($img) && mysqli_num_rows($img)) : ?>
              <?php while($row = mysqli_fetch_assoc($img))
                {
                  //the variable for the map_image_filepath stored in database
                  $filepath = $row['map_image_filepath'];

                  //stores x and y coordinates when image is clicked
                  $foo_x = isset ($_POST['foo_x']) ? $_POST['foo_x'] : '';
                  $foo_y = isset ($_POST['foo_y']) ? $_POST['foo_y'] : '';

                  //checks if file path queried exists
                  if(file_exists($filepath))
                  {
                    //displays the image
                    echo "<input type='image' src='{$filepath}'
                    name='foo' style='cursor:crosshair; border-right: #000000 2px outset;  border-bottom: #000000 2px outset; border-left: #000000 2px outset; border-top: #000000 2px outset;' height='300px' width='400px'>";
                    //for x 
                    echo"<div class='form-group form-inline' style='margin-top: 35px;'>";
                    echo"  <label class='form-inlineinput' for='inlineinput1'> X </label>";
                    //x textbox
                    echo"<input type='text' id='inlineinput' value='$foo_x' name='forx' size='10px'>";
                    echo"</div>";
                    //for y 
                    echo"<div class='form-group form-inline'>";
                    echo"  <label class='form-inlineinput' for='inlineinput1'> Y </label>";
                    //y textbox
                    echo"<input type='text' id='inlineinput' value='$foo_y' name='fory' size='10px'>";
                    echo"</div>";

                    //2nd dropdown for location selection
                    echo "<select name='loctn' class='dropdown' style='margin-top: 35px;'>";
                    echo "<option value=''>Choose Location</option>";
                      while($row=mysqli_fetch_assoc($lquery))
                      {
                        $locID=$row['location_ID'];
                        $locname=$row['location_name'];
                        echo '<option value="'.$locID.'">'.$locname.'</option>';
                      }
                    echo "</select>";
                    echo "<input type='submit' name='submit2' value='Save' class='btn btn-primary btn-xs'>";

                    //query to update x and y coordinates in location table
                    $query=mysqli_query($con, "SELECT x and y FROM location");
                    $row=mysqli_fetch_assoc($query);
                    if(isset($_POST['submit2']))
                    {
                      $xcor=$_POST['forx'];
                      $ycor=$_POST['fory'];

                      $result=mysqli_query($con, "UPDATE location SET x='$xcor', y='$ycor' WHERE location_ID='$locID' ");
                    }
                  }                    
                  else
                  {
                    //default image to display if no image is found
                    echo "<img src='../img/no map.jpg' height='300' width='400'>";
                  }
                }
              ?>
          <?php endif?>

But, the excitement was cut off when after several trials, the update button doesn’t work anymore. For testing purposes, I have A and B in my dropdown and if ever I choose to update A, the x and y values get saved to B. I don’t know where I got it all wrong in my updated code.

Please help :sob:

What does the source html code look like for your form? Can you post the page source here?

I still don’t see you asking for SQL errors with mysqli_error or debugging your variables with var_dump. If you do not look at what you provide to your script you will never know what the problem is, and nobody else will know how to help you.

I am sorrry @WebMachine, if i understood it right, you meant the source code for the form in submitting to update the x and y?

I’m sorry @chorn for missing to add it after I changed my code. I added the mysqli_error($result) ; after the update query (i hope I placed it right, please correct me if i’m wrong) and it displayed a warning after I clicked the “save” button - > “Warning: mysqli_error() expects parameter 1 to be mysqli boolean given… in line 169”

I also added var_dump($xcor); and var_dump($ycor) ; to check my variables and it showed the values of x and y respectively:

string(3) “126” string(2) “66”

Yes. It might not be relevant - I just thought it might be easier to see if there were issues with the form.

1 Like

That’s not how you call it.

mysqli_query($connection, $yourQuery) || die(mysqli_error($connection));

That will print a reason for the query failing when it fails, and stop processing the rest of the script. When the query runs ok nothing is output.

for testing purposes only, do not put code like this in production sites!

No, actually I meant the page source you get when you display the form in the browser. I was just wondering what you are seeing for $locID and $locname in the form source code.

I’m sorry @WebMachine :frowning:
This is the page source I get when I display the form in the browser:

<!--building dropdown list-->  
      <form name="bldg_form" method="post" action="">
        <select name="bldg" class="dropdown" style="margin-left: 30px; margin-top: 35px;">
          <option value="">Choose Building</option>
                          <option value="1" selected="selected">
              building 12          </option>
                          <option value="2" >
              building 13          </option>
                          <option value="3" >
              building 14          </option>
                    </select>
          <input type="submit" name="submit1" value="Select" class="btn btn-primary btn-xs" />
         
        <!--display image after queried from db buildings table-->  
        <section class="row placeholders">
          <div class="map-header col-md-3" style="margin-left: 30px; margin-top: 50px; ">
            <!--call $img query to display image-->
                          <input type='image' src='../img/map.jpg'
                    name='foo' style='cursor:crosshair; border-right: #000000 2px outset;  border-bottom: #000000 2px outset; border-left: #000000 2px outset; border-top: #000000 2px outset;' height='300px' width='400px'><div class='form-group form-inline' style='margin-top: 35px;'>  <label class='form-inlineinput' for='inlineinput1'> X </label><input type='text' id='inlineinput' value='49' name='forx' size='10px'></div><div class='form-group form-inline'>  <label class='form-inlineinput' for='inlineinput1'> Y </label><input type='text' id='inlineinput' value='109' name='fory' size='10px'></div><select name='loctn' class='dropdown' style='margin-top: 35px;'><option value=''>Choose Location</option><option value="1">A</option><option value="4">D</option></select><input type='submit' name='submit2' value='Save' class='btn btn-primary btn-xs'>                  </form>
        </div>
      </section>
    </form>

Thank you @rpkamp! :smiley:
This is the output after the query:
“You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘1’ at line 1”

Without seeing the query it’s rather hard to determine whats going on. Could you echo the query just before it’s executed and post the result here?

I would like to also add that you should put all PHP heavy code at the top of your file. This makes it easier to read without having the spaghetti code mash up with your HTML codes. It will also lessen the chances of getting headers already sent error if you happen to put a header redirect there.

1 Like

Sorry for the late reply and for adding the wrong code.
Please correct me if I get this wrong. I am still learning this and I really am confused.

I added it right after the query for UPDATE:

//query to update x and y coordinates in location table
                    $query=mysqli_query($con, "SELECT x and y FROM location");
                    $row=mysqli_fetch_assoc($query);
                    if(isset($_POST['submit2']))
                    {
                      $xcor=$_POST['forx'];
                      $ycor=$_POST['fory'];

                      $result=mysqli_query($con, "UPDATE location SET x='$xcor', y='$ycor' WHERE location_ID='$locID' ") or die(mysqli_error($con));
                    }
                  }                    

where should be echoed? the $result?

@rpkamp it didnt display any error. I presume the query is ok. But i really dont know what’s wrong on why it updates another option instead of what i clicked from the dropdown.

Is this proper syntax for SQL?

$query=mysqli_query($con, "SELECT x and y FROM location");

I’ve only ever seen it done as

$query=mysqli_query($con, "SELECT x,y FROM location");

In any case, what’s the need for that query? You select the x and y values from the location table for all rows.

Which of the two queries is giving you the syntax error message?

No, the query.

So, one the line before the UPDATE:

echo "UPDATE location SET x='$xcor', y='$ycor' WHERE location_ID='$locID' ";