Jump to content

Add a dynamic <a href> and POST data from db table to form.


paddyhaig

Recommended Posts

This first bit of code generates a html table parsed from the fields of my 'auth.user' mySQL database.

What I would like is for the the first column (id) to be represented by an (edit) graphic button/link. This to appear on every row dynamically created by the script.

So by clicking on the button all the data in the row can then be sent by the POST method to another form and can then be edited. (A copy of that form I am including below.)

My problem is, I cannot figure out how to add a dynamic <a href> and the ability to post the data to the next form?

 

<?php require_once('../Connections/Concierge.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_Concierge, $Concierge);
$query_Recordset1 = "SELECT id, first_name, last_name, login, privilege FROM auth ORDER BY id ASC";
$Recordset1 = mysql_query($query_Recordset1, $Concierge) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Staff Member</title>
</head>

<body>
<table width="439" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td width="41"><div align="center">Edit:</div></td>
    <td width="151"><div align="center">Name:</div></td>
    <td width="98"><div align="center">Login:</div></td>
    <td width="96"><div align="center">Privilege:</div></td>
  </tr>
  <?php do { ?>
    <tr>
          <td><div align="center"><a href> <?php echo $row_Recordset1['id']; ?> </a></div></td>
      <td><div align="center"><?php echo $row_Recordset1['first_name']; ?> <?php echo $row_Recordset1['last_name']; ?></div></td>
      <td><div align="center"><?php echo $row_Recordset1['login']; ?></div></td>
      <td><div align="center"><?php echo $row_Recordset1['privilege']; ?></div></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

This is the form that should receive the row of data from the POST method and then allow for editing and then send back to the database.

 

<?php require_once('../Connections/Concierge.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form6")) {
  $insertSQL = sprintf("INSERT INTO auth (first_name, last_name, login, password, privilege) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['forename'], "text"),
                       GetSQLValueString($_POST['surname'], "text"),
                       GetSQLValueString($_POST['login'], "text"),
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['priviege'], "text"));

  mysql_select_db($database_Concierge, $Concierge);
  $Result1 = mysql_query($insertSQL, $Concierge) or die(mysql_error());

  $insertGoTo = "edit_staff_member.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_Concierge, $Concierge);
$query_Recordset1 = "SELECT * FROM auth";
$Recordset1 = mysql_query($query_Recordset1, $Concierge) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit User Details</title>


<style type="text/css">
th {
color: #FFF;
}


</style>
</head>

<body>
<div align="center">
  <p> </p>
  <form id="form6" name="form6" method="POST" action="<?php echo $editFormAction; ?>">
    <table width="234" border="1" cellpadding="0" cellspacing="0">
      <tr bgcolor="#283a86">
        <th width="95" scope="row">First Name </th>
        <td width="133"><input name="forename" type="text" id="forename" size="19" /></td>
      </tr>
      <tr bgcolor="#283a86">
        <th scope="row">Last Name</th>
        <td><input name="surname" type="text" id="surname" size="19" /></td>
      </tr>
      <tr bgcolor="#283a86">
        <th scope="row">User Name</th>
        <td><input name="login" type="text" id="login" size="19" /></td>
      </tr>
      <tr bgcolor="#283a86">
        <th scope="row">Password</th>
        <td><input name="password" type="password" id="password" size="19" /></td>
      </tr>
      <tr bgcolor="#283a86">
        <th scope="row">Privilege</th>
        <td><select name="priviege" id="priviege">
          <option value="receptionist">Receptionist</option>
          <option value="manager">Manager</option>
          <option value="administrator">Administrator</option>
          <option value="suspended">Suspended</option>
        </select></td>
      </tr>
    </table>
    <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
    <input type="hidden" name="MM_insert" value="form6" />
  </form>
  
    
  </p>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Link to comment
Share on other sites

Does it need to be posted?  Can't you just use a hyperlink to the edit page with the user id in it (http://www.yoursite.com/userEditPage.php?id=123) so that you can re-query the database for the user's details on the edit page?

 

Something like:

 

<?php foreach ($user_records as $user) : ?>
<tr>
<td><a href="editUserPage.php?id=<?php echo $user['id']; ?>">edit</a></td>
<td><?php echo $user['first_name'], " ", $user['last_name']; ?></td>
<td><?php echo $user['login']; ?></td>
<td><?php echo $user['privilege']; ?></td>
</tr>
<?php endforeach; ?>

 

Then on the editUserPage.php you can query the database for the user id passed in the URL (remember to sanitize/validate) and pre-populate the edit form with the values from the database:

 

// editUserPage.php
$id = isset($_GET['id']) ? (int) $_GET['id'] : false;
if ( ! $id) die('No user id specified to edit');

// query database for user's details
// generate html form with users details in it for editing.
// etc.

 

Hope this makes sense to you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.