Jump to content

Dreamweaver update multiple rows


ferret147

Recommended Posts

I am not much of a hand coder and find the automated option within Dreamweaver very handy for quick app development until tonight when I ran in to a slight problem.

 

I have 1 database table(players) with the following three fields;

  • name
  • imageURL
  • information

 

There are 100 records inserted into this table.

 

I want to list all 100 entries on a page (Which I have done) and then edit each entry on the page so when I submit the page each record is updated instead of me having to update each record individually.

 

I have had a google around and read some articles but still not yet found a solution, below is my current code.

<?php require_once('Connections/brackets.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_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE players SET imageURL=%s, information=%s WHERE name=%s",
                       GetSQLValueString($_POST['image'], "text"),
                       GetSQLValueString($_POST['info'], "text"),
                       GetSQLValueString($_POST['name'], "text"));

  mysql_select_db($database_brackets, $brackets);
  $Result1 = mysql_query($updateSQL, $brackets) or die(mysql_error());

  $updateGoTo = "Tournament-Brackets-edit-players.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_brackets, $brackets);
$query_Players = "SELECT * FROM players";
$Players = mysql_query($query_Players, $brackets) or die(mysql_error());
$row_Players = mysql_fetch_assoc($Players);
$totalRows_Players = mysql_num_rows($Players);
?>
<!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>Untitled Document</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
<table border="1">
  <tr>
    <td>name</td>
    <td>imageURL</td>
    <td>information</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><input type="text" name="<?php echo $row_Players['name']; ?>" value="<?php echo htmlentities($row_Players['name'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
      <td><input type="text" name="<?php echo $row_Players['imageURL']; ?>" value="<?php echo htmlentities($row_Players['imageURL'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
      <td><input type="text" name="<?php echo $row_Players['information']; ?>" value="<?php echo htmlentities($row_Players['information'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <?php } while ($row_Players = mysql_fetch_assoc($Players)); ?>
</table>
<input type="submit" value="Update Page" />
<input type="hidden" name="MM_update" value="form1" />
</form>
</form>
</body>
</html>
<?php
mysql_free_result($Players);
?>

 

Everything in the loop words just fine as you would expect it is when it comes to submiting the data that well basically it does not submit the data, I am not a complete wolly and know it is to do with this part of the coding

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE players SET imageURL=%s, information=%s WHERE name=%s",
                       GetSQLValueString($_POST['image'], "text"),
                       GetSQLValueString($_POST['info'], "text"),
                       GetSQLValueString($_POST['name'], "text"));

 

So my question is how do I adjust the small snippet of code above to allow multiple inputs?

Link to comment
Share on other sites

  • 3 months later...

Your form field name="..." attributes are not 'image', 'info', and 'name' for the three input fields, so in the current code, $_POST['image'], $_POST['info'], and $_POST['name'] don't exist and don't have any value.

 

You should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. The non-existent $_POST variables would have been pointed out by the resulting error messages.

 

Once you fix the code to work for one set of form fields, you would use array names for the form fields, with the array index value being some identifying value for each row in the database, such as the auto-increment id. See this link - http://us3.php.net/manual/en/faq.html.php#faq.html.arrays

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.