View Full Version : help!
franches
09-01-2004, 02:02 PM
hi, :confused:
could someone examine my code please. i really need help. the user will enter the id and computer station. On this code I will check if the id entered is valid, if not then text will display 'PIN number not valid' then it will go back to the form again and if valid the entered computer station will automatically be deleted so that the next user will only have to select the available computer station to be used.
when i tested the program it display 'PIN number not valid' but when i press the refresh button at the toolbar it still display the 'PIN number not valid' and also i noticed eventhough it's not valid the station is deleted from the list and the data is still inserted into the database and which is supposedly should not happen. only the valid info should be inserted into the database.
hope you'll help me. i am trying my best but i really can't figure out what's wrong with my code.
thanks a lot!
Here's my code...
if($submit){
if ($PIC){
//set up the query
$query = "SELECT * FROM staff WHERE PIC='$PIC'";
//run the query and get the number of affected rows
$result = mysql_query($query) or die('error making query');
$affected_rows = mysql_num_rows($result);
//if there's exactly no result, the user is not validated.
if($affected_rows == 0) {
print 'PIN number not valid';
}
}
if ($Station) {
//set up the query
mysql_query("DELETE FROM workstation WHERE station='$Station'");
}
$sql="INSERT INTO borrow(PIC,Station ) VALUES ('$PIC','$Station')";
$result=mysql_query($sql);
}
?>
trib4lmaniac
09-01-2004, 02:12 PM
Please edit your post and encapsulate your code with php tags. Also use a more descriptive title next time.
franches
09-01-2004, 02:59 PM
I'm having problem with my code. whenever i enter an invalid id it displays the message but the data is also inserted into the database and the station entered is deleted from the combo box which is not correct. Another problem is whenever I press the refresh button at the toolbar the message should not be displayed with the form.
The objective of this code is it should simply accepts the data from the user if the id is valid. then it deletes the computer station chosen from combo box so that the other users will only see the recent available stations. if not valid it should display a message and display the form again.
please help me.
here's my code again.
<?php
if($submit){
if ($PIC){
//set up the query
$query ="SELECT * FROM staff WHERE PIC='$PIC'";
//run the query and get the number of affected rows
$result = mysql_query($query) or die('error making query');
$affected_rows = mysql_num_rows($result);
//if there's exactly no result, the user is not validated.
if($affected_rows == 0) {
print 'PIN number not valid';
}
}
if ($Station) {
//set up the query
mysql_query("DELETE FROM workstation WHERE station='$Station'");
}
$sql="INSERT INTO borrow(PIC,Station ) VALUES ('$PIC','$Station')";
$result=mysql_query($sql);
}
?>
marek_mar
09-01-2004, 08:15 PM
I've cleared up the code and added some tiny stuff:<?php
if(isset($submit))
{ // Probably should be $_POST['submit']
if(isset($PIC))
{
//set up the query
$query ='SELECT * FROM staff WHERE PIC="' . $PIC . '"';
//run the query and get the number of affected rows
$result = mysql_query($query) or die('error making query: ' . mysql_error());
/*$affected_rows = mysql_num_rows($result); Useless var. */
//if there's exactly no result, the user is not validated.
if(mysql_num_rows($result) == 0)
{
print 'PIN number not valid';
}
}
if(isset($Station))
{
//set up the query
$query = 'DELETE FROM workstation WHERE station="' . $Station . '"';
mysql_query($query) or die('error making query: ' . mysql_error()); // Let's stick to one way of querying.
}
$query = 'INSERT INTO borrow(PIC, Station) VALUES ("' . $PIC . '", "' . $Station . '")';
mysql_query($query) or die('error making query: ' . mysql_error()); // This does not have to be assigned to a var since it returns true/false if anything
}
?>
Do you get any error messages?
How and when are the variables $submit, $PIC, $Station set?
franches
09-02-2004, 07:24 AM
thanks, i tried the code the output is ok when the id is valid but when the id is not valid it did display the message but the data is still inserted into the database which should not happen.
hope you'll help me with this. this is what i'm assigned to do. i'm really new to php and esp. with programming but i really want to learn.
here's the code for borrowingform.php
<?php
$db=mysql_connect("localhost","root");
mysql_select_db("mydatabase",$db);
if(isset($submit))
{ // Probably should be $_POST['submit']
if(isset($PIC))
{
//set up the query
$query ='SELECT * FROM staff WHERE PIC="' . $PIC . '"';
//run the query and get the number of affected rows
$result = mysql_query($query) or die('error making query: ' . mysql_error());
/*$affected_rows = mysql_num_rows($result); Useless var. */
//if there's exactly no result, the user is not validated.
if(mysql_num_rows($result) == 0)
{
print 'PIN number not valid';
}
}
if(isset($Station))
{
//set up the query
$query = 'DELETE FROM workstation WHERE station="' . $Station . '"';
mysql_query($query) or die('error making query: ' . mysql_error()); // Let's stick to one way of querying.
}
$query = 'INSERT INTO borrow(PIC, Station) VALUES ("' . $PIC . '", "' . $Station . '")';
mysql_query($query) or die('error making query: ' . mysql_error()); // This does not have to be assigned to a var since it returns true/false if anything
}
?>
<table boarder=0><tr><td align=right>
<font size=4 face=arial color=#330066>
<form method="post" action="<?php echo $PHP_SELF?>">
Borrower: <input type="text" name="PIC"><Br><Br>
Work Station:
<?
include ("stations.inc");
?>
<br><br>
<input type="submit" name="submit" value="add to database">
</form>
</td></tr></table>
here's the code for stations.inc
<?php
$db=mysql_connect("localhost","root");
mysql_select_db("mydatabase",$db);
$tablename = "workstation";
$query = "SELECT * FROM $tablename";
$result = mysql_query($query) or die(mysql_error());
echo "<select name=\"Station\" size=\"1\">";
while($row = mysql_fetch_array($result))
{
echo "<option>".$row['station'];
}
echo "</select>";
?>
marek_mar
09-02-2004, 05:44 PM
Well the firs thing I noticed. It might be the problem
if(isset($Station))
{
//set up the query
$query = 'DELETE FROM workstation WHERE station="' . $Station . '"';
mysql_query($query) or die('error making query: ' . mysql_error()); // Let's stick to one way of querying.
}
$query = 'INSERT INTO borrow(PIC, Station) VALUES ("' . $PIC . '", "' . $Station . '")';
mysql_query($query) or die('error making query: ' . mysql_error()); // This does not have to be assigned to a var since it returns true/false if anything
Written in a more normal language.
IF $Station contains any value THEN DELETE the record WHERE station is equal to $Station.
This means whatever you select in the select box will get deleted.
After the IF statement
INSERT some new record with the values $PIC and $Station
This will always get inserted. If the user selects anything in the select box or not.
One more thing. The second IF statement doies not exclude the third IF statement.
That would be a logic error. I still don't get what it is suppose to do so I can't fix it. But I'm sure it will be easy for you to do it yourself :thumbsup: .
Not using $_POST, $_GET arrays is insecure.
franches
09-03-2004, 08:49 AM
thanks a lot i should have made the if ($station) as the else of if ($submit). did i say it right? anyways, only after the station is check if valid then it should delete the station. I'm happy i figured it out. thanks for the time. :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.