variable not defined.

captchadreams

Power Member
Joined
Sep 19, 2008
Messages
509
Reaction score
133
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.
 
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
 
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.
 
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.
 
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.
 
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.
 
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
 
Back
Top