Solved

problems in checkbox form and calculation from MySQL database

Posted on 2009-03-30
14
305 Views
Last Modified: 2013-12-12
there are 2 pages to calculate the equation :
$result =(($m1*$w1)+($m2*$w2)+($m3*$w3)+($m4*$w4)+($m5*$w5))/($w1+$w2+$w3+$w4+$w5)
from the table "oee"
CREATE TABLE OEE (
machineno varchar(20) NOT NULL,
oee char(10) NOT NULL
)
ENGINE=MyISAM DEFAULT CHARSET=utf8;

i want to get the oee value from the table "oee" of the coresponding machineno and store them into m1 , m2, m3, m4 , m5 if the user tick the checkbox

and let the user to input the weighting w1 w2 w3 w4 if the user tick the checkbox

but i find that there is error of


checkbox form
<?php
 
		$con = mysql_connect(localhost, root, 1234567);
	if (!$con)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	mysql_select_db(final)or die("cannot select DB");
 
$sql  = "Select max(case when machineno = 'machine1' then oee end) as m1 
, max(case when machineno = 'machine2' then oee end) as m2
, max(case when machineno = 'machine3' then oee end) as m3 
, max(case when machineno = 'machine4' then oee end) as m4
, max(case when machineno = 'machine5' then oee end) as m5 
from oee where machineno in ( 'machine1' , 'machine2', 'machine3' , 'machine4'  , 'machine5'  ) ";
 $result = mysql_query($sql, $con) or die(mysql_error()); 
 $row = mysql_fetch_assoc($result); 
 $m1 = $result["m1"];
 $m2 = $result["m2"];
 $m3 = $result["m3"];
 $m4 = $result["m4"];
 $m5 = $result["m5"];
?>
 
<form action="caloeeave1.php" method="POST">
 
<input type="checkbox" name="m1" value="$m1""/> Machine1
<input type="text" name="userinput1"/>
<input type="hidden" name="w1" value="$_Post["userinput1"]"/>
 
<input type="checkbox" name="m2" value="$m2""/> Machine 2
<input type="text" name="userinput2"/>
<input type="hidden" name="w2" value="$_Post["userinput2"]"/>
 
 <input type="checkbox" name="m3" value="$m3""/> Machine 3
<input type="text" name="userinput3"/>
<input type="hidden" name="w3" value="$_Post["userinput3"]"/>
 
<input type="checkbox" name="m4" value="$m4""/> Machine 4
<input type="text" name="userinput4"/>
<input type="hidden" name="w4" value="$_Post["userinput4"]"/>
 
<input type="checkbox" name="m5" value="$m5""/> Machine 5
<input type="text" name="userinput5"/>
<input type="hidden" name="w5" value="$_Post["userinput5"]"/>
 
 
<input type="submit" name="submit" value="  Calculate  " />
 
</form>
 
calculation php 
<?php
if (!empty($_POST["_submit"])) {
   echo "<pre>"; var_dump($_POST); echo "</pre> \n";
}
 
 
//set all zero for machine and weight
$m1 = 0;$m2 = 0;$m3 = 0;$m4 = 0;$m5 = 0;
$w1 = 0;$w2 = 0;$w3 = 0;$w4 = 0;$w5 = 0;
 
//check if the checkbox was checked, if so, get the values
if (isset($_POST['m1'])) 
{
  $m1 = $_POST['m1'];
  $w1 = $_POST['w1'];
}
 
if (isset($_POST['m2'])) 
{
  $m2 = $_POST['m2'];
  $w2 = $_POST['w2'];
}
 
if (isset($_POST['m3'])) 
{
  $m3 = $_POST['m3'];
  $w3 = $_POST['w3'];
}
 
if (isset($_POST['m4'])) 
{
  $m4 = $_POST['m4'];
  $w4 = $_POST['w4'];
}
 
 
if (isset($_POST['m5'])) 
{
  $m5 = $_POST['m5'];
  $w5 = $_POST['w5'];
}
 
//do calculations; since they are set to zero initially you can do one mass calculation as 0+0 will not affect the calc and thus you //don't need to worry about what they did or didn't pick.
$result =(($m1*$w1)+($m2*$w2)+($m3*$w3)+($m4*$w4)+($m5*$w5))/($w1+$w2+$w3+$w4+$w5)
 
 ?>

Open in new window

0
Comment
Question by:hihialan2009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
14 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24023375
Please put error_reporting(E_ALL) into the to of your scripts.  You want to see all the errors at this point in the application debugging cycle.

I see you are using the variable $result in an inconsistent manner in two places - might want to check that.

Note that PHP variables are case sensitive.  Where does the data value for this statement come from?
<input type="hidden" name="w1" value="$_Post["userinput1"]"/>

Can you please describe the SQL statement in plain language so I can understand its intent.  Also, please show us what is in $row at line 18 (use var_dump() to do that)

thanks.
0
 

Author Comment

by:hihialan2009
ID: 24024014
my logic is :
first , there is a table having 5 rows  2 column ( machineno and oee)
machine1    10---->oee
machine2    20
machine3    30
machine4    40
machine5    50

the value will be updated by another web pages ,

i want to have a page to let user tick the checkbox of each machine whatever they want
and then input the weighting (w1,w2....)

then have calculation of
$result =(($m1*$w1)+($m2*$w2)+($m3*$w3)+($m4*$w4)+($m5*$w5))/($w1+$w2+$w3+$w4+$w5)

<input type="hidden" name="w1" value="$_Post["userinput1"]"/>
<input type="text" name="userinput1"/>

i m wrong , i want to get the weighting that the user input in the text box , how to do it?

