I have problem to get to the checked value in checkbox.
I have first query that is taking names of the tables in database and second query that is taking names of the columns of that tables. Names of the columns are displayed in a checkbox, but i have problem to take that values from the checkbox. I know I should use $_POST['kolona'] but somehow that variable its not recognized:
if(mysqli_connect_errno())
{
echo "Error: Greška u konekciji";
exit;
}
$sql1 = "SHOW TABLES FROM db_baza";
$result1 = mysql_query($sql1);
if (!$result1)
{
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row1 = mysql_fetch_row($result1))
{
echo "<div id='blok' style='width:200px;height:200px;background-color:red;margin:20px;float:left;'>{$row1[0]}"."</br>";
$tablename=$row1[0];
$sql2="SELECT column_name FROM information_schema.columns WHERE table_name = '{$tablename}'";
$result2= mysql_query($sql2);
while($row2=mysql_fetch_row($result2))
echo "<form id='forma1' action='' method='POST'><input type='checkbox' name='kolona[]' value='{$row2[0]}' />{$row2[0]}</br>";
echo "<input type='submit' name='submit' value='{$tablename}'/></form>";
echo "</div>";
}
if(isset($_POST['submit']))
if(isset($_POST['kolona'])
echo 1;
?>
So, I am just trying to echo number one, i will need it for other stuff but just for the example.
Modify these lines:
echo "<form id='forma1' action='' method='POST'>";
while($row2=mysql_fetch_row($result2)) {
echo "<input type='checkbox' name='kolona[]' value='{$row2[0]}' />{$row2[0]}</br>";
}
echo "<input type='submit' name='submit' value='{$tablename}'/></form>";
echo "</div>";
Related
Wanted to update status from "selected" value using listview(data is from database)
Below is the code i tried
<input type='checkbox' name='userIDs[]' value='{$row['userID']}'>
if(isset($_POST['updatestatus'])){
$query = "UPDATE userdatabase SET userStatus='Couple' WHERE userID= '".$_POST['userIDs']."'";
$db->query($query);
}
When i clicked on "updatestatus" , it will update all the selected id "userStatus" column in the database
In your HTML code, you make that array, Then you try to run a query with an array.
If you don't need to work with array try this:
<?php
echo "<form action='' method='POST'>";
echo "<input type='checkbox' name='userIDs' value='{$row['userID']}'>";
echo "<input type='submit' name='updatestatus' value='Update'>";
echo "</form>";
if(isset($_POST['updatestatus'])){
$query = "UPDATE userdatabase SET userStatus='Couple' WHERE userID= '".$_POST['userIDs']."'";
$db->query($query);
}
?>
If want work with array try this :
<?php
echo "<form action='' method='POST'>";
echo "<input type='checkbox' name='userIDs' value='{$row['userID']}'>";
// OTHER UserIDs
echo "</form>";
if(isset($_POST['updatestatus'])){
foreach($_POST['userIDs'] as $userID)
{
$query = "UPDATE userdatabase SET userStatus='Couple' WHERE userID= '".$userID."'";
$db->query($query);
}
}
?>
I am trying to update a php form that holds a few rows of mysql data. I have a button next to each row and when i click on that I want to update the row. The issue im having below is the ID is only set as the last row. How do i get this to push the ID to the button? So basically no matter what button i press i always get the same ID which is the last one to load.
if($result){
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$beername = $row["BeerName"];
$beertype = $row["BeerType"];
$beerpercent = $row["BeerPercent"];
$beerdescription = $row["BeerDescription"];
$nowpouring = $row["NowPouring"] =='0' ? '' : 'checked=\"checked\"';
$glutenreduced = $row["GlutenReduced"] =='0' ? '' : 'checked=\"checked\"';
$beertogo = $row["BeerToGo"] =='0' ? '' : 'checked=\"checked\"';
echo "<form action='' method='POST'>";
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id\" value=\"$id\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerName\" value=\"$beername\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerType\" value=\"$beertype\"></h6></td>";
echo "<td><h6><textarea size=\"90\" style=\"width:250px;height:150px;\" name=\"BeerDescription\" value=\"\">$beerdescription</textarea></h6></td>";
echo "<td><h6><input type=\"text\" size=\"5\" name=\"Percent\" value=\"$beerpercent\"></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"NowPouring\" value=\"true\" $nowpouring></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"GlutenReduced\" value=\"true\" $glutenreduced></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"BeerToGo\" value=\"true\" $beertogo></h6></td>";
#echo "<td><h6> <a href=\". $_SERVER["PHP_SELF"] .?id=".mysql_result($result,$j,'id')."\" onclick=\"\"></h6></td>";
echo "<td><h6> <button name=\"submit\" type=\"submit\" value=\"$id\">Save</button></h6></td>";
echo "</tr>";
echo "</form>";
}
}
if (isset($_POST['submit'])) {
$user = $_POST['submit'];
echo "<p style=\"color:#ffffff\">$id</p>";
#$delet_query = mysqli_query($mysqli, "UPDATE NowPouring SET NowPouring = '1' WHERE ID = '4'") or die(mysql_error());
if ($delet_query) {
echo '<p style="color:#ffffff">Beer with id '.$id.' is updated. To refresh your page, click ' . ' <a href=' . $_SERVER["PHP_SELF"] . ' > here </a></p>';
}
}
?>
The main problem I see here is that the while loop your code has is generating the same name for the inputs...
All of your "<button name=\"submit\" type=\"submit\" value=\"$id\">Save</button>" will have the same name, that's why it always has the last id as value.
Maybe you should try something such as..
<button name=\"$id_submit\" type=\"submit\" value=\"$id\">Save</button>
or if you want you can store it in an array..
<button name=\"submit[]\" type=\"submit\" value=\"$id\">Save</button>
You are seeing this result because the 'name' of each of your inputs is the same, so essentially you have a form with a bunch of elements that have the same names. You need to add a dynamic aspect to each name.
For example, you could update your output to something like this:
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id_$id\" value=\"$id\"></h6></td>";
Where each line adds the current id. Then when you retrieve the form data, you can append the submitted id to the field you want to update.
Have you considered using an AJAX approach so you can submit just the line in question and not have to reload the page and return the whole data set each time?
Make <form> for each submit button. Adding <form> in the while():
if($result){
while($row = mysqli_fetch_array($result)){
echo "<form action='' method='POST'>";
//...
echo "</form>";
}
}
Your form tag is placed at the wrong place.
It should be within:
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
//....
//....
echo "<form action='' method='POST'>";
echo"<tr>";
echo "<td>" . $id . "</td>";
//....
//....
echo "<td><button type='submit' name='submit'>Save</button></td>";
echo"</tr>";
echo "</form>";
}
<?php
$connection = #mysql_connect ("localhost","root","");
#mysql_select_db ("streamurl" , $connection) or die ('Error in DB Connection');
$select_all= "SELECT * FROM url";
$array = mysql_query($select_all, $connection);
if(isset($_POST['btn'])){
$mysql_query_rename = "UPDATE url SET ST_NAME='$_POST[name]', URL='$_POST[url]', ID='$_POST[id]' WHERE ID='$_POST[hidden2]' AND ST_NAME='$_POST[hidden3]' AND URL='$_POST[hidden1]' ";
if(mysql_query($mysql_query_rename, $connection)){
echo 'Changes were applied Successfully';
} else {
echo 'Changes Could not be Applied';
};
};
while ($show = mysql_fetch_array($array)){
echo "<form action=deletestation.php method=post>";
echo "<input type=text name=id value=$show[ID] >";
echo "<input type=text name=name value=$show[ST_NAME] >";
echo "<input type=text name=url value=$show[URL] >";
echo "<input name=hidden1 type=hidden value=$show[URL] >";
echo "<input name=hidden2 type=hidden value=$show[ID] >";
echo "<input name=hidden3 type=hidden value=$show[ST_NAME] >";
echo "<input name=btn type=submit value=UPDATE>";
echo "</form>";
};
mysql_close($connection);
?>
First of all im new to PHP and MYSQL
the above code is used to update a table in mysql database and i have echoed out the results in text boxes . there are only few information in the table ( Station name , ID and URL ). the problem is only the 1st result is updateable and the other results can not be updated . Except 1st result the other wont display the full Station name (ST_NAME) .
Kindly point out the the Error in my code.
<?php
$connection = mysql_connect ("localhost","Sithira","1sithiraM");
mysql_select_db ("streamurl" , $connection) or die ('Error in DB Connection');
$select_all= "SELECT * FROM url";
$array = mysql_query($select_all, $connection);
if(isset($_POST['btn'])){
$mysql_query_rename = "UPDATE url SET ST_NAME='$_POST[name]', URL='$_POST[url]', ID='$_POST[id]' WHERE ID='$_POST[hidden]' ";
if(mysql_query($mysql_query_rename, $connection)){
echo 'Changes were applied succesfully';
} else {
echo 'Changes Could not be Applied';
};
};
while ($show = mysql_fetch_array($array)){
echo "<form action=deletestation.php method=post >";
echo "<input type=text name=id value=$show[ID] >";
echo "<input type=text name=name value=$show[ST_NAME] >";
echo "<input type=text name=url value=$show[URL] >";
echo "<input name=hidden type=hidden value=$show[ID] >";
echo "<input name=btn type=submit value=UPDATE>";
echo "</form>";
};
mysql_close($connection);
?>
finally i got it working :)
Im a newbie and working on a project for school
I have a website that lists foods.
I have an update table that allows me to change and add data.
For the food group field I have it cross reference another table called food_group which has the food_group(name) and an id.
When you view the food data you can see the name that it pulls instead of the ID. On the update page I would like a drop down to be in the place of the ID. So you can see the "friendly" name instead of the ID number, but it has to store the ID not the friendly name in the food table.
Website can be found at http://web.nmsu.edu/~jrortiz/ICT458/FINAL/
The code I have is:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost","user","pw","db");
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]', ph='$_POST[PH]' WHERE food='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM food";
$myData = mysqli_query($con,$sql);
echo "<table border=1>
<tr>
<th>Food Group</th>
<th>Food</th>
<th>PH</th>
<th>Update/Add</th>
<th>Delete</th>
</tr>";
while($record = mysqli_fetch_array($myData)){
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='Food_group' value='$record[food_group]'/></td>";
echo "<td><input type='text' name='Food' value='$record[food]'/></td>";
echo "<td><input type='text' name='PH' value='$record[ph]'/></td>";
echo "<td><input type='submit' name='update' value='update'/></td>";
echo "<td><input type='submit' name='delete' value='delete'/></td>";
echo "<td><input type='hidden' name='hidden' value='$record[food]'/></td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='addGroup'></td>";
echo "<td><input type='text' name='addFood'></td>";
echo "<td><input type='text' name='addPH'></td>";
echo "<td><input type='submit' name='add' value='add'/></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
____________ Update 12/2/13 10:30pm ___________________
Ok so if I create a new php page like the following it will work. However, I have no idea how to combine it into the original above... Can anyone help?
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$con = mysql_connect("localhost","user","pw");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql2="SELECT id, food_group FROM food_group";
$result = mysql_query($sql2,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$type=$row["food_group"];
$options.= '<option value="'.$row['id'].'">'.$row['food_group'].'</option>';
};?>
<SELECT NAME=Food_group>
<OPTION VALUE=0>Choose</OPTION>
<?php echo $options; ?>
</SELECT>
</body>
</html>
Thank you for all your help!
Jason
Your script is nice but I just want to point the following:
There's no need to concatenate this
"<td>" . "<input type=text name=Food_group value=" . $record['food_group'] . " </td>";
you can type it like this:
echo "<td><input type=text name=Food_group value='$record[food_group]'</td>";
also you missed to close your input tag
echo "<td><input type=text name=Food_group value='$record[food_group]' /></td>";
and another is you need to quote your attribute values , see below
echo "<td><input type='text' name='Food_group' value='$record[food_group]'</td>";
Last thing is that you're open to SQL injection, so you should start learning mysqli and prepared statement
I'm having bad time to update multiple tables from a form. I've done the query and checked for errors using (or die), but it seems that there are no errors in my MySQL codes. I can update the main table's data, but I can't update the other table. I'm suspecting that my form fields have some problem. This is my form codes :
<?php
$sql= "SELECT * FROM client WHERE resID=".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$sqlM= "SELECT * FROM menu WHERE resID=".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Edit</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='client_admin_post.php'>\n";
$f=mysql_fetch_array($rs);echo '<tr>';
echo "<td>Res ID :</td>";
echo "<td>{$f['resID']}<input type='hidden' name='resID' value='{$f['resID']}' /></td>";
echo '</tr>';
++$i;
echo '<tr>';
echo "<td>Restaurant Name :</td>";
echo "<td><input type='text' size='40' name='resName' value='{$f['resName']}' /></td>";
echo '</tr>';
++$i;
while ($fM = mysql_fetch_array($rsM)) {
echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname[$i]' value='{$fM['name']}' /></td>";
echo "<td>{$fM['id']}<input type='hidden' name='mid[$i]' value='{$fM['id']}' /></td>";
echo '</tr>';
++$i;
}
echo '<tr>';
echo "<td><input type='submit' value='submit' /></td>";
echo '</tr>';
echo "</form>";
echo '</table>';
?>
This is my POST codes :
<?php
//session_start();
include_once("connection.php");
$resID= $_POST["resID"];
$resName= $_POST["resName"];
$sql = "UPDATE client ".
"SET resName = '$resName' ".
"WHERE resID = '$resID' " ;
mysql_query($sql) or die ('query failed:' . mysql_error());
$size = count($_POST['mname']);
$i = 0;
while ($i < $size) {
$name= $_POST['mname'][$i];
$id = $_POST['mid'][$i];
$sqlM = "UPDATE menu SET name = '$name' WHERE id = '$id' LIMIT 1";
mysql_query($sqlM) or die ("Error in query: $sqlM");
echo "$name<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
?>
As you guys know, one restaurant got many menus. I can edit the main restaurant info, but I can't edit the menus. Please help me. Really appreciate your help. Thanks :D
You ++$i at least twice before outputing it as $mname index. So $_POST['mname'] would have indices 2 .. count_of_rows+1. Your while() ignores this fact and counts 0 .. size.
If you had all warnings turned on (as you should) you would get a warning indicating that invalid index 0 is used on line starting "$name= $_POST..." and you would have been able to figure out from there.