Jump to content

Problem running correct function based on previous entry


Recommended Posts

Well, I have 2 pages. InsertItem.php and InsertSupplier.php.

 

InsertItem.php has 4 inputs, ItemType, ItemPrice, ItemDetails and SupplierName

InsertSupplier.php has 3 inputs, SupplierName, Delivery and Reliability.

 

The idea is,you input a supplier, then you can load all his items into the database, by adding its type(dropdown choice between 4 set types), price(text input), details(textarea) and then selecting the supplier from the dropdown, which gives you a choice of all the suppliers.

 

I have this bit working.

 

But, when you submit either the InsertItem form, or the InsertSupplier form, I am trying to use Input2.php to figure out which form they submitted, by using a hidden input, with value of 1 on InsertItem, and 2 on InsertSupplier. Then Input2.php checks this, and runs the correct function. Or that is what it's supposed to do anyway, but I have 2 problems.

 

Firstly, I cannot get it to run a function at all, and secondly, it gives me errors for the variables that didnt get filled in (for instance ItemPrice doesnt get filled in on InsertSupplier.php, so Input2.php returns this as an error).

 

I will include the code for all 3 pages, in the hope someone can see what is wrong!

 

InsertItem.php-

<form action="Input2.php" method="POST">
<b>Item Type</b>:<br><select name="ItemType">
                  	<option value="Screw">Screw</option>
				<option value="Nail">Nail</option>
				<option value="Washer">Washer</option>
				<option value="Nut">Nut</option>
                  </select>
<br><br>
<b>Item Price</b>(Per Single Unit):<br>£<input type="Text" name="ItemPrice">
<br><br>
<b>Item Details</b><br><textarea height="5" width="30" name="ItemDetails">Input Item Details Here</textarea><br><br>

<b>Supplier</b>:<br>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Construction", $con);

$graball = mysql_query("SELECT * FROM supplier");

echo "<select name='supplier'>";
while($row = mysql_fetch_array($graball))
{
  $content = $row['SupplierName'];
  echo "<option value='$content'>";
  echo $content;
  echo "</option>";

  echo $row['SupplierName'];
  echo "</option>";
}

echo "</select>";


?>
<br><br>
<input type="hidden" value="1" name="PageCheck">
<input type="submit" value="Submit New Item">
</form>

 

InsertSupplier.php-

<form action="Input2.php" method="POST">
<b>Supplier Name</b>:<br><input type="Text" name="SupplierName"><br><br>
<b>Delivery Time</b>(Within UK):<br><input type="text" name="Delivery"><br><br>
<b>Reliability Rating</b>:<br><input type="Text" name="Reliability"><br><br>
<input type="hidden" value="2" name="PageCheck">
<input type="submit" value="Submit New Supplier">
</form>

 

Input2.php

   <?php
   
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Construction", $con);

$ItemType=$_POST['ItemType'];
$ItemPrice=$_POST['ItemPrice'];
$ItemDetails=$_POST['ItemDetails'];
$SupplierName=$_POST['SupplierName'];
$Delivery=$_POST['Delivery'];
$Reliability=$_POST['Reliability'];

$PageCheck=$_POST['PageCheck'];

// check which insert has just been used 
if ($PageCheck="2")
{
InsSupp(); // Supplier has just been inserted, run Supplier insert query
}
else InsItem();	// Item has just been inserted, run Item insert query


function InsSupp()	
{
$insSupp = mysql_query("INSERT INTO supplier (SupplierName,Delivery,Reliability) VALUES ('$SupplierName','$Delivery','$Reliability')";
echo "Ran Supplier Insert";
}

function InsItem()
{
$insSupp = mysql_query("INSERT INTO item (ItemType,ItemPrice,ItemDetails,SupplierName) VALUES ('$ItemType','$ItemPrice','$ItemDetails,'$SupplierName')";
echo "Ran Item Insert";
}

?>

 

As you can see the functions, I can't get those echo's to print, nor will the insert's work.

 

Any ideas?

you are missing a ) at the end of each of them:

$insSupp = mysql_query("INSERT INTO supplier (SupplierName,Delivery,Reliability) VALUES ('$SupplierName','$Delivery','$Reliability')";

should be

$insSupp = mysql_query("INSERT INTO supplier (SupplierName,Delivery,Reliability) VALUES ('$SupplierName','$Delivery','$Reliability')") or die(mysql_error());

 

edit: i also added mysql_error() for debugging

Thanks for that, seems to have helped some.

 

Now, I have split the variables now to this - (has got rid of the errors somewhat)

 

function InsSupp()	
{
$SupplierName=$_POST['SupplierName'];
$Delivery=$_POST['Delivery'];
$Reliability=$_POST['Reliability'];
$insSupp = mysql_query("INSERT INTO supplier (SupplierName,Delivery,Reliability) VALUES ('$SupplierName','$Delivery','$Reliability')");
echo "Ran Supplier Insert";
}

function InsItem()
{
$ItemType=$_POST['ItemType'];
$ItemPrice=$_POST['ItemPrice'];
$ItemDetails=$_POST['ItemDetails'];
$SupplierName=$_POST['SupplierName'];
$insSupp = mysql_query("INSERT INTO item (ItemType,ItemPrice,ItemDetails,SupplierName) VALUES ('$ItemType','$ItemPrice','$ItemDetails,'$SupplierName')");
echo "Ran Item Insert";
}

 

Now, I can fun the first function (InsSupp) perfectly. It works, with no errors and inserts into the database, and I can select it on the InsertItem page. But, after trying to insert an item with InsertItem.php, it tries to run the InsSupp function, returning these errors -

 

Notice: Undefined index: SupplierName in C:\wamp\www\Construction\Input2.php on line 60

Notice: Undefined index: Delivery in C:\wamp\www\Construction\Input2.php on line 61

Notice: Undefined index: Reliability in C:\wamp\www\Construction\Input2.php on line 62
Ran Supplier Insert

 

Now, those lines (60,61,62) are-

 

$SupplierName=$_POST['SupplierName'];
$Delivery=$_POST['Delivery'];
$Reliability=$_POST['Reliability'];

 

All 3 of which are from the wrong function. They are all in the InsSupp function, so it's trying to run both. Suggests a problem with the calling functions, or syntax of that.

 

Here is the page again, after update -

 

   <?php
   
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Construction", $con);


$PageCheck=$_POST['PageCheck'];

// check which insert has just been used 
if ($PageCheck="2")
{
InsSupp(); // Supplier has just been inserted, run Supplier insert query
}
else InsItem();	// Item has just been inserted, run Item insert query


function InsSupp()	
{
$SupplierName=$_POST['SupplierName'];
$Delivery=$_POST['Delivery'];
$Reliability=$_POST['Reliability'];
$insSupp = mysql_query("INSERT INTO supplier (SupplierName,Delivery,Reliability) VALUES ('$SupplierName','$Delivery','$Reliability')");
echo "Ran Supplier Insert";
}

function InsItem()
{
$ItemType=$_POST['ItemType'];
$ItemPrice=$_POST['ItemPrice'];
$ItemDetails=$_POST['ItemDetails'];
$SupplierName=$_POST['SupplierName'];
$insSupp = mysql_query("INSERT INTO item (ItemType,ItemPrice,ItemDetails,SupplierName) VALUES ('$ItemType','$ItemPrice','$ItemDetails,'$SupplierName')");
echo "Ran Item Insert";
}


$con = mysql_connect("localhost","root","");
mysql_select_db("Construction", $con);


?>

 

If anyone can see a problem, please point it out :)

 

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.