Hi all. It's been a while since I last worked with PHP. Hope someone can help me with this, because a few things aren't working :(

First thing first. I'm doing an expense tracking system. User will be able to track their spendings according to month and year. Since it's not real time, I've put in codes to generate random number (for item price) to be inserted into the database.

There wasn't any problem inserting until I start putting in extra conditions.

Here is the code for the interface; the random number is generated when 'Submit' button pressed.

<?php
session_start();
if(isset($_SESSION['name']) && ($_SESSION['ic']))
{
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#CCCCCC">
<form id="form1" name="form1" method="post" action="Untitled-8.php">
  <table width="830" border="0" align="center" bgcolor="#FFFFFF">
    <tr>
      <td><img src="img/header.jpg" width="830" height="140" /></td>
    </tr>
    <tr bgcolor="#6699CC">
      <td><div align="right"><a href="user_mainpage.php">Home</a> | <a href="logout0.php">Logout</a> </div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="center"> </div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="center">
        <label></label>
        <div align="center">
          <h1>VIEW EXPENSES BY MONTH</h1>
        </div>
      </div></td>
    </tr>
    <tr>
      <td><div align="center">Month :
          <label>
            <select name="select">
              <option>&lt;Select Month&gt;</option>
              <option value="1">January</option>
              <option value="2">February</option>
              <option value="3">March</option>
              <option value="4">April</option>
              <option value="5">May</option>
              <option value="6">June</option>
              <option value="7">July</option>
              <option value="8">August</option>
              <option value="9">September</option>
              <option value="10">October</option>
              <option value="11">November</option>
              <option value="12">December</option>
                        </select>
            </label>
      </div></td>
    </tr>
    <tr>
      <td><div align="center">Year :
        <label>
            <select name="select2">
              <option>&lt;Select Year&gt;</option>
              <option value="2010">2010</option>
              <option value="2011">2011</option>
            </select>
            </label>
      </div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
    </tr>
    <tr>
      <td><div align="center"></div></td>
    </tr>
    <tr>
      <td><div align="center">
        <label></label>
        <input type="submit" name="Submit" value="Submit" />
      </div></td>
    </tr>
    <tr>
      <td><div align="center"><a href="expenses.php">&lt;&lt; Back To Menu </a></div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr bgcolor="#6699CC">
      <td><div align="center">Copyright 2011. All Rights Reserved. </div></td>
    </tr>
  </table>
</form>
</body>
</html>

<?php
}

else{
header("Location: index.php");
}
?>

This doesn't necessarily mean the last number that comes up will be needed for the outcome. IMO this is the most logical place to put it. Do tell me if it's otherwise.

And here's the code for the process

<?php
session_start();

/* include db connection file */
include("dbconn.php");

$randomnumber = mt_rand(1,999);

	function dropdown_empty()
	{
		echo"<script language='Javascript'>
			alert('You must select both month and year.');
			location.href='exp_month.php';
			</script>";
	}

if(isset($_POST['Submit']))
{
	if(!isset($_POST['form1'])){
	dropdown_empty();
	}
	
	else{
	/* capture values from HTML form */
	$month = $_POST['select'];
	$year = $_POST['select1'];
	}

/* execute SQL command */
		$sql= "SELECT item FROM expenses";
		$query = mysql_query($sql) or die("Error: " . mysql_error());
		$row = mysql_num_rows($query);
		
		if($row != 0)
			mysql_query("UPDATE expenses SET price = CONCAT('$randomnumber')");

}
mysql_close();
?>

What I had in mind was for the system to detect input from the drop down menu. If it is not null & there are rows of data in database with 'price' not allocated a value, a number will generate and inserted into that row. Let's say if there are already 5 rows of complete data along with price, hitting 'Submit' will not do anything.

I'm having issues with these..
1. When I don't select anything from the drop down menus the popup appears but the number is generated anyway.
2. When I do select, popup still appears - number is also generated.
3. The 'price' keeps changing when I try #1 or #2, replacing the existing value I have previously (with one row of data).

I've been looking through the codes but can't seem to see what I should change. Hope what I'm trying to do here is possible. Thanks in advance.

Regards,
Atikah

you will not get back a $_POST
you can see thi if you do a var_dump($ _POST);

so

if(!isset($_POST['form1'])){
dropdown_empty();

will not work.
use

if (is_set($_POST['select']) $month = $_POST['select'];
else dropdown_empty();

even better

if (is_set($_POST['select'] && !empty($_POST['select'])) $month = $_POST['select'];
else dropdown_empty();

as it also checks if it has a value

It worked. Thanks for your help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.