php and mysql

I am trying to display all the records from a mysql database in a table and have the results all editable.  I want to be able to edit the records right in the table.  I can display the data but not edit it.  How do I do this?

thanx,
Lorne
livewirewebsolutionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi, after changing the values, you need to submit it, and update the table...

<?php
    if(isset($_REQUEST['update'])
    {
        $sql = "UPDATE mytable ...";
        mysql_query($sql) or die(mysql_error());
    }
    else
    {
?>

<FORM METHOD="post" ACTION="<?=$_SERVER['PHP_SELF']?>">
    <INPUT ...>
    <INPUT TYPE="hidden" NAME="update">
    <SUBMIT ...>
</FORM>

<?php
    }
?>

---
Harish
0
livewirewebsolutionsAuthor Commented:
Once the table is displayed, I can't change any values.  They are not editable.
0
Harisha M GEngineerCommented:
Well, how are you displaying it ?

<TABLE>

<?php
while($row = mysql_fetch_array($result))
{
    $i=$i+1;
?>

<TR>
<TD> <INPUT TYPE="text" NAME="col1<?=$i?>" VALUE="<?=$row['col1']?>"> </TD>
<TD> <INPUT TYPE="text" NAME="col2<?=$i?>" VALUE="<?=$row['col2']?>"> </TD>
...
</TR>

<?php
}
?>

</TABLE>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

livewirewebsolutionsAuthor Commented:
I am using dreamweaver and putting code together from other pages.  I am now able to display all the data in a table and edit it.  However, when I update the table,after making changes,  all the entries in the database are changed to the last entry.  So all entries are the same.  It is completely screwed up.  I'll show you the code here,

<?php require_once('Connections/connsearch.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 table1 SET scottid=%s,companyname=%s, addressline1=%s, addressline2=%s, city=%s, province=%s, postalcode=%s, telephone=%s, fax=%s, website=%s, email=%s, subscription=%s WHERE scottid=%s",
 
                       GetSQLValueString($_POST['scottid'], "text"),  
                       GetSQLValueString($_POST['companyname'], "text"),
                       GetSQLValueString($_POST['addressline1'], "text"),
                       GetSQLValueString($_POST['addressline2'], "text"),
                       GetSQLValueString($_POST['city'], "text"),
                       GetSQLValueString($_POST['province'], "text"),
                       GetSQLValueString($_POST['postalcode'], "text"),
                       GetSQLValueString($_POST['telephone'], "text"),
                       GetSQLValueString($_POST['fax'], "text"),
                       GetSQLValueString($_POST['website'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['subscription'], "text"));
                       

  mysql_select_db($database_connsearch, $connsearch);
  $Result1 = mysql_query($updateSQL, $connsearch) or die(mysql_error());

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

mysql_select_db($database_connsearch, $connsearch);
$query_rseditmachines = "SELECT * FROM table1";
$rseditmachines = mysql_query($query_rseditmachines, $connsearch) or die(mysql_error());
$row_rseditmachines = mysql_fetch_assoc($rseditmachines);
$totalRows_rseditmachines = mysql_num_rows($rseditmachines);
?><!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=iso-8859-1" />
<title>editmachines.php</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="<?php echo $editFormAction; ?>">
  <table border="1">
    <tr>
      <td><strong>scottid</strong></td>
      <td><strong>companyname</strong></td>
      <td><strong>addressline1</strong></td>
      <td><strong>addressline2</strong></td>
      <td><strong>city</strong></td>
      <td><strong>province</strong></td>
      <td><strong>postalcode</strong></td>
      <td><strong>telephone</strong></td>
      <td><strong>fax</strong></td>
      <td><strong>website</strong></td>
      <td><strong>email</strong></td>
      <td><strong>subscription</strong></td>
    </tr>
    <?php do { ?>
    <tr>
      <td><input type="text" name="scottid" value="<?php echo $row_rseditmachines['scottid']; ?>"></td>
      <td><input type="text" name="companyname" value="<?php echo $row_rseditmachines['companyname']; ?>"></td>
      <td><input type="text" name="addressline1" value="<?php echo $row_rseditmachines['addressline1']; ?>"></td>
      <td><input type="text" name="addressline2" value="<?php echo $row_rseditmachines['addressline2']; ?>"></td>
      <td><input type="text" name="city" value="<?php echo $row_rseditmachines['city']; ?>" size="32"></td>
      <td><input type="text" name="province" value="<?php echo $row_rseditmachines['province']; ?>"></td>
      <td><input type="text" name="postalcode" value="<?php echo $row_rseditmachines['postalcode']; ?>"></td>
      <td><input type="text" name="telephone" value="<?php echo $row_rseditmachines['telephone']; ?>"></td>
      <td><input type="text" name="fax" value="<?php echo $row_rseditmachines['fax']; ?>"></td>
      <td><input type="text" name="website" value="<?php echo $row_rseditmachines['website']; ?>"></td>
      <td><input type="text" name="email" value="<?php echo $row_rseditmachines['email']; ?>"></td>
      <td><input type="text" name="subscription" value="<?php echo $row_rseditmachines['subscription']; ?>"></td>
    </tr>
    <?php } while ($row_rseditmachines = mysql_fetch_assoc($rseditmachines)); ?>
  </table>
  <input type="hidden" name="MM_update" value="form1">
  <input type="submit" value="Update record">
</form>
</body>
</html>
<?php
mysql_free_result($rseditmachines);
?>

It probably looks like a mess as I have heard that dreamweaver gives some awful code sometimes.  Suggestions on changes or a complete overhaul would be appreciated
0
Muhammad WasifCommented:
you need to pass primary key value to the update query. store primary key for example id in the hidden field in html form. and on the update page make the query something like

mysql_query("update table set field1='value 1' where id='".$_POST["hidden_id"]."');
0
tolgaongCommented:
you should use ajax with editcontentable feature of each div to access table content if you wont use input text... If you will use input and you dont have any idea with ajax then you can write a code like mine below. It creates an image and i send it data with its src property.

<html>
<head>
<script>
function sendNew(sendData,where,id){
      a = new Image();
      a.src='data.php?where='+where+'&value='+sendData+'&id='+id;
      }
</script>
</head>
<body>
<input name="" type="text" onClick="sendNew(this.value,'fieldname',1)">
</body>
</html>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.