Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

php and mysql

Posted on 2006-04-20
8
Medium Priority
?
386 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:livewirewebsolutions
6 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16501670
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
 

Author Comment

by:livewirewebsolutions
ID: 16501912
Once the table is displayed, I can't change any values.  They are not editable.
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 672 total points
ID: 16502020
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:livewirewebsolutions
ID: 16511908
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
 
LVL 20

Assisted Solution

by:Muhammad Wasif
Muhammad Wasif earned 664 total points
ID: 16514578
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
 
LVL 9

Assisted Solution

by:tolgaong
tolgaong earned 664 total points
ID: 16518852
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question