Hello,

I am trying to create a stock recording system:

<h4>STOCK RECORDING SYSTEM</h4>

<form action="action.php" method="post">

<table border="0" width="200">
<tr>
    <tr>
        <td>Produk  :</td><br>
        <td><input type="text" size="12" maxlength="22" name="Produk"></td>    
    </tr>    
    <tr>
        <td>Jumlah  :</td>
        <td><input type="text" size="12" maxlength="22" name="Jumlah"></td><br> 
    <tr>
        <td>Tanggal :</td>
        <td><input type="text" size="12" maxlength="22" name="Tanggal"></td><br>
    </tr>    
</table>


</form>

<?php

username='root';
password='';

$con = mysql_connect(localhost, username, password);
mysql_select_db("snack", $con);

mysql_query("INSERT INTO Produk VALUES ('Produk')");
mysql_query("INSERT INTO Jumlah VALUES ('Jumlah')");
mysql_query("INSERT INTO Tanggal_Masuk VALUES ('Tanggal_Masuk')");


?>




Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\Innovation\script_shop\stock.php on line 27
Line27:    username='root';

the username is correct.  Why the error still appears?

You are missing the $ sign in front of the variable name, change it to $username, bye!

The same goes for the password variable.

I am having problem on the combo box.

I have one database : snack
two tables: po and stock

po: po
stock: produk, jumlah, tanggal_masuk

<?php

$username='root';
$password='';

$con = mysql_connect('localhost', $username, $password);
mysql_select_db('snack', $con);

mysql_query("INSERT INTO Produk VALUES ('Produk')");
mysql_query("INSERT INTO Jumlah VALUES ('Jumlah')");
mysql_query("INSERT INTO Tanggal_Masuk VALUES ('Tanggal_Masuk')");

// combo box

    <select name="batch" onchange="submit();">
    <option value="-1" >-- Select Adress --</option>
    <?php

    $result = mysql_query('SELECT * FROM PO ORDER BY PO ASC') or die(mysql_error());
    while ($item = mysql_fetch_assoc($result))
    {
    echo '<option value="'.$item['po'].'" '.isSelected($item['po'],$batch).'>'.$item['namn'].'</option>';
    }
    ?>
    </select>


?>

I am trying to fill in po with produk type to list all the available products in the combo box.

Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\Innovation\script_shop\stock.php on line 39

line 39: <select name="batch" onchange="submit();">

Check your opening and closing php tags. You have one missing.
You should re-write this code referring to a good reference manual as
you are doing it.

As TonyG said put a closing php tag on line 14 in above code.

Getting close.

I wonder why I do not see the list of products appears in the combo box. I only see // combo box --select address--

Probably you get an empty set from this query:

SELECT * FROM PO ORDER BY PO ASC

Try it from a mysql client.

I wonder why I get empty set - it should works from localhost mysql

As cereal said run the query:

SELECT * FROM PO ORDER BY PO ASC

in phpmyadmin (or mysql client) and see if you get the correct resultset for options in your select. If the query returns correct result then check the html code (in Firefox right click -> View page source) and check whether there are no html errors also.

You might want to check whether isSelected($item['po'],$batch) function returns correct value.

$result = mysql_query('SELECT * FROM PO ORDER BY PO ASC') or die(mysql_error());
    echo $result;

it doesn't print out anything

If I view - page source in IE:

// combo box

    <select name="batch" onchange="submit();">
    <option value="-1" >-- Select Adress --</option>
    Resource id #4<br />
<b>Notice</b>:  Undefined index: po in <b>C:\xampp\htdocs\Innovation\script_shop\stock.php</b> on line <b>48</b><br />
<br />
<b>Fatal error</b>:  Call to undefined function isSelected() in <b>C:\xampp\htdocs\Innovation\script_shop\stock.php</b> on line <b>48</b><br />

<b>Fatal error</b>: Call to undefined function isSelected() in <b>C:\xampp\htdocs\Innovation\script_shop\stock.php</b> on line <b>48</b><br />

This is probably the answer: the isSelected() function that you are calling, does not exist. Have you included appropriate file where the function should be defined?

And also

Undefined index: po in <b>C:\xampp\htdocs\Innovation\script_shop\stock.php

means that there are no records since nothing exists in the $item array.

You should temporary remove the isSelected($item['po'],$batch) function and see if you get anything:

while ($item = mysql_fetch_assoc($result))
{
    echo '<option value="'.$item['po'].'" . $item['namn'] . '</option>';
}

The isSelected() function probably only adds selected="selected" to selected option.

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\Innovation\script_shop\stock.php on line 71

that would be line 3.

Sory it should be:

echo '<option value="'.$item['po'].'">' . $item['namn'] . '</option>';

My error when copying.

The combo box remains empty

This is tricky.

What you get when you run this query in phpmyadmin:

SELECT * FROM PO ORDER BY PO ASC

