I have a table with more columns. On one column I have 3 buttons for each row with different queries associated. When I click one of the buttons, field in the table from my database should update.The problem is that when I click on a button on any row in the table, it updates only the field from the first row.For example, I have a table with 10 rows.If I click one of the buttons on the 10th row IT will update my first row, not the 10th one as I want. Is there any possibility to solve this?
This is the code..I'm sorry it is too long:
<?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
$result = mysqli_query($link ,$query);
$row = mysqli_fetch_assoc($result);
if(isset($_POST['locul1']))
{
$sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
}
else if(isset($_POST['locul2']))
{
$sql = "UPDATE masculin SET Premiu=2 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
}
else if(isset($_POST['locul3']))
{
$sql = "UPDATE masculin SET Premiu=3 WHERE CNP='".$row['CNP']."'";
mysqli_query($link,$sql);
header("Location:administrare.php");
} ?>
<u><i><h1 align="center">Administrare concurenti</h1></i></u>
<u><i><h2>MASCULIN</h2></i></u>
<?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
$result = mysqli_query($link ,$query);
if (mysqli_num_rows($result) == 0) {
echo 'Inca nu s-a inscris niciun concurent.';
} else {
?>
<table width="100%">
<tr>
<th>Nr.<br />concurs</th>
<th>Nume</th>
<th>Prenume</th>
<th>CNP</th>
<th>Categoria</th>
<th>Varsta</th>
<th>Premiu</th>
<th>Modifica<br />rezultat</th>
<th>Descalifica</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)){ ?>
<tr>
<?php
$query1="SELECT id_concurent FROM concurenti WHERE CNP='".$row['CNP']."'";
$result1=mysqli_query($link,$query1);
$nr_conc=mysqli_fetch_assoc($result1);
?>
<td> <?php echo $nr_conc['id_concurent'] ?> </td>
<td> <?php echo $row['Nume'] ?> </td>
<td> <?php echo $row['Prenume'] ?> </td>
<td> <?php echo $row['CNP'] ?> </td>
<td> <?php echo $row['Categorie'] ?> </td>
<td> <?php echo $row['Varsta'] ?> </td>
<td> <?php echo $row['Premiu'] ?> </td>
<td>
<form action="administrare.php" method="post">
<input type="submit" name="locul1" value="Premiul 1">
<input type="submit" name="locul2" value="Premiul 2">
<input type="submit" name="locul3" value="Premiul 3">
</form>
</td>
</tr>
<?php } ?>
</table> <?php } ?>
for updated the da tbale row when you submit the values related to a html table row you should add and hidden input with the value that let you reach the correct row eg:
<form action="administrare.php" method="post">
<input type="submit" name="locul1" value="Premiul 1">
<input type="submit" name="locul2" value="Premiul 2">
<input type="submit" name="locul3" value="Premiul 3">
<input type="hidden" name="CNP" value="<?php echo $row['CNP'] ?>">
</form>
and in your sql updated query add the POST value related to the row
$sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$_POST['CNP']. "'";
Related
code:
<script>
$(document).ready(function(){
$(".menu").click(function(){
ids = $('.menu:checked').map(function() {
return this.id;
}).get().join(',');
console.log(ids);
$("#ids").val(ids);
});
});
</script>
<?php
if(isset($_POST['submit']))
{
$adminid = $_POST['admin'];
$menuids = explode(",", $_POST['ids']);
foreach ($menuids as $idd)
{
$sql = "update menu set admin_id = concat(admin_id,'$adminid',',') where id = '$idd'";
$result = mysqli_query($link,$sql);
}
if($result == true)
{
$msg .= "<p style='color:green'>successfull</p>";
}
else
{
$msg .= "<p style='color:red'>error!</p>";
}
}
?>
<form method="post">
<select name="admin" id="admin">
<option value="">---Select Admin---</option>
<?php
$sql = "select * from admin";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['firstname']?></option>
<?php
}
?>
</select>
<table>
<tr>
<th>Share</th>
<th>Menu Name</th>
</tr>
<?php
$query = "select * from menu";
$results = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" id="<?php echo $fetch['id']; ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
</table>
<input type="text" name="ids" id="ids" value=""/>
<input type="submit" name="submit" id="submit" />
</form>
In this code I am update a table having name menu in database. Now, I want to check only those checkbox where admin_id like ,1, or ,2, which is update by query. How can I fix this issue ?please please help.
Thank You
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" value="<?php if($fetch['id']==1 or
$fetch['id']==2 ) { echo "checked";} else{} ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
I am trying to create a page in which a user selects a category (the button below represents one category), and a list of posts in that category are displayed, via the while loop, however when the button is clicked nothing is happening and I can't figure out why..
<div id = "cat_container">
$sql = "SELECT * FROM `categories`";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
?>
<table valign="top">
<tr>
<td>
<form method="get">
<input type="button" name="button" value="<?php echo $row["cat_name"];?>"></button>
</form>
</td>
</tr>
</table>
<?php
}
?>
</div>
<div id="post_container" >
<?php
if(isset($_GET['button'])) {
$category = $_GET['button'];
$sql = "SELECT * FROM `posts` WHERE `post_cat` = '$category'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td>
<?php echo $row["post_content"];?>
</td>
</tr>
</table>
<?php
}
}
?>
</div>
Can anyone help me with this?
Looks like the form is not being submitted because of "button" input type. Try to change it to "submit" and remove redundant </button> tag after it.
<input type="submit" name="button" value="<?php echo $row["cat_name"];?>">
I have to make everything happened on the same page. I have used action="<?PHP echo $_SERVER['PHP_SELF']; ?>" here but it is not working. I have insert the PHP query below the form. Basically, my question is how do I make sure the form is posting the values on the same page. If it is updated, a pop up will come up.
$user_id=$_SESSION['user_id'];
$date = date("l jS \of F Y h:i:s A");
$query1 ="SELECT daily_limit FROM user WHERE user_id='$user_id'";
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));
while ($row1 = mysqli_fetch_array($result1)) {
$dailylimit=$row1['daily_limit'];
}
$query2 = "SELECT SUM(debit) AS debited_today FROM transaction WHERE user_id = '$user_id' AND date = CURRENT_DATE" ;
$result2 = mysqli_query($link, $query2) or die (msqli_error($link));
while ($row2 = mysqli_fetch_array($result2)){
$debited_today = $row2['debited_today'];
}
// form
<form method="POST" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" >
<table id="table">
<tr>
<td class="alt">Existing Daily Limit</td>
<td>S$ <?php echo $dailylimit; ?> </td>
<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
</tr>
<tr>
<td class="alt"><label for="newdailylimit">New Daily Limit</label></td>
<td>$ <select name="newdailylimit">
<option value="100.00">100.00</option>
<option value="500.00">500.00</option>
<option value="1000.00">1000.00</option>
<option value=5000.00">5000.00</option>
</select></td>
</tr>
<tr>
<td class="alt">Amount Debited Today</td>
<td>S$ <?php echo $debited_today; ?></td>
</tr>
<tr>
<td class="alt">Amount Debited Left</td>
<td>S$ <?php echo ($dailylimit - $debited_today); ?> </td>
</tr>
</table>
<br/>
<input type="submit" name="submit "value="Submit"></input>
</form>
// Values I need to POST
$dailylimit = $_POST['dailylimit'];
$newdailylimit = $_POST['newdailylimit'];
if ($dailylimit != $newdailylimit){
$query = "UPDATE user SET daily_limit='$newdailylimit' WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
echo "<script>alert('You have successfully updated your daily limit');</script>";
}
else if ($dailylimit == $newdailylimit){
echo "<script>alert('You have selected the same daily limit as your previous one. Please choose a different one. ');</script>";
}
else{
}
Ahh I may have found out what the problem is:
You have a space after the word submit and no space before value.
This will prevent your form from submitting.
<input type="submit" name="submit "value="Submit"></input>
---^ ^
Change this to:
<input type="submit" name="submit" value="Submit">
The double quote might mess things up.
Other things that are wrong but won't fix your problem
Also
<option value=5000.00">5000.00</option>
should be
<option value="5000.00">5000.00</option>
Also
<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
// You have an extra space here ^
Which will change your $dailylimit, and append it with a space.
make your action=''. it will post to itself.
then on top of your page check if the request is post ex. if($_POST){//add your code}else{//yourform}
I have a table where each row in the table has a button that allows you to delete that particular row from the database. Although somehow my form only submits the last row and no the one I selected. Please ignore the deprecated tags, well aware of the issues.
<?php
$result = mysql_query("SELECT * FROM booking");
while($row = mysql_fetch_array($result))
{
?>
<form class='table-form' id='form' method='post'>
<tr>
<input id="bookid" type="hidden" name="bookid" value="<?php echo ($row['booking_id']); ?>" />
<td>
<?php echo ($row['booking_id']);?>
</td>
<td>
<?php echo ($row['user_id']); ?>
</td>
<td>
<?php echo ($row['event_id']); ?>
</td>
<td>
<?php echo ($row['payment_type']); ?>
</td>
<td>
<?php echo ($row['booking_date']); ?>
</td>
<center><button type="submit" id="submit" name="submit">Cancel</button><center>
<td>
</td>
</tr>
<?php
}
?>
</form>
</table>
</div>
<?php
if (isset($_POST['bookid'])){
$id = ($_POST['bookid']);
$result = mysql_query("DELETE FROM booking
WHERE booking_id = '$id'");
}
?>
If you preffer programing this way, move </form> above
<?php
}
?>
(to cycle body).
Your code needs a lot of clean up, style cleaning and html validation. :-P
i edited my code as below but the issue is that each time i click on the edit link, all of the products are being displayed instead of only the one beside which i clicked the edit link.
note: Sorry for posting another question relating to my other one. I could not add any more comments.
<?php
include_once("db_connect.php");
if(isset($_POST['update']))
{
$prod_id = $_POST['prod_id'];
$prod_name=$_POST['prod_name'];
$prod_brand=$_POST['prod_brand'];
$prod_price=$_POST['prod_price'];
// checking empty field
if(empty($prod_price))
{
//if name field is empty
if(empty($prod_price))
{
echo "<font color='red'>Price field is empty.</font><br/>";
}
}
else
{
//updating the table
//$result=mysql_query("UPDATE tblretprod SET prod_price='$prod_price' WHERE prod_id=$prod_id");
$result=mysql_query("UPDATE tblretprod SET prod_price='".$prod_price."' WHERE prod_id='".$prod_id."';");
//redirectig to the display page. In our case, it is index.php
header("Location: update.php");
}
}
?>
<?php
$prod_id = $_GET['prod_id'];
$result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id") or die(mysql_error());
?>
<html>
<title>Edit Product</title>
<body>
Home
<br/><br/>
<form name="edit" method="post" action="updprod.php">
<table border="0">
<?php
while($res=mysql_fetch_array($result))
{
$prod_name = $res['prod_name'];
$prod_brand = $res['prod_brand'];
$prod_price = $res['prod_price'];
?>
<tr>
<td>Product Name</td>
<td>
<input type="text" disabled="disabled" name="prod_name" value = "<?php echo $prod_name;?>"> </td>
</tr>
<tr>
<td>Brand</td>
<td>
<input type="text" disabled="disabled" name="prod_brand" value = "<?php echo $prod_brand;?>"> </td>
</tr>
<tr>
<td>Product Price</td>
<td>
<input type="text" name="prod_price" value = "<?php echo $prod_price;?>">
<input type="hidden" name="prod_id" value = "<?php echo $_GET['prod_id'];?>">
</td>
</tr>
<?php } ?>
<tr>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
Add in your select query in WHERE clause:
AND a.prod_id = ".$prod_id."
query:
"SELECT
a.prod_name,
a.prod_brand,
b.prod_price
FROM
tblproduct a, tblretprod b
where
a.prod_id = b.prod_id
AND a.prod_id = ".intval($prod_id).""
To make the query safer against SQL Injection i've added intval function like Kickstart well pointed out.