Jump to content

[SOLVED] Column Updatable


newbtophp

Recommended Posts

Im trying to make the name column editable/updatable like the email column, but can get my head around it.

 

Heres the code:

 

<?php
/**** Dealing with the database ****/
// connect to db
$conn = mysql_connect('localhost','username','password') or trigger_error("SQL", E_USER_ERROR); 
$db = mysql_select_db('databasename',$conn) or trigger_error("SQL", E_USER_ERROR); 

// INSERT: if we have a email to add...
if($_POST['email']) {
   // little bit of cleaning...
   $email = mysql_real_escape_string($_POST['email']);
   // insert new email into table
   $sql = "INSERT INTO mytable (name, email) VALUES ('','$email')";
   $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} // end if

// UPDATE: if we have email(s) to change...
if($_POST['cemail']) {
   // for each email to change...
   foreach($_POST['cemail'] as $cname => $cemail) {
      // little bit of cleaning...
      $name = mysql_real_escape_string($cname);
      $email = mysql_real_escape_string($cemail);
      // update email in the table  
      $sql = "UPDATE mytable SET email = '$email' WHERE name = '$name'";
      $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
   } // end foreach
} // end if
         
// DELETE: if we have a email to delete...
if($_GET['email']) {
   // little bit of cleaning...
   $email = mysql_real_escape_string($_GET['email']);
   // delete email from table
   $sql = "DELETE FROM mytable WHERE email = '$email'";
   $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} // end if

// ORDERBY: if one of the links was clicked..
if ($_GET['orderby']) {
   // make an aray of allowed emails
   $allowed = array('name','email');
   // bit of cleaning...
   $order = mysql_real_escape_string($_GET['orderby']);
   // is it a valname column email? yes: use it. no: default to 'name'
   $order = (in_array($order, $allowed))? $order : "name";
// if no link clicked, default to 'name'
} else {
   $order = "name";
} // end else

// SELECT: get the list of emails from database
$sql = "SELECT name, email FROM mytable ORDER BY $order";
$result = mysql_query($sql, $conn) or die(mysql_error());
/**** end deal with the database ****/

/**** list everything out ****/
// list columns
echo <<<LISTCOLS
<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>
<table border = '1'>
   <tr>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=name'>name</td>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=email'>email</td>
      <td>Delete</td>
   </tr>
LISTCOLS;

// loop through list of emails 
while ($list = mysql_fetch_assoc($result)) {
echo <<<LISTmytable
   <tr>
      <td>{$list['name']}</td>
      <td><input type = 'text' email = 'cemail[{$list['name']}]' value = '{$list['email']}'>
      <td><a href = '{$_SERVER['PHP_SELF']}?email={$list['email']}'>Delete</a></td>
   </tr>
LISTmytable;
} // end while

// list input box for adding new entry
echo <<<NEWENTRY
   <tr>
      <td bgcolor = 'gray'></td>
      <td><input type = 'text' email = 'email'></td>
      <td bgcolor = 'gray'></td>
   </tr><tr>
      <td></td>
      <td align = 'center'><input type = 'submit' value = 'Submit'></td>
      <td></td>
   </tr>
</table>
</form>  
NEWENTRY;
/**** end list everything out ****/

?>

Link to comment
Share on other sites

Im trying to make 3 columns, 1 whichs displays names, the 2nd which displays emails, and the 3rd which holds the delete links.

 

I've got the data displaying fine, my second column the emails is editable (each value is contained in a field), so when i edit it i can click submit and it gets updated.

 

The problem is im trying to add that also to the first column so i can edit the names.

 

preview.jpg

Link to comment
Share on other sites

Your need to use the Unique ID (autonumber) field, have that in a hidden field and use that,

if you don't have one then i guess you could store the name twice (new and old)

then have a statement like

$sql = "UPDATE mytable SET email = '$email', name = '$newname' WHERE name = '$oldname'";

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.