SQL Database - Update Record via PHP Form. No Errors, but no update.

15,624

I don't see anything in the second script setting those variables. The form values don't automatically pass into variables, you have to retrieve them from the $_POST array variable, which is why you have that 'post' method in your form on the first page. You do it like this:

if (isset($_POST['barcode']) && $_POST['barcode'] != "") {
    $barcode = $_POST['barcode'];
} else {
    echo "'barcode' input is not set.";
}

You don't actually need the if statement, but it's nice for troubleshooting and general error prevention. It just makes sure the field was both set and not empty.

(I would explain arrays, but it seems you already have a grasp on them based on your loop with the $rows variable. If you need the help there let me know and I'll give a brief explanation and link you out to a tutorial.)

Share:
15,624
Chris Edwards
Author by

Chris Edwards

Updated on June 04, 2022

Comments

  • Chris Edwards
    Chris Edwards almost 2 years

    Hoping this is the right area to be putting this. At current I'm working on a little project for fun and at the same time attempting to teach myself some PHP. I've created a little 'Audit' page that's associated with my website that allows me to Input new data to a pre-existing SQL Database, Search on that Data and Update that Data. Now, the Adding and Search works without a Hitch. The only problem is the 'Update' function does not... It does not Error (in fact it states it is successful) and it all works how it should, except that it does not 'actually' update the information within the table.

    I've spent the past 2-3 days reading through StackOverflow, Tutorial Guides, Forums and just playing with parts of the code myself, none of which has provided even a 'remote' answer.

    I have 2 pages that are relevant, the 'update.php' page and the 'update_ac.php' page that runs the script after hitting 'submit' on the Form, the code is as below:

    update.php

    <?php
    $host="localhost"; // Host name 
    $username="member2"; // Mysql username 
    $password="********"; // Mysql password 
    $db_name="audits"; // Database name 
    $tbl_name="data"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // get value of id that sent from address bar
    $id=$_GET['id'];
    
    // Retrieve data from database 
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    
    $rows=mysql_fetch_array($result);
    ?>
    
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <form name="form1" method="post" action="update_ac.php">
    <td>
    <table width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>&nbsp;</td>
    <td colspan="3"><strong>Update data in mysql</strong> </td>
    </tr>
    <tr>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td>
    <td align="center">&nbsp;</td> 
    </tr>
    <tr>
    <td align="center">&nbsp;</td>
    <td align="center"><strong>Barcode</strong></td>
    <td align="center"><strong>Product</strong></td>
    <td align="center"><strong>Category</strong></td>
    <td align="center"><strong>Assigned</strong></td>
    <td align="center"><strong>Notes</strong></td> 
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td align="center">
    <input name="barcode" type="text" id="barcode" value="<? echo $rows['barcode']; ?>">
    </td>
    <td align="center">
    <input name="product" type="text" id="product" value="<? echo $rows['product']; ?>" size="15">
    </td>
    <td align="center">
    <select name="category" id="category" value="<? echo $rows['category']; ?>">
    <option value="Hardware">Hardware</option>
    <option value="Software">Software</option>
    <option value="Furniture">Furniture</option>
    </select>
    </td>  
    <td align="center">
    <select name="assigned" id="assigned" value="<? echo $rows['assigned']; ?>">
    <option value="name1">Name 1</option>
    <option value="name1">Name 2</option>
    <option value="name1">Name 3</option>
    <option value="name1">Name 4</option>
    </select>
    </td>
    <td align="center">
    <input name="notes" type="text" id="notes" value="<? echo $rows['notes']; ?>" size="15">
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
    <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
    </td>
    <td align="center">
    <input type="submit" name="Submit" value="Submit">
    </td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    
    <?php
    // close connection 
    mysql_close();
    ?>
    

    update_ac.php

    <?php
    $host="localhost"; // Host name 
    $username="member2"; // Mysql username 
    $password="********"; // Mysql password 
    $db_name="audits"; // Database name 
    $tbl_name="data"; // Table name 
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // update data in mysql database 
    $sql="UPDATE $tbl_name SET barcode='$barcode', product='$product', category='$category', assigned='$assigned', notes='$notes' WHERE id='$id'";
    $result=mysql_query($sql);
    
    // if successfully updated. 
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='assets.php'>View Changes</a>";
    echo "<BR>";
    echo "<a href='login-home.php'>Home</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?> 
    

    Now, I did pull this from an older site and kind of 'changed' to suit my own needs to no avail. I even did a complete copy/paste of their Tutorial (inclusive of their Database) and even that did not work, which seems to indicate something is wrong with the code somewhere. Thanks in advance.