I must first select the database then enter the query: this appears:

BBQ Snack
Metalik
Rendang
Stik BBQ
Stik biru

This is mysterious. The code in the snippet you posted seem to be OK (once corrected with suggestions from other posts). Can you post the whole code? I would have to test it in my environment to try to find something.

This is the whole codes:

stock.php

<h4>STOCK RECORDING SYSTEM</h4>

<form action="action.php" method="post">

<table border="0" width="200">
<tr>
    <tr>
        <td>Produk  :</td><br>
        <td><input type="text" size="12" maxlength="22" name="Produk"></td>    
    </tr>    
    <tr>
        <td>Jumlah  :</td>
        <td><input type="text" size="12" maxlength="22" name="Jumlah"></td><br> 
    <tr>
        <td>Tanggal :</td>
        <td><input type="date" size="12" maxlength="22" name="Tanggal" value="10-01-2013"></td><br>
    </tr>    
</table>


</form>

<?php

$username='root';
$password='';

$con = mysql_connect('localhost', $username, $password);
mysql_select_db('snack', $con);
?>
<form action="action.php" method="post">
<?php
mysql_query("INSERT INTO Produk VALUES ('Produk')");
mysql_query("INSERT INTO Jumlah VALUES ('Jumlah')");
mysql_query("INSERT INTO Tanggal_Masuk VALUES ('Tanggal_Masuk')");
?>
<input type="submit">
</form>
// combo box

    <select name="batch" onchange="submit();">
    <option value="-1" >-- Select Address --</option>
    <?php

    $result = mysql_query('SELECT * FROM PO ORDER BY PO ASC') or die(mysql_error());
    echo $result;
    while ($item = mysql_fetch_assoc($result))
    {
    echo '<option value="'.$item['po'].'">'.$item['namn'].'</option>';
    }
    ?>
    </select>

</html>

snack.sql

-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 21, 2013 at 07:49 AM
-- Server version: 5.5.8
-- PHP Version: 5.3.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `snack`
--

-- --------------------------------------------------------

--
-- Table structure for table `po`
--

CREATE TABLE IF NOT EXISTS `po` (
  `PO` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `po`
--

INSERT INTO `po` (`PO`) VALUES
('Metalik'),
('Stik biru'),
('BBQ Snack'),
('Stik BBQ'),
('Rendang');

-- --------------------------------------------------------

--
-- Table structure for table `stock`
--

CREATE TABLE IF NOT EXISTS `stock` (
  `Produk` varchar(20) NOT NULL,
  `Jumlah` int(5) NOT NULL,
  `Tanggal Masuk` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `stock`
--

Do you have a column namn in the PO table? You are referring to it in the while loop:

echo '<option value="'.$item['po'].'">'.$item['namn'].'</option>';

There are two other small errors in the code:

  • remove <br> tags after </td> tags since they should not be there
  • remove // combo box text from HTML code (it is not a comment within a php block)
<select name="batch" onchange="submit();">
    <option value="-1" >-- Select Address --</option>
    <?php
    $result = mysql_query('SELECT `PO`,`namn` FROM PO ORDER BY PO ASC') or die(mysql_error());
    //php arrays are case sensitive, the field was defined in caps
    //you can use SELECT `po` to make it lower case in the mysql pull
    //var_dump($result);//$result will be an object so echo won't work on it, var_dump() will
    while ($item = mysql_fetch_assoc($result)){//this is good, i just added \r\n so
    //when you look at "view source" it's easier to read
        echo "<option value='{$item['po']}'>{$item['namn']}</option>\r\n";
    }
    ?>
</select>

Here's a tester for the code to show it works

<select name="batch" onchange="submit();">
    <option value="-1" >-- Select Address --</option>
    <?php
    $data = array(
            '1'=>array('po'=>'1','namn'=>'name 1'),
            '2'=>array('po'=>'2','namn'=>'name 2'),
            '3'=>array('po'=>'3','namn'=>'name 3'),
            '4'=>array('po'=>'4','namn'=>'name 4'),
            '5'=>array('po'=>'5','namn'=>'name 5')
        );
    foreach($data as $item){
        echo "<option value='{$item['po']}'>{$item['namn']}</option>\r\n";
    }
    ?>
</select>

You could also test the data in the result like this:

 while ($item = mysql_fetch_assoc($result)){
    var_dump($item);
    echo "<br/>\r\n";
}

and if you are going to submit that form you will also want to move the </form> tag to after the select box or the select box data won't be submitted or other fields will be missing

I have just noticed that you posted the structure and contents of the tables in your post, sory. You are missing the namn column in the po table. You are referring to the namn column in the while loop.

And as Biiim said, associative indexes (keys) are case sensitive while mysql names are not. I would strongly suggest that you use always lowercase to avoid confusion. Also chose table names and field names carefully and make them descriptive for the same reason.

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.