954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Insert Mysql problem

<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

Lius
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Is there any error?

MooGeek
Posting Whiz
355 posts since Mar 2009
Reputation Points: 26
Solved Threads: 33
 

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

divyakrishnan
Posting Whiz in Training
201 posts since May 2010
Reputation Points: 30
Solved Threads: 24
 
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

Lius
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

is this phonebook.php file?

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 
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.

Lius
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

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

Lius
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

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

Lius
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

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".

faroukmuhammad
Junior Poster
140 posts since Sep 2008
Reputation Points: 20
Solved Threads: 16
 

OK.. thx for your help

Lius
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: