Page 1 of 2 1 2 LastLast
Results 1 to 15 of 26

Thread: php insert id

  1. #1
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0

    Unhappy php insert id

    hi all, i'm new here and i really need your help..

    this is my situation,

    i have a form with to fields n submit button.
    when registered user submit this form, i want the id (primary key) to be something like this > pps/xxxx where xxxx is an integer for example pps/0001 in my database.

    the problem now is i do not know how to generate this kind of id. can someone please help me on this??? please...

  2. #2
    Join Date
    Mar 2008
    Location
    pd
    Posts
    497
    Rep Power
    152
    how to generate this kind of id
    Usually you don't have to generate it. What you do is design your database so that the id column is 'auto incremented'. Where does the 'pps' come from? OK imagine PPS comes from your application (your web page just 'knows' it has to be 'pps'), then the user types in their data and submits the form. Your server-side code INSERTs a new row into the table, but DOES NOT TRY TO SET THE ID. The database will automatically fill in the ID with the next value in series. If you need to show the user their ID, then you read the row you've just written, and like magic, the ID will be set to 0001, 0002 etc. 4 digits for an ID is not enough - you might use them all up in testing!

  3. #3
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by Seanie View Post
    Usually you don't have to generate it. What you do is design your database so that the id column is 'auto incremented'. Where does the 'pps' come from? OK imagine PPS comes from your application (your web page just 'knows' it has to be 'pps'), then the user types in their data and submits the form. Your server-side code INSERTs a new row into the table, but DOES NOT TRY TO SET THE ID. The database will automatically fill in the ID with the next value in series. If you need to show the user their ID, then you read the row you've just written, and like magic, the ID will be set to 0001, 0002 etc. 4 digits for an ID is not enough - you might use them all up in testing!
    If the 'pps' is still required, then it could be appended before the id ($Id) that was just read from database by adding the code below:

    <?php
    $NewId = "pps" . $Id;
    ?>

    and use variable $NewId for echoing. But make sure to use $Id to update data in database.

  4. #4
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0
    Thanks seanie n lotmaya for your quick reply..

    yes, i need to use the id start with PPS/xxxx. i have tried use the code given but still the PPS cannot shown. even in my database it's only show 1,2 and so on. or maybe i put in a wrong way. could lotmaya show me the right way please?
    and one more question, for the ID I use integer type with auto increment. so i think thats why PPS does not shown. could you please correct me if im wrong..

  5. #5
    Join Date
    Mar 2008
    Location
    pd
    Posts
    497
    Rep Power
    152
    Is 'PPS' a constant in your application? I mean, will IDs always start 'PPS'? If so, you can just add it in your web front end like:

    Code:
    // get latest row
    $result=mysql_query(select id from submittedform order by id desc limit 1);
    $row = mysql_fetch_assoc($result);
    echo "Latest is PPS/" . $row['id'];
    That would work if IDs always had to start PPS

    If you could have different prefixes on your ID, then add another column to your database like

    "ALTER TABLE submittedform ADD COLUMN idprefix CHAR(3) NOT NULL DEFAULT 'PPS' FIRST"
    The 'first' just tells MySQL (if that's what you're using)to put the column before the id field in the default column ordering in the table - it'll look nicer than adding it at the end). Then your php code would be

    Code:
    // get latest row
    $result=mysql_query(select id, concat(idprefix,'/',id) as myid from submittedform order by id desc limit 1);
    $row = mysql_fetch_assoc($result);
    echo "Latest is " . $row['myid'];
    I've just typed this stuff without checking syntax, so it might not be spot on. You wouldn't index 'idprefix' in the database: the autoincremented id column is unique. If you build your displayable ids with concat in a SQL statement, make sure you also select the id column on its own: otherwise you'll have to extract the unique integer id from decorated ID when you want to go go back to edit the row - and that would be nasty (lotmaya already advised this!). You don't have to display the integer id on its own - put it in a hidden element in a form, for example.
    Last edited by Seanie; 17-08-2009 at 09:42 AM. Reason: Think faster than type; also noticed lotmaya's advice

  6. #6
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0
    oh now i understand what lotmaya want me to do. i wrongly understand before this. i think that $newID is insert to database. but it is not like that. the $newID will appear on my display page. phew! sorry for my mistake.

  7. #7
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0
    oh.. thanks seanie. i do understand now.. i will use your code and test it. thanks for sharing your knowledge with me..

  8. #8
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0
    hi again. can i retrieve the ID from database like this 01 instead of 1 only?
    but of course PPS at the start. wat i'd got now is something like this. PPS/1. can i retrieve it like this PPS/01

  9. #9
    Join Date
    Aug 2009
    Location
    Malaysia
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by nur_kasih View Post
    hi again. can i retrieve the ID from database like this 01 instead of 1 only?
    but of course PPS at the start. wat i'd got now is something like this. PPS/1. can i retrieve it like this PPS/01
    Try this:

    <?php

    $NewId = "pps/" . str_pad($Id, 2, "0", STR_PAD_LEFT);

    ?>

  10. #10
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0
    Thanks lotmaya..

    i got it. many thanks to you..

    p/s: i wondering how to be a great php programmer like you all here...

  11. #11
    Join Date
    Mar 2008
    Location
    pd
    Posts
    497
    Rep Power
    152
    p/s: i wondering how to be a great php programmer like you all here...
    I don't know about lotmaya - trial and error for me!

    Just ask lots of questions, read lots of manuals, try new things... you'll get there soon enough.

    p/s a database is not a good place to keep PDFs, easier and faster to keep them on disk. If you must keep them in a database, you'll need a BLOB or a TEXT column.

  12. #12
    Join Date
    Aug 2009
    Location
    Kuching
    Posts
    19
    Rep Power
    0
    thanks seanie for sharing..

    about the pdf, i have a table for pdf in my database. before i can view the
    pdf i actually upload it to my server and all data like pdf file name, and
    description is post to my database. i manage to upload pdf file to my server
    but now i cannot view it at my page. can i do like that? i mean can i put pdf file to my server and then view it at my page?

  13. #13
    Join Date
    Mar 2008
    Location
    pd
    Posts
    497
    Rep Power
    152
    can i do like that?
    Can! But you need a PHP wizard. You'll have one page that has links to different PDFs right? For example one of the links will be:

    Code:
    <a href="/storedpdf.php?id=75">H1N1Precautions.pdf</a>
    Your storedpdf.php file will SELECT the row with id 75 from the database, and then echo the contents. I'm not sure how to do this in PHP - I code my sites in Java. It may be as simple as:

    Code:
    $result=mysql_query("select content from storedpdf where id = $id");
    $row = mysql_fetch_assoc($result);
    echo $row['content'];
    I'm not sure that's even the way it would work in PHP!
    The annoying thing about doing it this way is when a user comes to save the file, their browser will tell them the file name is 'storedpdf.php'. To get the browser to provide a 'nice' file name for the PDF file, you'd have to use 'URL rewriting', a technique (which I think is ugly) in the webserver itself, so that a request to '/storedpdf/h1n1precaution.pdf' will be handled transparently by storedpdf.php at the server.

    An easy way around this issue would be to have a folder on your server called 'storedpdf' (for example). Every time you upload a PDF, you create a new row in your storedpdf TABLE so that you get a new id, but the only thing you store in the table is the name of the file. Then you create a new folder in the storedpdf directory whose name is the id of the new row, and write the pdf file into that directory. When you build a page that links to the PDF the URL you give is then:

    <a href="/storedpdf/75/h1n1precaution.pdf">H1N1 Precautions</a>

    so that your users' browsers fetch the original PDF file direct from the server's disk, and when they try to save it, they get the original file name. Nice ah?

    Something to think about! I have just had the same issue with one of my projects written in Java. I haven't got round to fixing it yet, but it's a good example of the problem!

    [spider.my cache] http://www.ca8.uscourts.gov/opndir/08/08...

    When you see that page, try right-clicking on the link to the PDF (which is stored in a database) and 'Save As' - and see the terrible name the browser suggests!

  14. #14
    Join Date
    Aug 2009
    Location
    kch
    Posts
    2
    Rep Power
    0

    help me

    Quote Originally Posted by Seanie View Post
    Can! But you need a PHP wizard.
    what you mean by PHP wizard? can you explain more about it???because i have the same problem as nur_kasih

  15. #15
    Join Date
    Mar 2008
    Location
    pd
    Posts
    497
    Rep Power
    152
    what you mean by PHP wizard?
    An old guy, with a long white beard, tall pointy hat with strange symbols on it, carrying a staff. Knows a lot of PHP.

    At first glance, it should be as easy as storing the bytes from an URL to a PDF into the database, and then reading them back out again with an SQL statement and echoing them to the output stream. Perhaps also setting the Content-Type to application/x-pdf (or whatever it is).

    How far have you got? You have an upload page? You are sure that the bytes from the PDF are stored in the DB? How are you sending the bytes back to the user?

    I'm not a PHP wizard, but I do have Google. And I'm old. My beard is only white in places, and only when it grows. Here is the fruit of 5 minutes work, it works on a server here:

    Code:
    <?php
    mysql_connect(...);
    mysql_select_db (...);
    $result=mysql_query("select content from cc where id = 912842");
    $row=mysql_fetch_array($result);
    header('Content-Type: application/x-pdf');
    echo $row['content'];
    ?>
    I hope that helps!
    Last edited by Seanie; 18-08-2009 at 01:31 PM. Reason: left a useless line in the code

Page 1 of 2 1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to insert song to your site, i use Joomla CMS
    By blackmachine in forum Website Design
    Replies: 2
    Last Post: 14-01-2008, 10:37 AM
  2. insert php in html
    By sloth in forum Website Programming
    Replies: 7
    Last Post: 26-08-2005, 07:34 PM
  3. insert data into multiple table
    By azmaliya in forum Website Programming
    Replies: 18
    Last Post: 06-08-2005, 11:02 AM
  4. XML : macam mana nak insert new record?
    By therion in forum Website Design
    Replies: 21
    Last Post: 23-04-2005, 09:20 AM
  5. insert multiple data
    By vash in forum Website Programming
    Replies: 4
    Last Post: 28-04-2004, 02:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32