busby Posted January 4, 2011 Share Posted January 4, 2011 Hey i have this code that should update database on submit of a form. i have multiple rows selected from a database and i need to be able to update each row individually...however currently as the code stands it updates all rows with the data entered into the bottom row. i dont know how to solve this its very frustrating could someone take a look and help me? here is the code <?php session_start(); ?> <a href="adminlogout.php">Logout</a><br /> <?php $id = $_GET['id']; if(!isset($_SESSION['myusername'])) { header("location:adminlogin.php"); } $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $db = 'bank'; $conn = mysql_connect($dbhost,$dbuser,$dbpass); mysql_select_db($db); if(isset($_POST['submit'])){ $select = mysql_query("SELECT * FROM accounts WHERE cusid=$id"); $row3 = mysql_fetch_array($select); $update = mysql_query("UPDATE accounts SET balance='".$_POST['balance']."', type='".$_POST['type']."', name='".$_POST['name']."', active='".$_POST['active']."' WHERE cusid=$id"); } $result = mysql_query("SELECT * FROM customer WHERE cusid=$id"); $row2 = mysql_fetch_array($result); echo $row2['name'] . "'s bank accounts" . "<br><br>"; $result2 = mysql_query("SELECT * FROM accounts WHERE cusid=$id"); echo "<form method='post' action='accounts.php?id=$id'>"; while($row = mysql_fetch_array($result2)) { echo "<input type='text' name='accno' style='background-color:lightgrey;' readonly='readonly' value='$row[accno]'>" . "<input type='text' name='name' value='$row[name]'>"; if($row['type'] == "Current"){echo"<select name='type'>" . "<option selected='Selected'>Current</option>" . "<option>Savings</option>" . "</select>";} else{echo"<select name='type'>" . "<option selected='Selected'>Savings</option>" . "<option>Current</option>" . "</select>";} echo"<input type='text' name='balance' value='$row[balance]'>"; if($row['active'] == "No"){ echo "<select name='active'>" . "<option value='No' selected='selected'>No</option>" . "<option value='Yes'>Yes</option>" . "</select>";} elseif($row['active'] == "Yes"){echo "<select name='active'>" . "<option value='Yes' selected='selected'>Yes</option>" . "<option value='No'>No</option>" . "</select>";} echo "<input type='submit' name='submit' value='Update'>" . "<br>"; } echo "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/ Share on other sites More sharing options...
otuatail Posted January 4, 2011 Share Posted January 4, 2011 From what I see this is just a form with information taken from a table. You need to sho us what is in accounts.php. That is where the problem will be. Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154595 Share on other sites More sharing options...
busby Posted January 4, 2011 Author Share Posted January 4, 2011 all that code is accounts.php it selects the data from database and displays it in rows on the page. each bit of data is displayed in a text box for the user to edit...and then there is a submit button at the end of each row which when clicked should update that particular row in the database with the new edited info. but at the moment when the submit button is clicked it just updates all the rows with whatever the bottom row says. Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154596 Share on other sites More sharing options...
litebearer Posted January 4, 2011 Share Posted January 4, 2011 when you are updating multiple rows from a form, you need to make the form values arrays; otherwise you end up passing 1 value for each form item. Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154597 Share on other sites More sharing options...
revraz Posted January 4, 2011 Share Posted January 4, 2011 echo $update and post what it returns. Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154598 Share on other sites More sharing options...
busby Posted January 4, 2011 Author Share Posted January 4, 2011 it shouldnt be updating multiple rows. there is a submit button at the end of each row...the user will edit the data in the text boxes of a row...then press the submit button at the end of that row and it should update that row in the database. no other rows should get updated Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154599 Share on other sites More sharing options...
busby Posted January 4, 2011 Author Share Posted January 4, 2011 it just displays the number "1" which is the userid for that particular person logged in. Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154602 Share on other sites More sharing options...
otuatail Posted January 4, 2011 Share Posted January 4, 2011 if this is a form showing more than one account then you can't have just one ID. Surley they would all have there own ID's for the query Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154610 Share on other sites More sharing options...
busby Posted January 4, 2011 Author Share Posted January 4, 2011 yes each bank account has an "accid" which is its unique identifier. Quote Link to comment https://forums.phpfreaks.com/topic/223355-help-updating-database-please-please/#findComment-1154613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.