masral
masral

Reputation: 89

updating a mysqli database with php

I'm trying to get user input from a html form and then use that data to change a row in a mysql database. It also is supposed to load a list of the current table so the user can pick which row to change. Here is my code so far

<?php
    require('conn.php');
    error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_WARNING);
    ini_set("display_errors", 1);
    if( isset($_POST['send'])){
    $Change=htmlspecialchars($_POST["change"]);
    $Product_Name = htmlspecialchars($_POST["product_name"]);
    $Stock= htmlspecialchars($_POST["stock"]);
    $Price=htmlspecialchars($_POST["price"]);
    $update="UPDATE product SET Product_Name='$Product_Name', Stock='$Stock', Price='$Price' WHERE $Product_Name='$Change'";
            if (mysqli_query($conn,$update)){
                    echo "Record updated";
    } else{
    echo "Error updating record: " . mysqli_error($conn);
    }
    }
    $sql = "SELECT * FROM product";
    $result = mysqli_query($conn,$sql);
    if(!$result){
            echo 'Could not run query: ' . mysqli_error($conn);
            exit;
    }

    print   "<table border='1'>\n";
    print " <tr><th>Product Name</th><th>Total Stock</th><th>Price</th>  </tr>\n";

    while($row = mysqli_fetch_row($result)){
    print " <tr>\n";
    print "<td>".$row['0']."</td>";
    print "<td>".$row['1']."</td>";
    print "<td>".$row['2']."</td>";
    print " </tr>\n";
}
print "</table>\n";

mysqli_close($conn);
?>

<html>
<body>
<form action="update.php" method="post">

<table border="1">

<tr>
    <td>Row to Change</td>
    <td align="center"><input type="text" name="change" size="30" /></td>
</tr>

<tr>
    <td>Product Name</td>
    <td align="center"><input type="text" name="product_name" size="30" /></td>
</tr>

<tr>
    <td>In Stock</td>
    <td align="center"><input type="text" name="stock" size="30" /></td>
</tr>

<tr>
    <td>Price</td>
    <td align="center"><input type="text" name="price" size"30" /></td>
</tr>

<tr>
    <td>Submit</td>
    <td align="center"><input type="submit" name="send" value="send"></td>
</tr>

However, currently the page is blank when I load it up. The html portion works fine part of the as well as this portion of the php code

$sql = "SELECT * FROM product";
$result = mysqli_query($conn,$sql);
if(!$result){
        echo 'Could not run query: ' . mysqli_error($conn);
        exit;
}

print   "<table border='1'>\n";
print " <tr><th>Product Name</th><th>Total Stock</th><th>Price</th>  </tr>\n";

while($row = mysqli_fetch_row($result)){
print " <tr>\n";
print "<td>".$row['0']."</td>";
print "<td>".$row['1']."</td>";
print "<td>".$row['2']."</td>";
print " </tr>\n";
}
print "</table>\n";

mysqli_close($conn);
?>

When I added the rest it only showed a blank page. What can I do to change this and get the form input to update and change the respective row in my database?

Upvotes: 0

Views: 37

Answers (1)

gtryonp
gtryonp

Reputation: 407

Where does this braket close?

if( isset($_POST['send'])){

close properly the brakets after the update / insert statements, guess before the

$sql = "SELECT * FROM product";

statement.

(PS Try to use an programming editor (like notepad++). It shows the open/close proper tags. Really helpful.

Also a

error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_WARNING);
ini_set("display_errors", 1);

on the first line will help, too.

Upvotes: 1

Related Questions