1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest Last month we upgraded BlackHatWorld.com to a new platform - . If you notice anything that requires attention please start a new thread here.
    Dismiss Notice

variable not defined.

Discussion in 'PHP & Perl' started by captchadreams, Jan 10, 2013.

  1. captchadreams

    captchadreams Power Member

    Joined:
    Sep 19, 2008
    Messages:
    505
    Likes Received:
    126
    I found a tutorial that I was messing around with while trying to learn php and mysql and I've received an error on the last part of the script where it's suppose to update some table data. The error said that the variable wasn't defined.. when looking at the last script file.. it looks like he really didn't define the variable in his tutorial.. but I'm also new and could be wrong.

    here it is:
    Code:
    <?php
    $host="localhost"; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="test_mysql"; // Table name 
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // update data in mysql database 
    $sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
    $result=mysql_query($sql);
    
    // if successfully updated. 
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='list_records.php'>View result</a>";
    }
    
    else {
    echo "ERROR";
    }
    
    ?>
    this is the part where I get the error from: $sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";

    I'm wondering if someone can take a look at this tutorial and see if he actually didn't define the variable himself.

    http://www.phpeasystep.com/mysql/9.html

    if so.. if anyone would be willing to rewrite the end of that code for me, it would be much appreciated.
     
  2. captchadreams

    captchadreams Power Member

    Joined:
    Sep 19, 2008
    Messages:
    505
    Likes Received:
    126
    nevermind I figured it out.. I looked up the right way to send variables via post method and just created the variables at the beginning of the code.

    $id=$_POST["id"];

    etc
     
  3. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    707
    Location:
    Eastern North Carolina
    Two things:

    1) Do not use MySQL queries like that otherwise you should expect someone to delete or modify your database. Also, it's going to be removed in the next PHP version. Look at using phps MySQLI interface and prepared statements. Tons more secure.
    2) Consider trying to put the actual image of Ducreux in your sig. Lulz to be had.
     
    • Thanks Thanks x 1
  4. captchadreams

    captchadreams Power Member

    Joined:
    Sep 19, 2008
    Messages:
    505
    Likes Received:
    126
    Yeah I've noticed a lot of the tutorials I attempt searching on google and finding seem to be outdated.. which is very frustrating. I've been looking at random courses online like (udemy.com). I want to learn PHP/MySQL and gain a better understanding of HTML/CSS. I'm wondering if you or anyone else has any suggestions of where I could learn this online. Through courses/tutorials/etc.

    2) lol.. yeah, that meme is great.. but I hate large sigs.
     
  5. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    707
    Location:
    Eastern North Carolina
    nettuts+ is a decent place to learn. Tutorials on many different things.

    Usually if I need to brush up on something I just google for it and do the first page of results. But the best way to learn is to do. Decide on a project and make it. Then once you're done do another or redo it again with better methods. Once you think you're good enough you can do work on open source platforms (ex: SMF, vbulletin, wordpress, joomla, prestashop, opencart) and release mods for it for review.
     
    • Thanks Thanks x 2
  6. alias_unknown

    alias_unknown Newbie

    Joined:
    Dec 30, 2012
    Messages:
    41
    Likes Received:
    4
    Yes if working with databases in PHP, it's now best practice to use PDO, otherwise you risk SQL injection.

    When looking for PHP tutorials, search in Google and then use an advanced search to show recent pages, his way you can find tutorials that are up to date and use the latest practices.
     
    • Thanks Thanks x 1
  7. digitaluser

    digitaluser Junior Member

    Joined:
    Apr 6, 2012
    Messages:
    116
    Likes Received:
    3
    You can use even isset function to check whether variable is set or not it might be help you to get better idea of the variable values