tags:

views:

130

answers:

3

I have this code:

<?php
session_start();
$cmd=$_GET["cmd"];
$con = mysql_connect("localhost","root","geheim");
if(!con)
{
die('Connection failed because of' .mysql_error());
}
mysql_select_db("ebay",$con);
if($cmd=="GetEmployee")
{
sleep(10);
echo "<table border='1' width='100%'>
<tr>
<th>test1</th>
<th>test2</th>
<th>test3</th>
</tr>";
$sql="select * from tblAuction";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['SELLER_ID']."</td>
<td>".$row['ACCESSSTARTS']."</td>
<td>".$row['ARTICLE_NAME']."</td>
</tr>";
}
echo "</table>";
}
mysql_close($con);
?>

Which returns

Undefined index: cmd in C:\Programme\EasyPHP 2.0b1\www\get_employee.php on line 3

Notice: Use of undefined constant con - assumed 'con' in C:\Programme\EasyPHP 2.0b1\www\get_employee.php on line 5

And I am not exactly sure why.

+1  A: 

$_GET["cmd"] is unavailable in your array. Try this instead:

if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"];
else
  die("You should have a 'cmd' parameter in your URL");

Replace

if (!con)

By

if (!$con)
vIceBerg
+1  A: 

It's this:

if(!con)

It should be:

if(!$con)

It happens to us all...

philistyne
+4  A: 

The undefined index is because $_GET['cmd'] doesn't exist. You can get around it by using isset:

$cmd = 'some default value';
if (isset($_GET['cmd'])) {
    $cmd = $_GET['cmd'];
}

// Or a short form
$cmd = isset($_GET['cmd']) ? $_GET['cmd'] : 'default value';

And you have

if (!con)

Which should be

if (!$con)

PHP doesn't know what 'con' is, so it assumes it's a constant (i.e., that you can define()), but it got confused because there is no such constant. So when you see this, you almost always have made a typo somewhere.

Christopher Nadeau