after use var_dump()
give this :
array(5) { ["m1"]=> string(10) "58.8652482" ["m2"]=> string(10) "49.6969696" ["m3"]=> string(2) "40" ["m4"]=> string(10) "21.2121212" ["m5"]=> string(2) "60" }

that mean i can store the oee from the table to m1 m2 m3......
but cant have calcuation

as it gives

Warning: Division by zero in C:\AppServ\www\caloeeave1.php on line 44
thy?
thanks






 
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24024445
OK, lets' try to simplify.  There is no need to use to a checkbox here.  If the machine weighting is empty, it will be the same as an empty checkbox -- not be used in any calculation.  I will post a script that shows how to get the information from the form page to the action script, where it will get processed.

I also recommend that you get this book:
http://www.sitepoint.com/books/phpmysql1/

It covers a lot of the basics and will give you a much better foundation in these issues.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:hihialan2009
ID: 24024588
i must need to use checkboxes here as my other pages  will update the table and let the user choose the column of machine they want to involve in the calculation

how can i use checkbox with the MySQL databases ?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24024597
A checkbox is an HTML form processing thing - it has nothing to do with a MySQL data base.

Is this a classroom assignment by any chance?
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24024621

<?php // RAY_temp_machine_weights.php
error_reporting(E_ALL);
echo "<pre>";
 
// THE SAMPLE DATA - MACHINES AND OOE
$machines["1"] = 10;
$machines["2"] = 20;
$machines["3"] = 30;
$machines["4"] = 40;
$machines["5"] = 50;
 
// DEFAULT VALUES FOR WEIGHTS
$weights["1"] = 0.0;
$weights["2"] = 0.0;
$weights["3"] = 0.0;
$weights["4"] = 0.0;
$weights["5"] = 0.0;
 
 
 
 
// IF THE CLIENT HAS POSTED THE FORM
if (!empty($_POST))
{
 
// COPY THE WEIGHTS FROM THE FORM INTO OUR VARIABLES
   $total_weight = 0.0;
   foreach ($_POST["weights"] as $num => $weight)
   {
      $weights["$num"] = (float)$weight;
      $total_weight    = $total_weight + $weights["$num"];
   }
 
// SEE THE WEIGHTS
// print_r($weights);
 
// IF NO WEIGHTS FROM CLIENT, ISSUE ERROR MESSAGE
   if ($total_weight == 0.0) die("NO MACHINE WEIGHTS FOUND IN FORM INPUT");
 
// ARITHMETIC TO GET WEIGHTED TOTAL AND AVERAGE
   $weighted_total = 0.0;
   foreach ($machines as $id => $ooe)
   {
      $weighted_total = $weighted_total + ((float)$ooe * $weights["$id"]);
   }
   $weighted_average = number_format($weighted_total / $total_weight ,2);
   print_r($weighted_average);
 
// END OF FORM PROCESSING
}
 
 
 
 
// PUT UP THE FORM TO RECEIVE INPUT
echo "<form method=\"post\">\n";
foreach ($machines as $id => $ooe)
{
   echo "<br/>MACHINE $id <input name=\"weights[$id]\" />\n";
}
echo "<br/><input type=\"submit\" /></form>\n";

Open in new window

0
 

Author Comment

by:hihialan2009
ID: 24024851
thanks~ now i dun use checkboxes~ but how can i get the value from the talbe but not the preset value?
thanks
 
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24024930
Did you install and test that code?  Please explain - I do not understand what value you want to get?
0
 

Author Comment

by:hihialan2009
ID: 24027709
it works ~
but i want to get the value from mysql database but not the present value
$machines["1"] = 10;
$machines["2"] = 20;
$machines["3"] = 30;
$machines["4"] = 40;
$machines["5"] = 50;
preset value?
$machines["1"] = 10;
$machines["2"] = 20;
$machines["3"] = 30;
$machines["4"] = 40;
$machines["5"] = 50;
 
i want to get the oee from table "oee' 
$sql  = "Select * from oee ";
$result = mysql_query($sql, $con) or die(mysql_error());
while ($row = mysql_fetch_assoc($result ))
{
  $machines[$row["machineno"]] = $row["oee"];
  echo "$machines[$row["machineno"]]";
}

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 24028805
Try something more like this so you can see what is in the data base.  You can use var_dump() on any variable - it is very useful for visualizing the data.
// CONSTRUCT A QUERY
$sql  = "SELECT machineno, oee FROM [WHATEVER YOUR TABLE NAME] ";
 
// RUN THE QUERY
$result = mysql_query($sql, $con) or die(mysql_error());
 
// ACCUMULATE THE RESULTS
$machines = array();
while ($row = mysql_fetch_assoc($result ))
{
// GET LOCAL VARIABLES FOR EASY CODING
   extract($row);
   $machines["$machineno"] = $oee;
}
 
// SEE THE RESULTS
var_dump($machines);

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24270108
angelIII:

I've got a bit of a dilemma with this question and also with the other one here:
PHP/PHP_Databases/Q_24273775.html?cid=238

I am willing to help and want to help.  At the same time I don't want to waste my time on questions that are only going to get abandoned and eventually deleted, resulting in no award of any points for my effort.  

If you look at these two questions you can see that we engaged in considerable dialog and, hopefully, the Asker learned at least something about the application issues - that is a good result for the Asker and for EE.  But if you look at it from my perspective, the "least-time-wasted" approach might be to just stop monitoring the question after the third or fourth round of back-and-forth dialog.  Certainly I can do that, but it would seem unfair to the many Askers who simply need (often for language reasons) a little help framing the question.

What guidance should we follow in dealing with the Limited Members who are obviously lost?  What do you do with this sort of issue?

Thanks and regards, ~Ray
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question