How to post array value fron input name - php

i want to pass an array value from input name that generated from an array. i can view $_POST if i manually define on script.
$stmt = $db->prepare($sql);
$stmt->execute();
$i = 0;
while ($row = $stmt->fetch())
{
$id=strtoupper($row["id"]);
$nama=strtoupper($row["nama"]);
$sn=strtoupper($row["sn"]);
$kewpa=strtoupper($row["kewpa"]);?>
<form method="post" action="kewpaupdate.php"><tr>
<td><input type="text" name="id[<?php echo $i;?>]" value=<?php echo $id;?>></td>
<td></td>
<td></td>
<td><input type="text" name="kewpa[<?php echo $i;?>]" value=<?php echo $kewpa;?>></td>
<td align="center"><input type="submit" value="submit"></td>
</tr></form>
<?php ++$i;
?>
</tbody>
</table>
<?php
$w = $_POST['id'];
$r = $_POST['kewpa'];
echo $w;
echo "<br>";
echo $r;
?>
how to display array value that choose from submit button.

You need to do 2 things:
1) As you are printing the $_POSTed data on the page itself, you need to submit the form to same page.
Please change <form> action to blank.
Changed form action code:
<form method="post" action="">
This will submit the form to same page.
2) As your input has name of type array, you can't just print it with echo or print().
These are used for printing strings.
You need to use print_r().
So, please use:
echo '<pre>';
print_r($_POST['id']);
echo '<pre>';
You will get an array, same for the other field.

Related

Add a delete button to a table populated from a database

