Diving into PHP: Day 11

Continuing on from Day 10, we’ll review SQL insert statements that will allow us to add new rows to our MySql database with PHP. Additionally, we’ll take a look at using foreach statements to loop through an array.

Day 11: SQL Insert Statements

Be sure to click on the “Full Screen Toggle”.

Download the Source Code



36

Comments
  • Devin says:

    You messed up on net.tutsplus.com , when i was making a screencast I had the same problem. But nice tut.

  • M.A.Yoosuf says:

    :) , nice stuff, your Tweeter-deck is Screaming

  • Another great tutorial. I was worried you were going to forget to validate/sanitize the input, and was relieved when you mentioned it :)

  • Jeffrey says:

    @Shane – Yeah…that deserves a whole tutorial on its own.

  • M.A.Yoosuf says:

    completed watching. BTW why did u added jQuery for first, and in the ending of it you didn’t use it :S

    but some data radiancy is there,

    Overall Comment is its a good tutorial and Demo for Beginners.

    Finally Suggestion, Why dont you make this series as, Diving In to PHP, “for beginners” as slogan.

  • Jeffrey says:

    @M.A. – The jQuery was just an example of what’s possible using a text expander program.

    “Diving into PHP” implies that it’s for beginners.

  • Pix says:

    as always
    thansk jeff!

  • ciprian says:

    another grate tutorial Jeff. thank you.

  • Trond says:

    Great tutorial!

    just one question, what is the differense between
    if($_POST['submit'])
    and
    if(!empty($_POST['submit'])) ?

  • Jeffrey says:

    Trond,

    Look over the first example here:

    http://us2.php.net/empty

    We’re making sure that the array isn’t empty.

  • Bjorn says:

    You’re turning these out faster than I can watch them now! Thank you in advance, I guess!

  • Srigi says:

    2Jeff >> if you are satisfied with Notepad++, try best filemanager in editor I found – Explorer (downloadable on N++ homepage).

  • krike says:

    if($_POST['submit']) -> you read it as when the submit button gives true, so when someone clicks on the submit button with name submit

    if(!empty($_POST['submit'])) -> you read it as when the submit button is not empty, the ! before empty is a negation so “if NOT empty” then enter the if-lus.

    but both will give you the same result if I’m not wrong, however I recommend the second one, it’s cleaner checking with !empty()

  • Mark says:

    Jeff, thanks for taking this step by step, really helps for designers like me trying to learn programming. Can I just check so I can get my brain around it, that when you set the new mysqli your using a class that’s built into php in much the same way as you use functions that are built in, and then you have access to the functions or i believe they are called methods that are in that class. Thanks

  • Randy says:

    These are fantastic! I was thinking these were going to be on nettuts for some reason and lost track of them. I just watched all installments to catch up! You are a fantastic teacher. Even the stuff I already knew has been great to have re-explained so well. Your on my iGoogle RSS so I’ll never miss another tut!!!
    Thanks!

  • Fawaz says:

    Hey, I watched the Tuts + tutorial of this week and It didn’t say anything about those shortcut that you were talking about.

  • Tom says:

    Hey Jeff, I want to let you know, about Some guy and some girl problem…
    It is because you are first trying to catch data, and the POST thing is done after that, so when the content is displayed, it doesn’t include your last input. Did you get it?

    So easy, but recommendable to put the SELECT query after the INSERT query…

  • Trond says:

    Jeffrey:
    I know what the !empty() does, i just think it’s faster to write just if($_POST['submit']).. As far as I know, in this case it doesn’t matter what you use..

    Krike:
    yes, that’s what i ment. But it think it’s faster to write the first one. But i guess it’s just up to yourself to choose which method to use :)

  • Jeffrey says:

    @Trond – I suppose it comes down to readability.

  • Jeffrey says:

    @Mark- Exactly! Mysqli is a built in PHP class which contains a bunch of methods . We’ll go over some of them in the next few weeks.

    Remember – “method” is just a fancy term for “function”. Place a function inside of a class, and it becomes a “method”. :)

  • Meshach says:

    Thanks very very very much Jeffrey, absolutely awesome!!

  • nawabz says:

    this is sooooooooooooooo helpfull. you know what i will subscribe

  • Dorin says:

    Good explanation! Pleasure of understanding.

  • Jeraldo says:

    Nice tutorials out there. Im using Php for my school project and its a job posting website. I wonder if you have a non-video tutorial of this. A book maybe or a textual tutorial. Im suffering from a slow connection. Crappy ISPs in the Philippines.

  • steve says:

    validate/sanitize tutorial??? what happened to it? :)
    2 more lessons have been release whilst this one had been on hold. id love to implement what was taught in this lesson, however concerned about database integrity without sanitizing the input…..
    any expected release Jeff on the sanitization tutorial????

  • Maxime says:

    Is it possible to talk about BLOB? Or just give an example?
    Thanks a lot for the tutorial!!!

    greetings from Belgium
    Max

  • Chris says:

    I’m shocked that there was no mention of PDO. In any case, I really like the tutorials. I hope we get into some more advanced stuff like OOP & Zend Framework.

  • wrerm says:

    Jeff did your page update automatically when you clicked “Submit” ????

  • Alex says:

    Can you show teach us how to email form data along with an uploaded file? I have looked around some forums for an answer but they just tell you to use the free phpmailer. This of course doesn’t teach me how the process actually works.

    I would love to see you do a tutorial on this and from the comments I’ve seen around in the forums I am confident others would too.

    Thanks again for your superb videos!

  • markj says:

    Can I just ask why I would get an error like this please? It happens even when I copy and paste your code.

    Fatal error: [] operator not supported for strings in /var/www/vhosts/mark.bluebit.co.uk/httpdocs/php/day11/index.php on line 9

  • Ray says:

    Guys,

    I have this strange thing going on, where everything seems to work great, yet when i press the submit button it returns the value i added in the textbox before the last entry.

    Example:

    I input ‘Some Guy’ -> Nothing happens
    I input ‘Some Girl’ -> it echoes ‘Some Guy’
    I input ‘Some Boy’ -> it echoes ‘Some Girl’

    etc.

    One could state it lags 1 name behind.

    We might have seen this happen in the above screencast at the end, where Jeffrey entered a value twice athe the end of the tut. But no one mentioned this so i might be wrong :)

    Any ideas? Thanks!!

    Ray

  • Michael says:

    Just a note Texter doesn’t seem to work correctly on windows 7

  • Giles says:

    Hi Jeffrey,

    Having a bit of trouble… I’m trying this out with more than one row, but I don’t know how to add more than one row to the array.

    $item[] = $firstName;

    That works fine, however the lastName row that I am using is the same for all of them. Would I do something like;

    $item[] = $firstName, $lastName;

    …so that I can echo both variables in my foreach() statement?

    Thanks for the help,
    Giles

  • jqueryninja says:

    @Ray – More than likely you figured this out a few weeks ago, but I believe your problem is that you were querying the database and building your “name array” before you processed the form. Because the insert into the database happens after you build your array (in your code), you are missing the last item. To solve simply move the if($result) code block after the if(!empty section of code.

    @Jeffrey – Not sure if you keep up on these comments after so many months, but I am very appreciative of this series. Keep up the great work on both Nettuts+ and themeforest.

  • whiz says:

    ray is right when u submit a name the prev one is shown on the list. beside u can submit empty fields – shouldnt we check if there is an output before we update the list?

  • fahrrad says:

    What about if a name already exist in the database and i want to get a error if the same name is submited again?