Need some help with updating MySQL database with PHP and forms

sstoveld

New Member
hey guys, im having some trouble with a school assignment of mine. the assignment buids on previous assignments. this one consists of a mysql database that has different brands of coats, sizes, and stock available of the coat (like a coat distributor).

this is what i have so far:

Code:
<?php
////////// CONNECT TO SERVER //////////

$dbcnx = @mysql_connect('localhost', 'dbase18', 'newer333') or die ('I cannot connect to the database because: ' .mysql_error());

///////// CONNECT TO DATABASE ////////////
mysql_select_db('dbase18', $dbcnx);
if (!@mysql_select_db('dbase18')) {
	exit ('<p>Error in query.'. mysql_error(). '<p>');
}

////////// MAKE AN HTML TABLE TO HOLD THE DATA ////////////
?>
        
    <table>
    	<tr>
        	<td><strong>Brand</strong></td>
            <td><strong>Stock (XXL)</strong></td>
            <td><strong>Update</strong></td>
        </tr>
<?php
//////////// QUERY DATABASE /////////////
$queryresult = @mysql_query('select brand.id, brand_name, stock from brand left join stock on brand.id = stock.brand_id left join size on size.id = stock.size_id where size.id = 3;');
if (!$queryresult) {
	exit ('<p>Error in query.'. mysql_error().'</p>');
}

/////////// RESULTS OF QUERY AND PUBLISH TO TABLE ///////////
while ($row = mysql_fetch_array($queryresult)){
	echo '<tr><td>'.$row['brand_name'].'</td><td>'.$row['stock'].'</td><td><a href=edit.php?id='.$row['id'].'>Edit</a></td></tr>';
}
?>
	</table>

now i dont have any problems this far, but im confused on how to redirect to an update page that will allow the user to update the quantity. how its supposed to work us the user can click the edit link next to a quantity, and it will take the user to a form where he can put in a new quantity then hit an update button. im not sure how to redirect to the edit page.

below are some screenshots of my html table and my tables in my database:

asdfleu.jpg


yrtyrty.jpg


thanks for any help in advance
 

conor

New Member
First of all there are two ways to do comments:
Code:
// comment here
/* multiple
line
comment */

What I reccomend is to put all the connection details in a seperate file called 'connect.php' include that in your file above and in your new file called edit.php. This is what I would to but it's a bit rough:

Code:
/* Include the connection information */

require_once 'connect.php';

/* Update processing */

if(isset($_POST['save'])){
    $new_value = addslashes($_POST['new_value']);
    mysql_query('update table set value="'.$new_value) or die(mysql_error());
    unset($new_value);
    $updated = '<h1>Database Updated</h1>';
}

/* If form has been submited echo the updated notice */

if(isset($updated)) echo $updated;

/* Echo the update form */

echo '
<form method="post">
    <input type="text" name="new_value"/>
    <input type="submit" name="save" value="Save"/>
</form>
';

/* Close page */
mysql_close();
exit;

The above assumes that you are only updating one value. You will also have to change the query to be compatible with your database.

Hope that helps.
 
Last edited:
Top