I have a table which I populate from a mysql db. I want to add a delete button to each of the rows in the table, and when the button is clicked, I want to remove that line from the db table. I am using an array to update any changes made to the table. How can I use that array to delete a specific row too?
<table>
<tr><th>Category ID</th><th>Description</th><th>Valid</th><th></th></tr>
<?php
$query=mysqli_query($link,"SELECT * FROM cd_categories");
while($row = mysqli_fetch_array($query)){
$catid = $row['Catg_Id'];
$des = $row['Description'];
$datep = $row['Date_Posted'];
$postedb = $row['Posted_By'];
$valid = $row['Valid_YN'];
?>
<tr><td><input type="text" name="data[<?php echo $catid; ?>][catid]" value="<?php echo $catid; ?>" ></td>
<td><input type="text" name="data[<?php echo $catid; ?>][des]" value="<?php echo $des; ?>" ></td>
<td><input type="button" name="data[<?php echo $catid; ?>][delete]" value="Delete" ></td>
</tr>
<?php } ?>
</table>
<br>
<input type="submit" name="update" value="Save Changes" >
To remove a row from database you need to use a DELETE statement with a primary key, which you need to pass from this while loop.
Make a link inside while loop: [Demo]
<a href='delete.php?id=your_id'>Delete</a>
Now in your delete page, you need to capture or store the id using $_GET and using the DELETE Statement you can simply delete row from database.
DELETE FROM table_name WHERE primary_key=your_get_value;
Note: In your delete page just make a query for delete the row also
make some security.
Try this approach:
<table>
<tr><th>Category ID</th><th>Description</th><th>Valid</th><th></th></tr>
<?php
$query=mysqli_query($link,"SELECT * FROM cd_categories");
while($row = mysqli_fetch_array($query)){
$catid = $row['Catg_Id'];
$des = $row['Description'];
$datep = $row['Date_Posted'];
$postedb = $row['Posted_By'];
$valid = $row['Valid_YN'];
?>
<tr><td><input type="text" name="catid_<?php echo $catid; ?>" value="<?php echo $catid; ?>" ></td>
<td><input type="text" name="desc_<?php echo $catid; ?>" value="<?php echo $des; ?>" ></td>
<td>
Edit |
Delete</td>
</tr>
<?php } ?>
</table>
<br>
in delete.php:
delete from table where cat_id= $_GET["id"];
in edit.php
$desc =
update table set desc=$_GET["desc_".$_GET["id"]], catid = $_GET["catid_".$_GET["id"]] where cat_id= $_GET["id"];
Basically, to do this without javascript, you need to have a separate form for each row of your HTML table (which displays one row from your db). Add a hidden input field in your form which contains the unique identifier for that particular row, and a submit button to delete the row. Leave the form action field blank so the same page receives the submitted form data, then have PHP test for which button was submitted and if it was the delete button, delete the data and re-display the table.
Example HTML code:
<table>
<tr><td>
<form action="" method="post">
<input type="hidden" name="row_id" value="<?php echo 'identifier here'; ?>">
<?php echo 'stuff here'; ?>
<input type="submit" name="submit" value="Save Changes">
<input type="submit" name="submit" value="Delete">
</form>
</td></tr>
</table>
Example PHP code:
if (isset($_POST['submit')) // Form submitted
{
if ($_POST['submit'] == 'Delete') // Delete button clicked
{
// Run delete query based on the hidden field containing the row identifier
}
elseif ($_POST['submit'] == 'Save Changes')
{
// Run update query
}
}

While loop $_post from form

<?php
include("dbFunctions.php");
$query ="SELECT * FROM `physical_examination` WHERE `PE_Opt_ans`= 0";//select form options name
$result = mysqli_query($link,$query);
?>
<div id="tabs-3">
<table>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="tab0">
<?php while ($arrayResult = mysqli_fetch_array($result)){ ?>
<tr>
<td><label for="input"><b><?php echo $arrayResult['PE_Opt_name']?></label></td>
<?php if ($arrayResult['PE_Opt_type'] == "textarea") { ?>
<td><textarea rows="8" cols="45"name = "other1"></textarea></td>
<?php } else { ?>
<td><input type="<?php echo $arrayResult['PE_Opt_type']?>" name="input<?php echo $arrayResult['id']?> " ></td>
</tr>
<?php } ?>
<?php } ?>
<br> <input value="Submit" type="submit" name="submit1">
</form>
</table>
<?php
include "dbFunctions.php";
if(isset($_POST['submit1'])) {
$number = $_POST['other1'];
I am stuck here.
How do I $_POST the form based on the while loop after I click on submit button? Do I need another while loop again for the name value of input <?php echo $arrayResult['id']?>
I didn't quiet get what you want but i assume you want the contents of the textarea generated by the while loop.
Change in your loop the names from input to input[] and other1 to other1[]
If you var_dump($_POST) afterwards you should see that they are now an array which you can loop over with foreach
PS: You should NEVER EVER eat raw $_POST/$_GET inputs and PHP_SELF is another security breach. But that's just my two cents

html dynamic form, pass multiple values through post and insert mysql

i have this database
and this form, dynamically generated from the database
<table border="1" cellspacing="0" cellpadding="6">
<tr bgcolor="#CCCCCC">
<td><strong>product id</strong></td>
<td><strong>product name</strong></td>
<td><strong>product price</strong></td>
<td><strong>quantity</strong></td>
</tr>
<form method="post" action="insert.php">
<?php
$query = $dbh->query('SELECT * FROM products');
$results = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row)
{
?>
<tr>
<td><?php echo $row['product_id']; ?></td>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_price']; ?></td>
<td><input name="quantity" type="text" value="0"></td>
</tr>
</form>
<?php } ?>
</table>
<br>
<input type="submit" value="Add Records">
the quantity in the form is a textbox so i can modify it.
I would like to enter the quantities and the pressing the button to insert the values in the order_products table (including the quantity).
1) How can i pass ALL the quantities and product_id (and the rest) to the next page through post? (until now i know how to pass single values)
2) is there a better way to achieve it?
3) the insert statements should be in the same page or the page where i get the post vars?
db scheme
http://i.stack.imgur.com/oqdOy.jpg
thanks
Rob
1) First, you have to wrap your <input>s inside a single form (move your </form> tag after your <input type="submit" value="Add Records">, the way you have it now closes the <form> tag at first iteration) and submit it via HTTP POST method. Then, based on your schema, the only field you'll have to insert aside of quantity is product_id, which value you can assign inside a hidden field, like:
<?php
echo "<input type='hidden' name='pid_$row['product_id']' value='$row['product_id']'>";
echo $row['product_id'];
?>
Notice that you can still echo the value itself for viewing purposes. You also have to generate your quantity <input> field name property dynamically, otherwise $_POST will overwrite values when their keys are the same.
<?php
echo "<input type='text' name='pid-qtd_$row['product_id']'>";
?>
2) It depends on your development priorities. There are some frameworks out there that might simplify your process. I'd recommend you to keep all your DB queries and connection data within a DB helper class and require it wherever you need it.
3) Since you're using PDO, I assume you have an OOP design, which implies in doing that at your DB helper class or such. The page receiving the HTTP request must require your helper and deal with the $_POST parsing to parameters to its query methods. Don't forget to prepare your statements and parameterizing your queries.
Using hidden element you can post your data to second page. Using counter variable you can add dynamic form element and post it into second page.
<form method="post" action="test2.php">
<table border="1" cellspacing="0" cellpadding="6">
<tr bgcolor="#CCCCCC">
<td><strong>product id</strong></td>
<td><strong>product name</strong></td>
<td><strong>product price</strong></td>
<td><strong>quantity</strong></td>
</tr>
<?php
$query = $con->query('SELECT * FROM product'); //your query goes here
$results = $query->fetchAll(PDO::FETCH_ASSOC);
$i=0; //counter variable
foreach ($results as $row)
{
?>
<tr>
<td>
<?php echo $row['prod_id']; ?>
<input type="hidden" name="prod_id<?php echo $i; ?>" value="<?php echo $row['prod_id']; ?>" />
</td>
<td>
<?php echo $row['prodname']; ?>
<input type="hidden" name="name<?php echo $i; ?>" value="<?php echo $row['prodname']; ?>" />
</td>
<td>
<?php echo $row['price']; ?>
<input type="hidden" name="price<?php echo $i; ?>" value="<?php echo $row['price']; ?>" />
</td>
<td><input name="quantity<?php echo $i; ?>" type="text" value="0"></td>
</tr>
<?php
$i++; //increment counter variable
}
?>
<input type="hidden" name="rows" id="rows" value="<?php echo $i; ?>" />
</table>
<br>
<input type="submit" value="Add Records">
</form>
Your insert page code goes here....
for($i=0;$i<=$_POST['rows'];$i++)
{
$prodid = $_POST['prod_id'.$i];
$pname = $_POST['name'.$i];
$pprice = $_POST['price'.$i];
$con ->exec("insert into product(prod_id,prodname,price)values('$prodid', '$pname','$pprice' )" );
}
I suggest putting the product_id in a hidden form element like this:
<tr>
<input type='hidden' name='product_id' value='<?php echo $row['product_id']; ?>'/>
<td><?php echo $row['product_id']; ?></td>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_price']; ?></td>
<td><input name="quantity" type="text" value="0"></td>
</tr>
This will send the product_id with your quantity and you can use it in your insert statement.
The only problem with this is if you have more than one row, there will be more than one hidden element for product_id, etc. Ways to overcome this include differentiating them by appending an incrementing number on the hidden element's name, e.g.:
$i = 1;
foreach ($results as $row)
{
$product_id_name = 'product_id_'.$i;
$quantity_name = 'quantity_'.$i;
... echo your table row, using $product_id_name in the hidden element, and $quantity_name in your text input
$i++;
}
Then in your inserting code you have to look for all the items in $_POST whose keys start with "quantity_", and if they are non-zero, get the integer NNN after the key prefix "quantity_", and get the corresponding product_id_NNN value to do your insert.

