I am using a dreamweaver form, which has some buttons on it. The button id is stored in the database as the conveyor number.

When updating records i want to be able to click a button, which will use the button id to call for the correct record in the database, whereas at the moment when any button is clicked it displays all records stored in the database.

The conveyor number field would not be editable on the update page.

Recommended Answers

All 6 Replies

You should post your update query and the form used. Without code we can't tell you where mistakes are.

Here i have my layout page and when a button is clicked i want the button id/name to appear in the conveyor number textbox on update_record page. Also once i get this working once the conveyor number appears in the text box the relevant information stored in the database should display in the other text boxes.

layout.php

<?php require_once('Connections/Database_Connection.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_Database_Connection, $Database_Connection);
$query_Recordset1 = "SELECT * FROM conveyor_number";
$Recordset1 = mysql_query($query_Recordset1, $Database_Connection) 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>Untitled Document</title>
</head>

<body>

<p>&nbsp;</p>
<form id="form2" name="form2" method="post" action="">
<p>
    <label>
      <input name="CMD_LRC1140" type="button" id="CMD_LRC1140" value="CMD_LRC1140" onclick="location.href='update_record.php'"/>
    </label>
  </p>
  <p>
    <label>
      <input name="CMD_LRC1138" type="button" id="CMD_LRC1138" value="CMD_LRC1138" onclick="location.href='update_record.php'"/>
    </label>
  </p>
  <p>
    <label>
      <input name="CMD_LRC1115" type="button" id="CMD_LRC1115" value="CMD_LRC1115" onclick="location.href='update_record.php'"/>
    </label>
  </p>
  <p>&nbsp;</p>
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Update_record.php

<?php require_once('Connections/Database_Connection.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"] == "form2")) {
  $updateSQL = sprintf("UPDATE conveyor_number SET `Conveyor Number`=%s, `Motor Temperature`=%s, `Motor Frame`=%s WHERE `Asset Type`=%s",
                       GetSQLValueString($_POST['Conveyor Number'], "text"),
                       GetSQLValueString($_POST['Motor_Temperature'], "int"),
                       GetSQLValueString($_POST['Motor_Frame'], "int"),
                       GetSQLValueString($_POST['Asset_Type'], "int"));

  mysql_select_db($database_Database_Connection, $Database_Connection);
  $Result1 = mysql_query($updateSQL, $Database_Connection) or die(mysql_error());

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

mysql_select_db($database_Database_Connection, $Database_Connection);
$query_Recordset1 = "SELECT * FROM conveyor_number WHERE conveyor_number.`Conveyor Number`";
$Recordset1 = mysql_query($query_Recordset1, $Database_Connection) 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>Untitled Document</title>
</head>

<body>

<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Conveyor Number:</td>
      <td><input type="text" name="Conveyor Number"  />
        <label>

        <!--  <select name="conveyor_list" id="conveyor_list">
          <?php
          $query_Recordset1="select * FROM conveyor_number ";
          $Recordset1=mysql_query($query_Recordset1, $Database_Connection) or die(mysql_error());;
          $options="";
          $asset="";
          $motor_temp="";
          $motor_frame="";

 while ($row = mysql_fetch_assoc($Recordset1)) {

  $Conveyor_Number=$row["Conveyor Number"];
  $Asset_Type=$row["Asset Type"];
  $Motor_Temperature=$row["Motor Temperature"];
  $Motor_Frame=$row["Motor Frame"];

  $options.="<option value=\"$Conveyor_Number\">$Conveyor_Number";
  $asset.="<text value=\"$Asset_Type\">$Asset_Type";
  $motor_temp.="<text value=\"$Motor_Temperature\">$Motor_Temperature";
  $motor_frame.="<text value=\"$Motor_Frame\">$Motor_Frame";
  }
?>
            <option value=0>Choose<?php echo $options?></option>

          </select> -->

      </label></td>

      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Asset Type:</td>
      <td><input type="text" name="Asset_Type" value="<?php echo htmlentities($Recordset1['Asset Type'], ENT_COMPAT, 'utf-8'); ?>" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Motor Temperature:</td>
      <td><input type="text" name="Motor_Temperature" value="<?php echo htmlentities($Recordset1['Motor Temp'], ENT_COMPAT, 'utf-8'); ?>" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Motor Frame:</td>
      <td><input type="text" name="Motor_Frame" value="<?php echo htmlentities($Recordset1['Motor Frame'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input name="save" type="button" id="save" value="Save" onclick="location.href='layout.php'"/></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>
    <input type="hidden" name="MM_update" value="form2" />
  </p>
</form>
<p>&nbsp;</p>


</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

It's really hard to read this code. Next time use bbcode tags.

On update_record.php this query is not complete:

$query_Recordset1 = "SELECT * FROM conveyor_number WHERE conveyor_number.`Conveyor Number`";

It should be:

$query_Recordset1 = "SELECT * FROM conveyor_number WHERE conveyor_number.`Conveyor Number` = `id_to_search`";

Try to change this.

Now. Layout page is just used to open the edit form on update_record? Am I wrong? Why don't you just use links? Something like:

<a href="update_record.php?id=1">update record</a>

On the update page you just need to run a command like this:

if(ctype_digit($_GET['id']))
{
   $id = $_GET['id'];
   $query_Recordset1 = "SELECT * FROM conveyor_number WHERE conveyor_number.`Conveyor Number` = $id";
}

I have got the links from layout to update_record page but i need the button id to appear in the conveyor number text box on update_record page.

I have changed my code as above, but i get this error.
Unknown column 'id_to_search' in 'where clause'

Surely that id_to_search, needs to be Conveyor Number, but when i use that i dont get the error, just the update_record page appears with no conveyor id in the text box???

Sure, in this line you need to set the value:

<input type="text" name="Conveyor Number" />

that becomes something like:

<input type="text" name="Conveyor Number" value="<?php echo $id; ?>" />

Right now, update_record.php doesn't send out this value. And the form in your layout.php page doesn't have an ID field value, you set a different name for each button and send a value like this: CMD_LRC1140
this:

<input name="CMD_LRC1138" type="button" id="CMD_LRC1138" value="CMD_LRC1138" onclick="location.href='update_record.php'"/>

should be:

<input name="id" type="submit" id="CMD_LRC1138" value="CMD_LRC1138" />

So try to change your layout.php form to this:

<form id="form2" name="form2" method="post" action="update_record.php">
    <p><label><input name="id" type="submit" id="CMD_LRC1140" value="CMD_LRC1140" /></label></p>
    <p><label><input name="id" type="submit" id="CMD_LRC1138" value="CMD_LRC1138" /></label></p>
    <p><label><input name="id" type="submit" id="CMD_LRC1115" value="CMD_LRC1115" /></label></p>
</form>

In update_record.php you can write:

$id = $_POST['id'];

# the query will become:
$query_Recordset1 = "SELECT * FROM conveyor_number WHERE conveyor_number.`Conveyor Number` = $id";

Hope is useful. Bye.

Thanks that now works. but..... it doesnt show the information from the database realating to the conveyor number in the other fields. How do i get this to work?

Also from the layout.php when the a button is clicked it would check the database to see if any data relating to that conveyor number has already been entered, if not it would open a balnk form for the data to be entered and once saved the button would change colour to indicate that conveyor information has been entered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.