Jump to content

consolidating two pages


elentz

Recommended Posts

I have two pages;  The first takes a user input, queries the DB and presents a table with form qualities, where the user can submit a change to the record using the second page.  I want to a: consolidate both pages into one, the table could have as many as 60 rows that might need to be changed (I am OK with the user clicking on many "submit" buttons, or b: hold ALL the changes for one submit.  Here are the pages:

Keys.php

<!DOCTYPE html>
<html>
<head>
<Title>Edit Template Settings</title>
</head>
<body>
<p>Phone Provisioning System</p>
<hr>

<form action="" method="post">
Template Name: <input type="text" name="tname"><br>
<input type="submit">

<?php
$tempname = $_POST['tname'];
echo $tempname;
$con = mysqli_connect('localhost','root','');
$sql = "Select * from cqadmin.keys where templatename = '$tempname'";
$records = mysqli_query($con,$sql);
?>
<table>
<tr>
<th></th>
<th></th>
<th>Key Name</th>
<th>Key Type</th>
<th>Value</th>
<th>Label</th>
</tr>
<?php
while($row = mysqli_fetch_array($records))
{
    echo "<tr><form action=update.php method=post>"; 
    echo"<td><input type=hidden name=pid value='".$row['id']."'></td>";   
    echo"<td><input type=hidden name=name value='".$row['templatename']."'></td>";
    echo"<td><input type=text name=key value='".$row['keyname']."'><Readonly></td>";
    echo"<td><input type=text name=type value='".$row['keytype']."'></td>";
    echo"<td><input type=text name=value value='".$row['keyvalue']."'></td>";
    echo"<td><input type=text name=label value='".$row['keylabel']."'></td>";
    echo "<td><input type=submit>";
    echo"</form></tr>";
}

?>
</body>
</html>


Update.php

<?php
//Connect to MYSQL
$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,'cqadmin');
//Get Variables
$name = $_POST['name'];
$key = $_POST['key'];
$type = $_POST['type'];
$value = $_POST['value'];
$label = $_POST['label'];
$keyname = $_POST['keyname'];
$id = $_POST['pid'];

$sql = "update cqadmin.keys set templatename='$name',keytype='$type',keyvalue='$value',keylabel='$label' where id='$id'";
if(mysqli_query($con,$sql))
  header("refresh:.01; url=keys.php");
	else
	echo"The update failed";
mysqli_close($con);

?>

It works this way except that only one record can be changed and then the template must be re-entered.  Can a function be created to do this within the 1st page?  What other ways?  

 

Thanks

Link to comment
Share on other sites

One thing at a time.

 

For one page to do both actions, in this case, it's easiest to make it read like

<?php

common initialization stuff;

if (form was submitted) {
	process the form;
	redirect back to this page;
}

get data;

?>
display data
Remember to set the form's action back to itself (action="" is good). The redirect after updating stays there.

 

When you have it working in one file then you can move on to updating multiple records at once...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.