$as = mysql_query('SELECT u.id,u.username,c.score FROM user u, course c WHERE u.id = c.userid ');
echo '<form action="score.php" method="post"><table>';
while($row = mysql_fetch_array($as)
{
$uid = $row['id'];
$username = $row['username'];
$score = $row['score'];
echo '<tr><td>'.$username.'</td>
<td><input type="hidden" name="uid" value='.$uid.'>
<input type="text" name="score" value='.$score.'>
</td>
</tr>
}
echo '<tr><td><input type="submit" name="submit" value="update"></td></tr>';
echo '</table></form>';
if($_SERVER['REQUEST_METHOD == 'POST']
{
$uid = $_POST['uid'];
$score = $_POST['score'];
$sql = mysql('UPDATE user SET c.score = '.$score.' WHERE c.userid = '.$uid.'');
}
course table
userid score
4 45%
3 30%
5 80%
It wasn't updating to table. And I tried to echo the variables, It was showing only the last row, but I edited for user 3 Can anyone suggest where I went wrong
Your reusing the same input, so it will only submit the last one
change
echo '<tr><td>'.$username.'</td>
<td><input type="hidden" name="uid" value='.$uid.'>
<input type="text" name="score" value='.$score.'>
</td>
</tr>
to
echo '<tr><td>'.$username.'</td>
<td><input type="hidden" name="uid['.$uid.']" value='.$uid.'>
<input type="text" name="score['.$uid.']" value='.$score.'>
</td>
</tr>
ALSO
if(sizeof($_POST)>0)
{
if(is_array($_POST['uid']))
{
while(list($key,$value)=each($_POST['uid'])
{
$sql="UPDATE user SET score='".mysql_real_escape_string($_POST['score'][$key])."' WHERE userid=".intval($value);
mysql_query($sql);
}
}
}
Related
I am making a book management system.I have a return button ,with that button user can return books.
Here is my release-books.php:
<?php
$sql_select = "SELECT * FROM carti ORDER BY titlu";
$rezultat = mysqli_query($conn, $sql_select);
if (mysqli_num_rows($rezultat) > 0) {
while ($row = mysqli_fetch_assoc($rezultat)) {
$disabled = $row['stoc'] > 0 ? "" : "disabled"; ?>
<tr><td><input type="submit" name="id" value="<?php echo $row['idCarte']; ?>" <?php echo $disabled; ?> formaction="imprumutare.php"></input></td>
<td><input type="submit" name="returnare" value="<?php echo $row['idCarte']; ?>" formaction="returnare_carte.php"></input>
</td>
<td><input type="text" name="nume" value="<?php echo $row['titlu']; ?>" ></input></td>
<td><input type="text" name="" value="<?php echo $row['autor']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['editura']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['categorie']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['data_adaugarii']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['stoc']; ?>"></input></td>
</tr>
<?php
}
}
?>
And here is my borrowing.php
include('conexiune.php');
//sfarsit if
//Imprumutare
if (isset($_POST['id'])) {
$identificator = $_POST['id'];
$carte_nume = $_POST['nume'];
$current_date = date('y:m:d');
$current_date_plus_14days = date('y:m:d', strtotime("+14 days"));
$nume_carte = $_POST['nume'];
$insert_in_imprumuturi = "INSERT INTO imprumuturi(idc,nume_carte,data,termen_returnare,carti_imprumutate) VALUES('$identificator','$carte_nume','$current_date','$current_date_plus_14days','1')";
mysqli_query($conn, $insert_in_imprumuturi) or die(mysqli_error($conn));
$sql_rezervare = "UPDATE carti SET stoc=stoc-1 WHERE iDCarte='$identificator' ";
if (mysqli_query($conn, $sql_rezervare)) {
header('Refresh:0,url=emitere_carti.php');
} else {
die(mysqli_error($conn));
}
}
But I do not know how to disable the return button when the returned books volume is equal or greater that the originial volume
Can somone help me?
Check If your books volume is equal or greater than original volume, if it is. Use PHP echo to disable the Return Button.
<button type="submit" name="return"
<?php
if($returned_books_volume >= $original_books_volume){
echo 'disabled';
}
?>
>Return</button>
I have a web page that outputs data from my sql database. Each row has a delete button that should delete the data in this particular row. I am having a issue where when I click the delete button it always deletes the row with the first/lowest ID regardless of which button I click. It doesnt delete the row I want it to delete. Here is my code:
HTML
<form action="process.php" method="post">
<?php
$sql = "
SELECT *
FROM playerTeam
";
$result = mysqli_query($connection, $sql);
?>
<table>
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><input type="text" name="id" value="<?php echo $row['id']?>"></td>
<td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
<td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
<td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
<td><input type="submit" name="delete" value="Delete"></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
<?php
}
}
?>
</table>
</form>
process.php
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$sql = "DELETE FROM playerTeam WHERE id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
}
please could someone help me. Thanks
Because you delete the value of last id in
$id = $_POST['id'];
$sql = "DELETE FROM playerTeam WHERE id='$id'";
the value of $_POST['id'] is equal to last row in <?php echo $row['id']?>
in each run of the while the $_POST['id'] value replaced by new $row['id'] so the last on would be read in $_POST['id']
As you are using while($row = mysqli_fetch_assoc($result)), this will loop to the last row in your MySQL Table, thus every time, it will have the last id of your table.
You can code it in a way that your delete script will get the id. So, your delete button for each row will be have the row id, e.g. process.php?1, process.php?2, process.php?3 and so on.
tablepage.php (not sure of your page's real name)
Replace this line:
<td><input type="submit" name="delete" value="Delete"></td>
With this:
<td><p><a href="/process.php?id=<?php echo $id; ?>"></td>
process.php?id=1
// this will get `id=1` and thus your `id` will be `1`, and it will delete row `1`.
$id = $_GET['id'];
$sql = "DELETE FROM playerTeam WHERE id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
Hope that helps, thanks!
In actual in this condition ,use ajax and jquery is much more easier i think .But do only by php gonna be use logic code
$i = 0;
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><input type="text" name="id_<?= $i ?>" value="<?php echo $row['id']?>"></td>
<td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
<td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
<td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
<td><input type="submit" name="delete[<?= $i ?>]" value="Delete"></td>
<td><input type="submit" name="update[<?= $i ?>]" value="Update"></td>
</tr>
<?php
$i++; //for unique num for each row
}
process.php
if (isset($_POST['delete'])){
$delete_id = array_keys($_POST['delete']);//print array key as a array
$delete_id = $delete_id[0];//so get array key
$id = $_POST["id_$delete_id"];//get its relative id
.....
}
I have this form and out of this form, I can get 2 values from the radio buttons with post. But, I want to able to send another value winch is in my while loop the $fieldname variable with my form. I just don't know how to do that.
This my code:
$result = mysqli_query($con,"SELECT * FROM Velden");
while($row = mysqli_fetch_array($result)) {
echo "<div>";
echo "<h1>".$row['name']."</h1>";
echo "<h3>".$row['locatie']."</h3>";
echo '<img src="images/'.$row['photo'].'" width="120px" height="120px"/>';
echo "<p>".$row['aanwezig']."</p>";
$namefield = $row['name'];
$players = mysqli_query($con, "SELECT name, user_status FROM veld_user WHERE user_status=1 AND name='$namefield'");
echo "veld: ".$row['name']."<br />";
$number = mysqli_num_rows($players);
echo "Aantal spelers aanwezig: ".$number."<br /><br />";
?>
<form action="" method="post" id="registerForm">
<table class="form imageFrom">
<tr>
<td><input checked type="radio" name="status" value="1"/> aanwezig</td> <?php if (isset($_POST['status']) && $_POST['status']=='1') echo ' STATUS="aanwezig"';?>
<td><input checked type="radio" name="status" value="0"/> afwezig</td><?php if (isset($_POST['status']) && $_POST['status']=='0') echo ' STATUS="afwezig"';?>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit" class="knop"/></td>
</tr>
</table>
</form><?php
echo"</div>";
}
And this is the code were i get the post. And update my database
if(isset($_POST['submit'])){
if (isset($_POST['status']) && $_POST['status']=='1'){
$sql = "UPDATE veld_user SET user_status = 1 WHERE id=".$user->data()->id;}
elseif (isset($_POST['status']) && $_POST['status']=='0'){
$sql = "UPDATE veld_user SET user_status = 0 WHERE id= ".$user->data()->id;}
if (mysqli_query($con, $sql)) {
Session::flash('home', 'update success');
} else {
echo "Error updating record: " . mysqli_error($con);
}}
Btw thnx guys...input hidden made it work
<input type="hidden" name="fieldname" value="<?php echo $namefield?>" />
I am running while loop and fetch 3 records from database. and then update it on same page. Every record have submit button. But after edit when i submit the form it catchs the values of last record only and update other rows with the last record values. Please if somebody help me out i'll be very thankful. Remember it catches the exact (id) but the other parameters are only of last row.
<form method="post" action="">
<table width="700" border="1">
<tr><th><?php echo $_SESSION['teamtwo']; ?></th></tr>
<tr>
<th>Player Name</th>
<th>Runs</th>
<th>Edit</th>
<th>Save</th>
</tr>
<?php
$team = new DBConnection();
$condition = "WHERE teamname = '".$_SESSION['teamtwo']."' and datecreated = CURDATE()";
$sel_player = $team->SelectRecord(array("*"),"`match`","$condition");
//$sel_player = mysql_query("SELECT * FROM `match` WHERE teamname = '$team1' and datecreated = CURDATE()") or die(mysql_error());
while($get_player = mysql_fetch_array($sel_player))
{
$totalruns = $get_player['runs_bat'];
$totalballs = $get_player['ball_bat'];
#$strike = $totalruns / $totalballs * 100;
?>
<tr>
<td><input type="text" name="player_name" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
<td><input type="text" name="runs" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>
<td><button>Edit</button></td>
<td><input type="submit" value="Save" name="team" /></td>
</tr>
<?php
} ?>
</table>
</form>
<?php } ?>
</div>
</div>
</body>
</html>
<?php
if(isset($_POST['team'])){
$runs = $_POST['runs'];
$balls = $_POST['ball'];
$object = new DBConnection();
$arr_Field=array("runs_bat","ball_bat","player_status","how_out","opposite_bowl","opposite_player","sr","overs","bowl_ball","runs_ball","extra","madien");
$arr_Values=array("$runs","$balls","$status","$how_out","$opposite_bowler","$opposite_player","$sr","$over","$bowls","$score","$extra","$madien");
$condition = "WHERE id = '".$_REQUEST['player']."'";
//echo $_REQUEST['player'];
//echo $runs.$balls;
$object->UpdateRecord("`match`",$arr_Field,$arr_Values,"$condition") or die(mysql_error());
//header("Location:extra.php?update");
}
the problem is you are having one form and when you submit the form it will submit the last rows values because you are having same name for all 3 rows inside 1 form.
Solution:-
Create form element inside the while loop and close it inside the while loop itself . Like this you will have 3 forms each for 3 rows.
Code Example:-
while($get_player = mysql_fetch_array($sel_player))
{
$totalruns = $get_player['runs_bat'];
$totalballs = $get_player['ball_bat'];
#$strike = $totalruns / $totalballs * 100;
?>
<form>
<tr>
<td><input type="text" name="player_name" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
<td><input type="text" name="runs" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>
<td><button>Edit</button></td>
<td><input type="submit" value="Save" name="team" /></td>
</tr>
</form>
<?php
} ?>
1.
you need to make input array in while because name attribute is overwriting in loop
<td><input type="text" name="player_name[<?php echo $get_player['id']?>]" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
<td><input type="text" name="runs[<?php echo $get_player['id']?>]" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>
2.
you have all text boxes mean if press submit button of one row, then also you will get all textboxes as php side so make hidden variable in form to get which button clicked
//write javascript in your page
<script>
function setPlayerId(id) {
document.getElementById('playerid').value=id;
}
</script>
//take hidden field into form
<input type='hidden' name='playerid' value='0'>
//write down onlick button event
<input type="submit" value="Save" name="team" onClick="setPlayerId('<?php <?php echo $get_player['id']?>?>')"/>
3.
Now in php you will get that as below
echo $_POST['player_name'][$_POST['playerid']];
// same way you can do your insert or update.
this code must work
<form method="post" action="">
<table width="700" border="1">
<tr><th><?php echo $_SESSION['teamtwo']; ?></th></tr>
<tr>
<th>Player Name</th>
<th>Runs</th>
<th>Edit</th>
<th>Save</th>
</tr>
<?php
$team = new DBConnection();
$condition = "WHERE teamname = '".$_SESSION['teamtwo']."' and datecreated = CURDATE()";
$sel_player = $team->SelectRecord(array("*"),"`match`","$condition");
//$sel_player = mysql_query("SELECT * FROM `match` WHERE teamname = '$team1' and datecreated = CURDATE()") or die(mysql_error());
while($get_player = mysql_fetch_array($sel_player))
{
$totalruns = $get_player['runs_bat'];
$totalballs = $get_player['ball_bat'];
#$strike = $totalruns / $totalballs * 100;
?>
<tr>
<td><input type="text" name="player_name" value="<?php echo $get_player['player_name']; ?>" disabled="disabled" /></td>
<td><input type="text" name="runs<?=$get_player['id']?>" value="<?php echo $get_player['runs_bat']; ?>" size="1" /></td>
// you didnt write this i added
<input type="text" name="ball<?=$get_player['id']?>" value="<?php echo $get_player['ball_bat']; ?>" size="1" />
<td><button>Edit</button></td>
<td><input type="submit" value="Save" name="team" /></td>
</tr>
<?php
} ?>
</table>
</form>
<?php } ?>
</div>
</div>
</body>
</html>
<?php
if(isset($_POST['team'])){
$runsname = 'runs'.$_GET['player'];
$ballsname = 'ball'.$_GET['player'];
$runs = $_POST[$runsname];
$balls = $_POST[$ballsname];
$object = new DBConnection();
$arr_Field=array("runs_bat","ball_bat","player_status","how_out","opposite_bowl","opposite_player","sr","overs","bowl_ball","runs_ball","extra","madien");
$arr_Values=array("$runs","$balls","$status","$how_out","$opposite_bowler","$opposite_player","$sr","$over","$bowls","$score","$extra","$madien");
$condition = "WHERE id = '".$_REQUEST['player']."'";
//echo $_REQUEST['player'];
//echo $runs.$balls;
$object->UpdateRecord("`match`",$arr_Field,$arr_Values,"$condition") or die(mysql_error());
//header("Location:extra.php?update");
}
I get the values from same row, but many values with fetch array. I did it, but with buttons, as many as how many values there are. If I have 5 rows, then I should have five submits, but I want one submit button.
Here is my code:
$result2 = mysql_query ("select * from price where dom='$cat'",$db);
$myrow2= mysql_fetch_array($result2);
<form action="priceupdatetes.php" method="post">
<?php
do {
echo <<<here
<td><input name="etiket[$myrow2[id]]" type="text" value="$myrow2[etiket]"/></td>
<td><input name="pricestandart[$myrow2[id]]" type="text" value="$myrow2[pricestandart]"/></td>
<td><input name="number[$myrow2[id]]" type="text" value="$myrow2[number]"/></td>
<td><input name="totalunper[$myrow2[id]]" type="text" value="$myrow2[totalunper]" disabled="disabled"/></td>
<td><input name="discount[$myrow2[id]]" type="text" value="$myrow2[discount]"/></td>
<td><input name="totalwithper[$myrow2[id]]" type="text" value="$myrow2[totalwithper]" disabled="disabled"/></td>
</tr>
here;
}
while($myrow2= mysql_fetch_array($result2)) ;
?>
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
<input name="submit" type="submit" value="Submit"/><br>
</form>
HERE is UPDATEPAGE.php:
if (isset($_POST['etiket'])) {$etiket = $_POST['etiket']; }
if (isset($_POST['pricestandart'])) {$pricestandart = $_POST['pricestandart'];}
if (isset($_POST['number'])) {$number = $_POST['number']; }
if (isset($_POST['discount'])) {$discount = $_POST['discount']; }
if (isset($_POST['id'])) {$id = $_POST['id']; }
$totalunper=$pricestandart*$number;
$percent=$discount/100;
$totalwithper1=$totalunper*$percent;
$totalwithper=$totalunper-$totalwithper1;
foreach($id as $team_id)
{
$result = mysql_query("UPDATE price SET etiket='$etiket[$team_id]',pricestandart='$pricestandart[$team_id]',number='$number[$team_id]',totalunper='$totalunper[$team_id]',discount='$discount[$team_id]',totalwithper='$totalwithper[$team_id]' WHERE id='$team_id'"); }
How can I get values with different id's and update them?
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
is wrong. $myrow2[id] is not an array. Inside your while-loop, you should add:
<input NAME="id[]" TYPE="hidden" value="$myrow2[id]"/>