<script type="text/javascript">
function populateHarga() {
var x=document.getElementById("barang").value;
document.getElementById("harga").value=x;  
var y=document.getElementById('barang')[document.getElementById('barang').selectedIndex].innerHTML;
document.getElementById('nama_brg').value=y;
}
function populateTotal()
{
var y=document.getElementById("jumlah").value;
var x=document.getElementById("harga").value;
document.getElementById("totalnya").value=x*y;
}
</script>

	<body>
		&nbsp;<p>

		

		

		<?php
			mysql_connect("localhost","root","");
			mysql_select_db("stageco");
	
		?>
		<table border="1" cellpadding="0" cellspacing="0">
			<?php
				$nama_b=$_GET['nama_brg'];
				$harga_b=$_GET['harga'];
				$jumlah_b=$_GET['jumlah'];
				$total_b=$_GET['totalnya'];
				if($nama_b!="")
				{
					mysql_query("INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')");
				}
				$result=mysql_query("SELECT * FROM temp");
				$i = 0;
				while ($row = mysql_fetch_array($result)) {
					if ($i > 0) {
						echo "<tr valign='bottom'>";
						echo "<td bgcolor='#ffffff' height='1'></td>";
						echo "</tr>";
					}
					echo "<tr valign='middle'>";
					echo "<td class='tabval'><width='10' height='20'></td>";
					echo "<td class='tabval'><b>".$row['t_nama']."</b></td>";
					echo "<td class='tabval'>".$row['t_jumlah']."&nbsp;</td>";
					echo "<td class='tabval'>".$row['t_harga']."&nbsp;</td>";
					echo "<td class='tabval'>".$row['t_total']."&nbsp;</td>";
					echo "<td class='tabval'></td>";
					echo "</tr>";
					$i++;
					}
				echo "<tr valign='bottom'>";
				echo "<td bgcolor='#fb7922' colspan='6'></td>";
				echo "</tr>";
							?>

		</table>

<h2>add Entry</h2>
<?
echo "<form action=phonebook.php method=get>";
$get = mysql_query("select * from inventori");

echo"<table width=649 border=0 cellspacing=3 cellpadding=3>";

echo"<tr>";
echo"<td width=99>Nama Produk</td>";
echo"<td width=198><select id=barang onchange=populateHarga()>";
echo "<option></option>";
while($list=mysql_fetch_array($get))
{
echo "<option value='$list[harga]'>$list[nama_barang]</option>";
}
echo"</select></td>";
echo"</tr>";
echo"<tr>";
echo"<td>Jumlah</td>";
echo"<td><input id=jumlah name=jumlah type=text size=12 onblur=populateTotal() /></td>";
echo"</tr>";
echo"<tr>";
echo"<td>Harga</td>";
echo"<td>";
echo"<input id=harga  name=harga type=text />";
echo"</td>";
echo"</tr><tr><td>";
echo"Total :";
echo"</td><td>";
echo"<input type=text name=totalnya id=totalnya /> </td></tr>";
echo"<input type=text name=nama_brg id=nama_brg />";
echo"<tr>";
echo"<td colspan=2><div align=right><input type=submit  value=Next />
  <input type=reset id=batal value=Reset />
</div></td></tr>";
echo"</table></form>";
?>

	</body>
</html>

My problem is i want to insert the DB from a form i make in the same page.. but it doesnt work at all..thanks b4

Is there any error?

Post your errors.
Then it is easy to find out solution.

Post your errors.
Then it is easy to find out solution.

the problem is i cant insert the data i filled to the database..no error detected..it confuse me

is this phonebook.php file?

is this phonebook.php file?

yes.. i just testing some part of the main source code..and this little part has a problem.. so the name i use is not really important.

after 34th line echo out your query

echo "INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')"

copy the query from output and execute it in the SQL section of your phpmyadmin and see what happens

after 34th line echo out your query

echo "INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')"

copy the query from output and execute it in the SQL section of your phpmyadmin and see what happens

SQL query:

echo "INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')"

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'echo "INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b'' at line 1

I dont get it

dont execute this

echo "INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')"

just put this line in your code after 35th line.
Run this page in browser.
copy the printed query from your browser and execute this in phpmyadmin

dont execute this

echo "INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')"

just put this line in your code after 35th line.
Run this page in browser.
copy the printed query from your browser and execute this in phpmyadmin

Yeah done it and gives me error :
INSERT INTO temp( 't_nama', 't_harga', 't_jumlah', 't_total' )
VALUES (
'Kabel merah', '3000', '9', '27000'
)

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''t_nama','t_harga','t_jumlah','t_total') VALUES('Kabel merah','3000','9','27000'' at line 1

just to mention my type of data:
t_nama=varchar
t_harga=int
t_jumlah=int
t_total=int

thx b4..

replace your line 36 with

mysql_query("INSERT INTO temp(`t_nama`,`t_harga`,`t_jumlah`,`t_total`) VALUES('$nama_b','$harga_b','$jumlah_b','$total_b')");

just replace the "'" with"`" in your query.

Your mistakes are uniformly distributed from line 23-38. Replace those lines with this;

<?php		$conn = mysql_connect("localhost","root","");
			         mysql_select_db("stageco"); 		?>
<table border="1" cellpadding="0" cellspacing="0">			
<?php				
                  $nama_b=$_GET['nama_brg'];
		$harga_b=$_GET['harga'];
		$jumlah_b=$_GET['jumlah'];
		$total_b=$_GET['totalnya'];	
			
[INDENT]if($nama_b!="")	{					
 mysql_query("INSERT INTO temp('t_nama','t_harga','t_jumlah','t_total') VALUES     ('$nama_b','$harga_b','$jumlah_b','$total_b')", $conn);

  }[/INDENT]				

                  $result=mysql_query("SELECT * FROM temp", $conn);

When ever you are sending a query you need your connection identifier; stored in the variable "$conn".

OK.. thx 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.