Multiple row insert to table if check box is selected

I am trying to insert multiple rows to a database table if check box is selected. But in my code when I am trying to insert, new rows are inserting based on check box selection. But no data is passing. I need some advice on below code to modify:
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$qry="select * from pi";
$result=mysql_query($qry);
?>
<form action="check.php" method="post">
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr><td><input type=checkbox name=name[] value='".$row['id']."'>".$row['PI_NO']."</td><td>".$row['CUSTOMER_NAME']."</td><td>".$row['PI_ADDRESS']."</td></tr>";
}
?>
<input type="submit" value="save" id="submit">
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$name=$_POST['name'];
foreach($_POST['name'] as $x)
{
$qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
}
?>
Notes:
You forgot to bind the name of your checkbox using a single tick (')
You used variables in your query which you didn't defined and assigned value with yet
You only passed on the value of name, and did not include the Pi Address and Customer name. I'll be passing them by hidden input using <input type="hidden">.
I'll change the way you check your passed on form by looping them and check them using for() and if()
Use mysql_real_escape_string() before using them in your queries to prevent some of the SQL injections. But better if you consider using mysqli prepared statement rather than the deprecated mysql_*.
Is your post a single file? If it is, you must enclose your query using an isset() to prevent error upon loading the page.
You didn't close your <form>
Here's your corrected while loop:
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td>
<input type="checkbox" name="name[]" value="<?php echo $row['id']; ?>">
<?php echo $row["PI_NO"]; ?>
<!-- HERE IS THE START OF YOUR TWO HIDDEN INPUT -->
<input type="hidden" name="piaddress[]" value="<?php echo $row["PI_ADDRESS"]; ?>">
<input type="hidden" name="customer[]" value="<?php echo $row["CUSTOMER_NAME"]; ?>">
</td>
<td><?php echo $row['CUSTOMER_NAME']; ?></td>
<td><?php echo $row['PI_ADDRESS']; ?></td>
</tr>
<?php
} /* END OF WHILE LOOP */
?>
<input type="submit" value="save" id="submit">
</form> <!-- YOU DID NOT CLOSE YOUR FORM IN YOUR POST -->
And your query:
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */
for($x=0; $x<=$counter; $x++){
if(!empty($_POST["name"][$x])){
$PI_NO = mysql_real_escape_string($_POST["name"][$x]);
$CUSTOMER_NAME = mysql_real_escape_string($_POST["customer"][$x]);
$PI_ADDRESS = mysql_real_escape_string($_POST["piaddress"][$x]);
$qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
} /* END OF CHECKING THE CHECKBOX IF SELECTED */
} /* END OF FOR LOOP */
?>
Lots of little problems. And some big ones.
as $x){ .. $x is not being used so I assume you just loop for the number of checked boxes.
These have no values: '$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS'
Missing </form>
Not being used: $name=$_POST['name'];
<?php
echo '<form action="check.php" method="post"><table><tr><th>A</th><th>B</th><th>C</th></tr>';
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$sql = "select `id`,`PI_NO`, `CUSTOMER_NAME` ,`PI_ADDRESS` from `pi`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo "<tr><td><input type=\"checkbox\" name=\"name[]\" value=/"$row[0]/"'>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
}
echo '<input type="submit" value="save" id="submit"></form>';
foreach($_POST['name'] as $x){
$sql="INSERT INTO pi (`PI_NO`, `CUSTOMER_NAME`, `PI_ADDRESS`)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($sql);
}
?>

Delete From Table - Via Form

I have a HTML table that displays all my table entries. Also in the table is a delete button next to every SQL entry.
I want to be able to delete an entry the user selects. I've made the form to Post PHP_Self, and I'm passing in the index $i from the while loop as a reference:
$i = 0;
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td>
<? echo $row['uniqueIdentifier']; ?>
</td>
<td>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type='hidden' name='remove_entrie_ID' value='<? echo $i; ?>' />
<input type='submit' value='Delete' name='remove_entrie'>
</form>
</td>
</tr>
<?
$i++;
}
So this is passed to itself, I now want to do a DELETE WHERE 'INDEX OF TABLE' == $i, type thing? I don't even know if this is possible.
if(isset($_POST['remove_entrie'])){
$index = $_POST['remove_entrie_ID']
echo "Index: " . $index;
//mysqli_query($con, "DELETE FROM Users WHERE INDEX = '$index'");
}
I'm basically using the $i to pick out the index of how the table was loaded, and then using that to pick out which row I want to delete. But I have a feeling this can't be done?
I basically want to delete a row the user has selected from the table.
You don't need $i variable. Just make simple modification in your list:
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td>
<? echo $row['uniqueIdentifier']; ?>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="remove_entrie_ID" value="<? echo $row['uniqueIdentifier']; ?>" />
<input type="submit" value="Delete" name="remove_entrie">
</form>
</td>
</tr>
<?
}
It is also good idea to escape specials chars etc in your post variable to avoid sql injection.

Categories