I am trying to display values from table and want to modify the values on form submit.
here is my code:
<?php
$sql = mysql_query("SELECT * FROM TABLENAME");
//Get and store values into new variable
while ($row = mysql_fetch_array($sql)) {
$name[] = $row['name'];
$status[] = $row['status'];
}
//run for loop to identify values
for ($i = 0; $i < count($name); $i++) {
echo "<tr> ";
echo "<td>" . $name[$i] . "</td>";
echo "<td><input name='offer_new_name' size='5' type='text' value='$status[$i]' /></td>";
}
echo '<form action="" method="post">';
echo '<input type="submit" value="Save New Values" name="save_new">';
echo "</form>";
if (isset($_REQUEST['save_new'])) {
for ($i = 0; $i < count($name); $i++) {
echo "name:$name[$i]<br>";
echo "status:$status[$i]";
}
}
When I click Save New Values , it displays the old values ,, taken from db. Please guide me how to save
echo "<td><input name='new_status' size='5' type='text' value='$status[$i]' /></td>";
into new variable for each name and save them into the same table again.
Please guide me if my approach is wrong towards this requirement.
Update1:
table structure:
id name: status:
auto name1 0
auto name2 1
auto name3 0
Where this runs,
while ($row = mysql_fetch_array($sql)) {
$name[] = $row['name'];
$status[] = $row['status'];
}
values are stored like this
$name[0]
$status[0]
$name[1]
$status[1]
$name[2]
$status[2]
I can run update query like this
UPDATE TABLE set status=$status[0] where name= $name[0]
First, the following line should be in the <form></form>
echo "<td><input name='new_status' size='5' type='text' value='$status[$i]' /></td>";
Second, this code block should be on top of your script:
if(isset($_REQUEST['save_new'])){
//Your update query
}
In this code block you need an update query, which updates your database e.g
UPDATE TABLENAME SET a = new_value WHERE b = c;
This is the variable you can access the entered value;
$_POST['new_status'];
Lastly I can strongly recommend to use prepared statements. Otherwise your code will be open for SQL-Injections.
Pseudo code:
<?php
//if the submit button is pressed, update the Database
if (isset($_REQUEST['save_new'])) {
//Update your Database
}
//get the new values from Database
$sql = mysql_query("SELECT * FROM TABLENAME");
//store values into new variable
$name = array();
$status = array();
$id = array();
while ($row = mysql_fetch_array($sql)) {
$name[] = $row['name'];
$status[] = $row['status'];
$id[] = $row['id'];
}
//create HTML-From
echo '<form action="" method="post">';
for ($i = 0; $i < count($name); $i++) {
echo "<tr> ";
echo "<td>" . $name[$i] . "</td>";
echo "<td><input name='new_status' size='5' type='text' value='$status[$i]' /></td>";
}
echo '<input type="submit" value="Save New Values" name="save_new">';
echo "</form>";
?>
This 'new id' can be saved as a hidden input in your script:
$indecipherable_id = (int)$id * 7258289382234; //Only a example --> use a more complex algorithm!
Before your update query you can decrypt the id
and select the row.