Jump to content

barbatruc

Members
  • Posts

    28
  • Joined

  • Last visited

    Never

Posts posted by barbatruc

  1. For different MySQL data types, use inputs phpMyAdmin is using, this can help: (ref: http://www.phpmyadmin.net).

     

    For example:

    varchar, int, char, float: use input of type text (or password).
    
    enum: use select or radio buttons (one choice only)
    
    set: use select with attribute multiple="multiple" and a size="x" where x is min(number of items, 7) (note: 7 is a magic number for interfaces displaying item listings) or checkboxes (multiple choices)
    
    text: use textarea

    For your situation, since you only have 3 choices, I would use checkboxes. They all have the same name (name=\"Type[]\") only their value are different. To insert their values into MySQL, simply use implode() method:

    // let\'s say values are in $_POST[\'Type\']:
    
    $sql = "INSERT INTO table SET Type = \'".implode(",", $_POST[\'Type\'])."\'";

    JP.

  2. It doesn\'t have anything to do with PHP.. it only describes the rules about the way data is recorded into your database.

     

    A primary key is a set of fields (1 or more) than has a restriction: there is no possibility of having two rows with the same values into it (or at least in the fields contained in the Primary key). For example, if you have 4 pets in your table:

     

    [petName], [petColor]

    Bob, brown

    Polo, white

    Rex, Black

    Roxy, Blue (?!?)

     

    this will be legal because there are no pets with twice the same name and the same color at the time. The following would be legal too, even if you have Bob twice:

     

    Bob, brown

    Polo, white

    Bob, yellow

    Rex, Black

     

    The following would be illegal because \"Bob, brown\"would be find twice, which is incorrect (the set petName and petColor is the same...):

     

    Bob, brown

    Polo, white

    Bob, brown

    Rex, Black

     

    What this all means? If you try to execute an SQL query to insert a pet that is already in your table, then the query will fail to execute. In PHP term, you have the following:

    // your SQL query:
    
    $sql = "INSERT into Blah (\'bob\',\'brown\',\'some other params\')";
    
    // execution of the query
    
    $result = mysql_query($sql);
    
    // if an error occured
    
    if (!$result) {
    
     // we display the error returned by the MySQL server
    
     echo "An error occured: ".mysql_error()."<br />n";
    
    }

    Hope this will help.

     

    JP.

  3. If you use PEAR, you can use two queries and harvest the ids in a single array:

    $ITEMS = array();
    
    $sql = "SELECT ITEM FROM <first_table> WHERE ...";
    
    $ITEMS += $db->getCol($sql);
    
    $sql = "SELECT ITEM FROM <second_table> WHERE ...";
    
    $ITEMS += $db->getCol($sql);
    
    $ITEMS = array_unique($ITEMS);

    You have an array with unique IDs from both tables.

     

    JP.

  4. When dealing with users table in MySQL, you need to restart the MySQL server to make changes effective.

     

    In PHPMyAdmin, when you deal with users (from version 2.4.0 I think) the service is automatically restarted (tell me if I\'m wrong). I remember creating new users in MySQL in an older version of PHPMyAdmin and I had to restart the server each time I wanted the newly created user to be active. You can restart MySQL with PHPMyAdmin on the home page (Restart MySQL).

     

    This should work. Also, if you set the users in your db but didn\'t restarted MySQL and your server crashed, this explains why you should really restart MySQL when you do modifications on users.

     

    JP.

  5. The solution would be to have several servers... Apache won\'t work faster because you run several instance of it.

     

    Ta machine est bonne... mais je pense que plus tu as de serveurs, plus tu as la possibilité de partager les connexions sur les différents sites! Et tu évite les pannes générales! Un seul serveur c\'est aussi compliqué pour faire des backups, ce qui est TRÈS important quand tu es un host.

     

    Au plaisir!

     

    JP.

  6. What you did may be safe, but you could do something like this:

    <input type="text" name="var1[]">
    
    <input type="text" name="var2[]">
    
    <input type="text" name="var3[]">
    
    
    
    <input type="text" name="var1[]">
    
    <input type="text" name="var2[]">
    
    <input type="text" name="var3[]">
    
    
    
    <input type="text" name="var1[]">
    
    <input type="text" name="var2[]">
    
    <input type="text" name="var3[]">

    And then after that:

    foreach($_POST[\'var1\'] AS $id => $value) {
    
     $var1 = $_POST[\'var1\'][$id];
    
     $var2 = $_POST[\'var2\'][$id];
    
     $var3 = $_POST[\'var3\'][$id];
    
     // build your SQL with $var1, $var2, $var3!
    
    }

    Hope this helps!

     

    JP.

  7. JUst remember I already encountered something like this...

     

    it says parse error on line one... is the line one containing something like <?xml ?

    If it is, PHP is trying to parse XML content... because of the short tag <? may be accepted by your php.ini configuration. To solve that, you may try to output the first line with PHP itself:

    <?php echo \'<?xml ... ?>\'; ?>

    If this works, then this is not necessary to submit part of your code. ;)

     

    JP.

  8. according to your query, it will select everything from test that has a specific id and not older than a certain number of days OR everything in category #9 (no matter how old its content is). Is this what you want?

     

    You should try this; it will ensure all content is not older than a certain number of days, but might be coming from a specific category or from category #9:

    $query = "SELECT * FROM test WHERE (TO_DAYS(NOW()) - TO_DAYS (timestamp)) < days_valid AND (category_id=\'$category[id]\' or category_id=\'9\') ORDER BY timestamp DESC LIMIT $pager->offset,$pager->limit";
    
    // notice the use of ( and ) for the OR operator...

    Use need to know how to use logical operators... This might help:

    http://java.sun.com/docs/books/tutorial/ja...ts/bitwise.html

     

    JP.

  9. Use AUTO_INCREMENT so your field \"id\" will get a new value for each INSERT you do. Also make your id an Int (unsigned by preferences) so it may increment for each insert query.

    ALTER TABLE `news` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

    Voilà!

     

    JP.

  10. Auto_increment fields (mostly int unsigned ones) must be

    1) Unique

    2) Set as the only one field for the primary key

     

    In other words:

    CREATE TABLE dn_jobs ( 
    
      jobnr_week int(2) NOT NULL, 
    
      jobnr_volgnr int(4) NOT NULL AUTO_INCREMENT, 
    
      jobnr_jaar int(4) NOT NULL, 
    
      i_ordernr int(8), 
    
      v_ordernr int(8), 
    
      levnr int(6), 
    
      leersoort varchar(10), 
    
      status char(3), 
    
      administratie char(4), 
    
      ht_jobnr varchar(30), 
    
      PRIMARY KEY(jobnr_volgnr),
    
      )

    may do the trick... (not sure but this is not a good idea to set your auto_increment field jobnr_volgnr since auto_increment fields are ALWAYS unique). Can I suggest something else ?

    CREATE TABLE dn_jobs ( 
    
      id int(10) unsigned NOT NULL AUTO_INCREMENT, 
    
      jobnr_week int(2) NOT NULL, 
    
      jobnr_volgnr int(4) NOT NULL, 
    
      jobnr_jaar int(4) NOT NULL, 
    
      i_ordernr int(8), 
    
      v_ordernr int(8), 
    
      levnr int(6), 
    
      leersoort varchar(10), 
    
      status char(3), 
    
      administratie char(4), 
    
      ht_jobnr varchar(30), 
    
      PRIMARY KEY(id),
    
      UNIQUE(jobnr_week, jobnr_volgnr, jobnr_jaar)
    
      )

    may be alright!

     

    Hope this helps!

     

    JP.

  11. What is the browser you\'re using ? In which OS ?

     

    Sometimes, you may have troubles with empty <td></td> and this might be why you get strange result like this.

     

    To avoid this you may try to do, for each <td> statement:

    <td><?php echo (!empty($row[\'indextodisplay\']) ? $row[\'indextodisplay\'] : "& nbsp;"); ?></td>

    Note: there is no \" \" between the \"&\" and \"nbsp;\", PHPbb sucks at displaying it when there is no \" \" between them.

     

    JP.

  12. Yes you can.

     

    Several fields can be part of the same Unique key. They\'ll be combined together to ensure they are unique. So let\'s say you have First Name, Last name. You could say full name won\'t appear twice by setting a Unique key as a combination of First Name and Last Name. So there might be several people with first name John in your database and several people with last name Doe, but only one John Doe.

     

    You can also have several Unique keys and use them as a combination of one or several fields. With the First name / Last name example, you would have 2 unique keys, one for each fields and this way you could only have one person with first name John and only one person with last name Doe (this doesn\'t make sense naturally...).

     

    Primary key acts like UNIQUE key, only you cannot have more than one PRIMARY KEY in each table. I\'m not sure but it doesn\'t seem that you can have several fields for a Primary key (I can\'t confirm that now - but it makes sense that a Primary key should only have one field - foreign tables are then logically using primary keys for relations between them).

     

    JP.

  13. Hi!

     

    I think you used backquotes for the values which are only valid for table and field names.

     

    Single quotes should be used instead:

    addnews_sql = "INSERT INTO `news` (`subject`, `username`, `body`) VALUES (\'$_POST[\'subject\']\', \'$_POST[\'username\']\', \'$_POST[\'body\']\')";

    Also please note that creating queries that way is not really secure... you should try to see if get_magic_quotes_gpc() returns you true and if it doesn\'t, you should use addslashes() on the values you insert in your query:

    addnews_sql = "INSERT INTO `news` (`subject`, `username`, `body`) VALUES (\'".addslashes($_POST[\'subject\'])."\', \'".addslashes($_POST[\'username\'])."\', \'".addslashes($_POST[\'body\'])."\')";

    Hope this helps!

    JP.

  14. First select the document_ids of the book the user read (userID = $user_id).

    $book_ids = array();
    
    $sql = "SELECT id FROM book_hits WHERE user_id = \'$user_id\'";
    
    $result = mysql_query($sql) or die("Error selecting book ids of user");
    
    while($book = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
     $book_ids[] = $book[\'id\'];
    
    }
    
    $sql = "SELECT * FROM books";
    
    if (!empty($book_ids)) {
    
     $sql .= " WHERE id NOT IN (\'". implode("\',\'", $book_ids) ."\')";
    
    }
    
    $books = array();
    
    $result = mysql_query($sql) or die("Error selecting user\'s unread books");
    
    while($book = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
     $books[] = $book;
    
    }
    
    // unread books in $books!

    Hope this helps.

     

    JP

×
×
  • 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.