Jump to content

SQL query Help


newbtophp

Recommended Posts

I've used Crayon Violents tutorial, and got to a point where I need help, im trying to remove the id column and add another column where I can select an option from a dropdown menu.

 

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

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

// UPDATE: if we have website(s) to change...
if($_POST['cwebsite']) {
   // for each website to change...
   foreach($_POST['cwebsite'] as $cid => $cwebsite) {
      // little bit of cleaning...
      $id = mysql_real_escape_string($cid);
      $website = mysql_real_escape_string($cwebsite);
      // update website in the table  
      $sql = "UPDATE info SET website = '$website' WHERE id = '$id'";
      $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
   } // end foreach
} // end if
         
// DELETE: if we have a website to delete...
if($_GET['website']) {
   // little bit of cleaning...
   $website = mysql_real_escape_string($_GET['website']);
   // delete website from table
   $sql = "DELETE FROM info WHERE website = '$website'";
   $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 websites
   $allowed = array('id','website');
   // bit of cleaning...
   $order = mysql_real_escape_string($_GET['orderby']);
   // is it a valid column website? yes: use it. no: default to 'id'
   $order = (in_array($order, $allowed))? $order : "id";
// if no link clicked, default to 'id'
} else {
   $order = "id";
} // end else

// SELECT: get the list of websites from database
$sql = "SELECT id, website FROM info ORDER BY $order";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_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=id'>ID</td>
      <td><a href = '{$_SERVER['PHP_SELF']}?orderby=website'>Website</td>
      <td>Delete</td>
   </tr>
LISTCOLS;

// loop through list of websites 
while ($list = mysql_fetch_assoc($result)) {
echo <<<LISTINFO
   <tr>
      <td>{$list['id']}</td>
      <td><input type = 'text' website = 'cwebsite[{$list['id']}]' value = '{$list['website']}'>
      <td><a href = '{$_SERVER['PHP_SELF']}?website={$list['website']}'>delete</a></td>
   </tr>
LISTINFO;
} // end while

// list input box for adding new entry
echo <<<NEWENTRY
   <tr>
      <td bgcolor = 'gray'></td>
      <td><input type = 'text' website = 'website'></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 ****/

?>

 

Heres how it currently looks:

 

2u4tzc5.jpg

 

Heres how im trying to get it too look:

 

34rzml2.jpg

 

Can anyone help.

 

Thanks

 

Link to comment
Share on other sites

Well why don't you have a go first? I'm sure you can work out how to prevent the ID from being displayed. And i'm sure you can attempt to output a select box in the loop. There's not much point in following a tutorial and not attempting to alter it to your needs. If you get stuck with something or you get errors you don't understand, then post back here and i'm sure people will help.

 

You might have some troubles getting the form to work properly, but once you've got it displaying correctly, we can deal with that.

Link to comment
Share on other sites

Well why don't you have a go first? I'm sure you can work out how to prevent the ID from being displayed. And i'm sure you can attempt to output a select box in the loop. There's not much point in following a tutorial and not attempting to alter it to your needs. If you get stuck with something or you get errors you don't understand, then post back here and i'm sure people will help.

 

You might have some troubles getting the form to work properly, but once you've got it displaying correctly, we can deal with that.

 

Every post I make here, I always try before I ask, I do never abuse the knowledge of the gurus/recommend's.

 

I started one from scratch at:

 

http://www.phpfreaks.com/forums/index.php/topic,259286.msg1225881.html#msg1225881

 

But then I got stuck, so Crayon Violent suggested me his tutorial so I literally restarted and used his code, but Im unfamilar with the code, so theirfore I asked for help.

 

I tried modifying this code, but Im back to the same point as I was when I originally started my own code. I cant get the drop down options to work with the submit.

Link to comment
Share on other sites

I cant get the drop down options to work with the submit.

 

So the issue isn't with the display of the above? It's more with using multiple select boxes? May we see the code that you have problems in this area with? I suspect what you need to do is name the select boxes as an array and loop through the results when you process the form (a process similar to which, albeit using checkboxes, can be found here). I might be wrong, however, which is why we're going to need to see some code.

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.