Jump to content

Why are 5 of my 7 Variables reporting as Undefined? I'm Lost......


Modernvox

Recommended Posts

My form


<form action="process.php" method="post">
<font face= "calibri" size= "4">
<table border= "1">

<tr style= "background: #cccccc">  <td><b>Item Description:</b></td>
<td><input type= "text" name= "itemDescription" size= "30" value=""/></td>
</tr>
<tr style= "background: #99ff00">   <td><b>Item Price:</b></td>
<td><input type= "text" name= "itemPrice" size= "5" value=""/></td>
</tr>
</tr style= "background: #A8E5FF">   <td><b>Winning Bidders:</b></td>
<td><input type="text" name= "winningBidder" size= "5" />
</td>
</tr>
<tr>   <td><b>How many deals?:</b></td>
<td><input type="text" name= "itemQty" size= "3" value= "1" /></td>
</tr>
</table>
<br/>
<input type="reset" value="Reset Form"><input type="submit" name="submit" value= "Save & Cont."
" />


</form></font>

 

 

My process script:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$host= "";
$db_name= "";
$db_user= "";
$db_password= "";


$logUser= $_POST['logUser'];
$deleteBidder = $_POST['deleteBidder'];
$newBidder= $_POST['newBidder'];
$itemDescription= $_POST['itemDescription'];
$itemPrice= $_POST['itemPrice'];
$winningBidder= $_POST['winningBidder'];
$itemQty= $_POST['itemQty'];
ob_start();

if ($_POST['newBidder']) {
$bidderId= $newBidder;

mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM bidders WHERE biddersId='$bidderId'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==0){
// Add $biddersId and redirect to anypage
mysql_Query("INSERT INTO bidders (biddersId)
VALUES ('$bidderId')");
header("Location: index.php");
exit();
  }
}

if ($_POST['deleteBidder'])
{

mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

mysql_query("DELETE FROM bidders WHERE biddersId='$deleteBidder'");
header("Location: index.php");
exit();
}

