0

So this is for a school project. We are making a website and I am using PHP with MySQL and phpMyAdmin.
My problem is that when I am updating a product instead of changing the field, it erases all the data for the row besides the ProductID, which is the primary key.
***EDIT: I will address this here: This is a school project and thus security is not an issue for me.
I just need the products to update, now. The erasing problem has been solved.
** End Edit **
Code: update-page.php - has the actual form in it

 <'?'php
 session_start();

 require_once 'db_connect.php';

 $query = "SELECT * FROM Products";

 $result = mysql_query($query);

 if (!$result) {
 die("Database query failed: " . mysql_error());
 }
 $num = mysql_num_rows($result);

 ?>

 <!DOCTYPE html>
 <html lang="en">

 <head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>AppleRetail - Update Product</title>

<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="css/update-custom.css" rel="stylesheet">

 </head>

 <body>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html">AppleRetail.com</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li>
                    <a href="products-page.php">Products</a>
                </li>
                <li>
                    <a href="add-page.php">Add Product</a>
                </li>
                <li>
                    <a href="delete-page.php">Delete Product</a>
                </li>
                <li>
                    <a href="update-page.php">Update Price</a>
                </li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

<!-- Page Content -->
<div class="container">

    <!-- Jumbotron Header -->
    <header class="jumbotron hero-spacer">
        <h1>Update Product</h1>
        <p>Change the information below and select "Update" to update the product information.</p>
    </header>

    <hr>
<form action="update.php" method="POST">
    <table class="table table-bordered table-hover">
        <thead>
          <tr>
            <th>Product ID</th>
            <th>Name</th>
            <th>Description</th>
            <th>Price</th>
            <th>Image URL</th>
          </tr>
        </thead>

        <tbody>
    <?php while($row = mysql_fetch_array($result)) { ?>
            <tr>
            <input type="hidden" name="id" value="<?php echo $row['ProductID']; ?>"/>
                <td><?php echo $row['ProductID']; ?></td>
                <td><input type="text" name="name" value="<?php echo $row['Name']; ?>"/></td>
                <td><input type="text" name="description" value="<?php echo $row['Description']; ?>"/></td>
                <td><input type="text" name="price" value="<?php echo $row['Price']; ?>"/></td>
                <td><input type="text" name="image" value="<?php echo $row['Image']; ?>"/></td>
                <td>
                 <input type="submit" name="update" value="Update">
                </td>
            </tr>
    <?php }?>
        </tbody>
    </table>
</form>
    <!-- Footer -->
    <footer>
        <div class="row">
            <div class="col-lg-12">
                <p>Copyright &copy; AppleRetail.com</p>
            </div>
        </div>
    </footer>

</div>
<!-- /.container -->

<!-- jQuery -->
<script src="js/jquery.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>

And then the update.php has the query in it.

 <?php
  session_start();

  require_once 'db_connect.php';

 // Grab the posted data and send to variable
 $ProductID = $_POST['ProductID'];
 $Name = $_POST['Name'];
 $Description = $_POST['Description'];
 $Price = $_POST['Price'];
 $Image = $_POST['Image'];

 $_SESSION['ProductId1'] =$ProductId;
 $_SESSION['Name1'] = $Name;
 $_SESSION['Description1'] = $Description;
 $_SESSION['Price1'] = $Price;
 $_SESSION['Image1'] = $Image;

  $query = "UPDATE Products SET Name='$Name', Description='$Description', Price='$Price', Image='$Image' WHERE ProductID='$ProductID'";
  $result = mysql_query($query);

  if (!$result) {
   die("Database query failed: " . mysql_error());
  }

  if($result =='true'){echo "<p>Post is add</p>";}
  else{ echo "<p>Post is not add</p>"; }
 ?>

  <!doctype html>
  <html>
  <head>
  <meta charset="utf-8">
  <title>AppleRetail Update Page</title>
  </head>
 <body>
 <?php header("Location: updateFinal.php"); ?>

 </body>
 </html>

Any Help is appreciated! Thank you! Please ask if more information is needed!

shay2013
  • 13
  • 6
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 11 '15 at 18:20
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 11 '15 at 18:21
  • @JayBlanchard This is just a school project. No one is going to be using it. It's all fake. And for the functions, it's how the teacher taught and what I'm supposed to be using. – shay2013 Dec 12 '15 at 19:52

1 Answers1

1
input type="text" name="name"
input type="text" name="description"

Compare that with

$_POST['Name'];
        ^
$_POST['Description'];
        ^

And spot the error ;)

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95