newbtophp Posted August 27, 2009 Share Posted August 27, 2009 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 ****/ ?> Quote Link to comment https://forums.phpfreaks.com/topic/172053-solved-column-updatable/ Share on other sites More sharing options...
newbtophp Posted August 27, 2009 Author Share Posted August 27, 2009 Anyone can help? ^^ Quote Link to comment https://forums.phpfreaks.com/topic/172053-solved-column-updatable/#findComment-907212 Share on other sites More sharing options...
MadTechie Posted August 27, 2009 Share Posted August 27, 2009 What are you trying to do ? what's not working? any errors ? Quote Link to comment https://forums.phpfreaks.com/topic/172053-solved-column-updatable/#findComment-907233 Share on other sites More sharing options...
newbtophp Posted August 27, 2009 Author Share Posted August 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172053-solved-column-updatable/#findComment-907240 Share on other sites More sharing options...
MadTechie Posted August 27, 2009 Share Posted August 27, 2009 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'"; Quote Link to comment https://forums.phpfreaks.com/topic/172053-solved-column-updatable/#findComment-907259 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.