if ($_POST['itemDescription'] AND $_POST['itemPrice'] AND $_POST['winningBidder'] AND $_POST['itemQty'])
{


mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM transactions WHERE biddersId='$winningBidder'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
// If result matched, table row must be 1 row

if($count==0){
echo "That Bidder Number is NOT logged in, ";
echo "would you like to set this bidder as active?";
echo " Enter 1 for NO or 2 for YES";
echo "<form action= \"process.php\" method= \"POST\">";
echo "<input type =\"text\" name= \"logUser\"/>";
echo "<input type= \"submit\" value = \"Submit\"/>";
$logUser= $_POST['logUser'];
exit();
}
if ($logUser= 1) {
header("Location: inprogress.php");
exit();
}

else if ($logUser= 2){
// Add $biddersId and redirect to anypage
mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

mysql_Query("INSERT INTO bidders (biddersId)
VALUES ('$winningBidder')");


mysql_query("INSERT INTO transactions (itemDescription, itemPrice, bidderId, itemQty , totalPrice) 
VALUES('$itemDescription', '$itemPrice','$winningBidder', '$itemQty', '$totalPrice')")
or die(mysql_error());  
$_SESSION['itemDescription']='$itemDescription'; 
header("Location: index.php");
exit();
   }
}

echo "<font color= \"red\" face=\"calibri\" size=\"4\">That bidder is already logged, Please press your browsers back button and try again.</font>";


ob_end_flush();

?>

 

DeleteBidder and newBidder are the only variables working?

Link to comment
Share on other sites

Here's what my code id producing for an error

 

Notice: Undefined index: deleteBidder in C:\xampp\htdocs\process.php on line 44

 

Notice: Undefined index: newBidder in C:\xampp\htdocs\process.php on line 45

 

Notice: Undefined index: itemDescription in C:\xampp\htdocs\process.php on line 46

 

Notice: Undefined index: itemPrice in C:\xampp\htdocs\process.php on line 47

 

Notice: Undefined index: winningBidder in C:\xampp\htdocs\process.php on line 48

 

Notice: Undefined index: itemQty in C:\xampp\htdocs\process.php on line 49

 

Notice: Undefined index: newBidder in C:\xampp\htdocs\process.php on line 56

 

Notice: Undefined index: deleteBidder in C:\xampp\htdocs\process.php on line 78

 

Notice: Undefined index: itemDescription in C:\xampp\htdocs\process.php on line 89

That bidder is already logged, Please press your browsers back button and try again.

Link to comment
Share on other sites

Is that the right form? There's no field named deleteBidder on that form.

 

This isn't valid either:

 

<input type="submit" name="submit" value= "Save & Cont."
" />

 

Yeah..Fixed the <input type="submit" name="submit" value= "Save & Cont."

" />  crap, but still producing same errors

Link to comment
Share on other sites

Did you fix that HTML error I pointed out?

 

If you're using multiple forms and all of those variables won't always be set than you'll always get those errors with your current code. You should be doing something like this for all your variables:

 

$logUser= isset($_POST['logUser']) ? $_POST['logUser'] : '';

 

This probably isn't related to your current problem, but it is an issue with your code:

 

$_SESSION['itemDescription']='$itemDescription'; 

 

That will set $_SESSION['itemDescription'] to th literal string '$itemDescription' and not the value that the variable holds. Instead it should be:

 

$_SESSION['itemDescription'] = $itemDescription;

Link to comment
Share on other sites

These 2 forms work as expected:

<tr>    <td><font face= "calibri" size= "4"> Let's begin by adding bidders...</font></td>
</tr>
<Table border= "1">
        <form action="process.php" method="post">
        <tr style="background: #99cc33;">    <td><strong><font face= "calibri" style= "bold"> Bidders Id: </font></strong></td>
        <td><input type= "text" name= "newBidder"/></td>
</tr>
<tr>   <td><input type="Submit"  value="submit"></td>
</tr>
</table>

 

<tr>    <td><font face= "calibri" size= "4"> Here we can </font><font color= "red"> delete bidders.</font></td>
</tr>
<Table border= "1">
        <form action="process.php" method="post">
        <tr style="background: #ff6600;">    <td><strong><font face= "calibri" style= "bold"> Bidders Id: </font></strong></td>
        <td><input type= "text" name= "deleteBidder"/></td>
</tr>
<tr>   <td><input type="Submit"  value="submit"></td>
</tr>
</table>

Link to comment
Share on other sites

Did you fix that HTML error I pointed out?

 

If you're using multiple forms and all of those variables won't always be set than you'll always get those errors with your current code. You should be doing something like this for all your variables:

 

$logUser= isset($_POST['logUser']) ? $_POST['logUser'] : '';

 

This probably isn't related to your current problem, but it is an issue with your code:

 

$_SESSION['itemDescription']='$itemDescription'; 

 

That will set $_SESSION['itemDescription'] to th literal string '$itemDescription' and not the value that the variable holds. Instead it should be:

 

$_SESSION['itemDescription'] = $itemDescription;

 

 

So I should be using isset instead of just  if $_POST['whatever'] ?

Link to comment
Share on other sites

When using it to the set the value of variables like this:

 

$logUser= isset($_POST['logUser']) ? $_POST['logUser'] : '';

You wouldn't use multiple conditions. The purpose of that is to set $logUser to $_POST['logUser'] if it's set, otherwise set it equal to an empty string, ''.

 

For using isset in other contexts it does support multiple parameters and returns true if they're all set.

 

if(isset($_POST['something'], $_POST['somethingelse'])) {
    // ...
}

Link to comment
Share on other sites

When using it to the set the value of variables like this:

 

$logUser= isset($_POST['logUser']) ? $_POST['logUser'] : '';

You wouldn't use multiple conditions. The purpose of that is to set $logUser to $_POST['logUser'] if it's set, otherwise set it equal to an empty string, ''.

 

For using isset in other contexts it does support multiple parameters and returns true if they're all set.

 

if(isset($_POST['something'], $_POST['somethingelse'])) {
    // ...
}

 

 

Ok. So, i must do this for each of my variables I suppose> $logUser= isset($_POST['logUser']) ? $_POST['logUser'] : '';  ?

Link to comment
Share on other sites

You could still use isset on the $_POST variables.

 

if ($_POST['itemDescription'] AND $_POST['itemPrice'] AND $_POST['winningBidder'] AND $_POST['itemQty'])

Could be:

if (isset($_POST['itemDescription'], $_POST['itemPrice'], $_POST['winningBidder'], $_POST['itemQty']))

 

Optimally you shouldn't define all the variables at the top if you're not going to be using them every time the script runs. You should have them defined within the if statements that determine if they're going to be needed or not.

Link to comment
Share on other sites

You could still use isset on the $_POST variables.

 

if ($_POST['itemDescription'] AND $_POST['itemPrice'] AND $_POST['winningBidder'] AND $_POST['itemQty'])

Could be:

if (isset($_POST['itemDescription'], $_POST['itemPrice'], $_POST['winningBidder'], $_POST['itemQty']))

 

Optimally you shouldn't define all the variables at the top if you're not going to be using them every time the script runs. You should have them defined within the if statements that determine if they're going to be needed or not.

 

 

Even with all these fixes, I still get the same errors in that all my variables are undefined from that particular form..

I need a cheeseburger, i'm getting upset

Link to comment
Share on other sites

If variables from the form that you're submitting are not set then it's most likely a problem with your HTML. Make sure that is all correct. I noticed that in the two forms you posted you weren't closing them off, but I figured that you probably closed it the line after what you posted.

Link to comment
Share on other sites

After all fixes here's what we got for errors (smaller list:-)

Notice: Undefined index: deleteBidder in C:\xampp\htdocs\process.php on line 43

 

Notice: Undefined index: newBidder in C:\xampp\htdocs\process.php on line 44

 

Notice: Undefined index: newBidder in C:\xampp\htdocs\process.php on line 52

 

Notice: Undefined index: deleteBidder in C:\xampp\htdocs\process.php on line 74

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\process.php on line 98

Link to comment
Share on other sites

If variables from the form that you're submitting are not set then it's most likely a problem with your HTML. Make sure that is all correct. I noticed that in the two forms you posted you weren't closing them off, but I figured that you probably closed it the line after what you posted.

 

Yeah the two forms I didn't close off are the ONLY 2 working:-)

Link to comment
Share on other sites

Well they should be closed off.. did you close them off now?

 

Yes sir and here's what I have as of now for errors

 

Notice: Undefined index: newBidder in C:\xampp\htdocs\process.php on line 52

 

Notice: Undefined index: deleteBidder in C:\xampp\htdocs\process.php on line 75

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\process.php on line 99

That Bidder Number is NOT logged in, would you like to set this bidder as active? Enter 1 for NO or 2 for YES

Link to comment
Share on other sites

Ok this is the form that adds item description, price, qty and biddersid number.


<form action="process.php" method="post">
<font face= "calibri" size= "4">
<table border= "1">

<tr style= "background: #cccccc">  <td><b>Item Description:</b></td>
<td><input type= "text" name= "itemDescription" size= "30" value=""/></td>
</tr>
<tr style= "background: #99ff00">   <td><b>Item Price:</b></td>
<td><input type= "text" name= "itemPrice" size= "5" value=""/></td>
</tr>
</tr style= "background: #A8E5FF">   <td><b>Winning Bidders:</b></td>
<td><input type="text" name= "winningBidder" size= "5" /></td>
</tr>
<tr>   <td><b>How many deals?:</b></td>
<td><input type="text" name= "itemQty" size= "3" value= "1" /></td>
</tr>

<br/>
<input type="reset" value="Reset Form"><input type="submit" name="submit" value= "submit" />
</font>
</table>
</form>

 

These are the foms that add or delete a bidders ID located on a different page other than the of the itemdescription , etc form.

<tr>    <td><font face= "calibri" size= "4"> Let's begin by adding bidders...</font></td>
</tr>
<Table border= "1">
        <form action="process.php" method="post">
        <tr style="background: #99cc33;">    <td><strong><font face= "calibri" style= "bold"> Bidders Id: </font></strong></td>
        <td><input type= "text" name= "newBidder"/></td>
</tr>
<tr>   <td><input type="Submit"  value="submit"></td>
</tr>
</table>
</form>

 


<tr>    <td><font face= "calibri" size= "4"> Here we can </font><font color= "red"> delete bidders.</font></td>
</tr>
<Table border= "1">
        <form action="process.php" method="post">
        <tr style="background: #ff6600;">    <td><strong><font face= "calibri" style= "bold"> Bidders Id: </font></strong></td>
        <td><input type= "text" name= "deleteBidder"/></td>
</tr>
<tr>   <td><input type="Submit"  value="submit"></td>
</tr>
</table>
</form>

 

Still don't understand why the script tells me delete and newbidder are undefined when I am not submitting those forms??

 

Link to comment
Share on other sites

Oops and here's the process.php page for all the forms.

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$host= "";
$db_name= "";
$db_user= "";
$db_password= "";

ob_start();
if ($_POST['newBidder']) {
$newBidder= isset($_POST['newBidder']) ? $_POST['newBidder'] : '';
$bidderId= $newBidder;

mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM bidders WHERE biddersId='$bidderId'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==0){
// Add $biddersId and redirect to anypage
mysql_Query("INSERT INTO bidders (biddersId)
VALUES ('$bidderId')");
header("Location: index.php");
exit();
  }
}

if ($_POST['deleteBidder'])
{
$deleteBidder= isset($_POST['deleteBidder']) ? $_POST['deleteBidder'] : '';
mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

mysql_query("DELETE FROM bidders WHERE biddersId='$deleteBidder'");
header("Location: index.php");
exit();
}

if (isset($_POST['itemDescription'], $_POST['itemPrice'], $_POST['winningBidder'], $_POST['itemQty']))
{
$itemDescription= isset($_POST['itemDescription']) ? $_POST['itemDescription'] : '';
$itemPrice= isset($_POST['itemPrice']) ? $_POST['itemPrice'] : '';
$winningBidder= isset($_POST['winningBidder']) ? $_POST['winningBidder'] : '';
$itemQty= isset($_POST['itemQty']) ? $_POST['itemQty'] : '';

mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM transactions WHERE biddersId='$winningBidder'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
// If result matched, table row must be 1 row

if($count==0){
echo "That Bidder Number is NOT logged in, ";
echo "would you like to set this bidder as active?";
echo " Enter 1 for NO or 2 for YES";
echo "<form action= \"process.php\" method= \"POST\">";
echo "<input type =\"text\" name= \"logUser\"/>";
echo "<input type= \"submit\" value = \"Submit\"/>";
$logUser= isset($_POST['logUser']) ? $_POST['logUser'] : '';
exit();
}
if ($logUser= 1) {
header("Location: inprogress.php");
exit();
}

else if ($logUser= 2){
// Add $biddersId and redirect to anypage
mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

mysql_Query("INSERT INTO bidders (biddersId)
VALUES ('$winningBidder')");


mysql_query("INSERT INTO transactions (itemDescription, itemPrice, bidderId, itemQty , totalPrice) 
VALUES('$itemDescription', '$itemPrice','$winningBidder', '$itemQty', '$totalPrice')")
or die(mysql_error());   
header("Location: index.php");
exit();
   }
}

echo "<font color= \"red\" face=\"calibri\" size=\"4\">That bidder is already logged, Please press your browsers back button and try again.</font>";


ob_end_flush();

?>

Link to comment
Share on other sites

 

Still don't understand why the script tells me delete and newbidder are undefined when I am not submitting those forms??

 

Because even though you're not submitting that form the code is getting a point where it's using a variable that is not defined (obviously). Just make sure that before you use a variable from an outside source that might not be set you make sure that it is using isset.

 

You must've edited that code because the errors you posted can't be happening on those lines in the code provided.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.