We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,439 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Insert into database table not working

can someone explain me where is problem in this code

it need to send this data to database table called anketa

<html>
	<head><title>Anketa</title></head>
	<body>
	
	<form action="insert.php" method="post">
	
Mesna zajednica 
<select name="mesto">
<option value="donji_grad">Donji Grad</option>
<option value="karadjordjev_dud">Karadjordjev Dud</option>
<option value="plavinac">Plavinac</option>
<option value="seone">Seone</option>
<option value="ladna_voda">Ladna Voda</option>
<option value="lestar">Lestar</option>
<option value="zlatno_brdo">Zlatno Brdo</option>
<option value="slavija">Slavija</option>
<option value="papazovac">Papazovac</option>
<option value="carina">Carina</option>
<option value="sveti_sava">Sveti Sava</option>
<option value="25maj">25. Maj</option>
<option value="udovice">Udovice</option>
<option value="lipe">Lipe</option>
<option value="mihajlovac">Mihajlovac</option>
<option value="vranovo">Vranovo</option>
<option value="radinac">Radinac</option>
<option value="mala_krsna">Mala Krsna</option>
<option value="skobalj">Skobalj</option>
<option value="osipaonica">Osipaonica</option>
<option value="drugovac">Drugovac</option>
<option value="badljevica">Badljevica</option>
<option value="salinac">Salinac</option>
<option value="kulic">Kulic</option>
<option value="suvodol">Suvodol</option>
<option value="lunjevac">Lunjevac</option>
<option value="landol">Landol</option>
<option value="petrijevo">Petrijevo</option>
<option value="kolari">Kolari</option>
<option value="binovac">Binovac</option>
<option value="lugavcina">Lugavcina</option>
<option value="saraorci">Saraorci</option>
<option value="vucak">Vucak</option>
<option value="vrbovac">Vrbovac</option>
<option value="ralja">Ralja</option>
<option value="dobri_do">Dobri Do</option>
<option value="vodanj">Vodanj</option>
<option value="malo_orasje">Malo Orasje</option>
</select><br><br>





Ime i prezime : <input type="text" name="ime"><br><br>

Broj clanova domacinstva : <input type="text" name="broj_clanova"><br><br>

Broj clanova domacinsta sa pravom glasa: <input type="text" name="clanova_sa_pravom_glasa"><br><br>

Ulica i broj: <input type="text" name="adresa"><br><br>

Broj zaposlenih u domacinstvu <input name="broj_zaposlenih" type="text" id="broj_zaposlenih" size="40"><br><br>

Broj ne zaposlenih <input name="broj_nezaposlenih" type="text" id="broj_nezaposlenih" size="20"><br><br>

Broj penzionera clanova domacinstva <input name="broj_penzionera" type="text" id="broj_penzionera" size="20"><br><br>

Da li ste Vi ili neko od clanova vaseg domacinstva clanovi neke stranke i koje, ako nije tajna: Da<input name="clan_stranke" type="radio" id="clan_stranke" value="da">   Ne<input name="clan_stranke" type="radio" id="clan_stranke" value="ne">
<select name="ime_stranke">
<option value="" selected="selected"></option>
<option value="ds">DS</option>
<option value="sps">SPS</option>
<option value="srs">SRS</option>
<option value="sns">SNS</option>
<option value="pzs">PZS</option>
<option value="kzbs">KZBS</option>
<option value="urs">URS</option>
<option value="ldp">LDP</option>
<option value="ostali">Ostali</option>
</select><br>

Problemi u vasoj okolini:

Glavni problem: <input name="glavni_problem" type="text" id="glavni_problem" size="200"><br><br>

Predlog resenja: <input name="predlog_resenja" type="text" id="predlog_resenja" size="200"><br><br>

Sporedni problemi: <input name="sporedni_problemi" type="text" id="sporedni_problemi" size="200"><br><br>

Predlog resenja: <input name="predlog_resenja2" type="text" id="predlog_resenja2" size="200"><br><br>

Kom smederevskom politicaru najvise verujete:<br><br>
1. Suzana Spasojevic <input name="politicar" type="checkbox" id="politicar" value="suzana_spasojevic"><br>
2. Brance Stojanovic <input name="politicar" type="checkbox" id="politicar" value="brance_stojanovic"><br>
3. Predrag Umicevic <input name="politicar" type="checkbox" id="politicar" value="predrag_umicevic"><br>
4. Jasna Avramovic <input name="politicar" type="checkbox" id="politicar" value="jasna_avramovic"><br>
5. Sasa Radosavljevic <input name="politicar" type="checkbox" id="politicar" value="sasa_radosavljevic"><br>
6. Milan Lukic <input name="politicar" type="checkbox" id="politicar" value="milan_lukic"><br>
7. Dragan Sormaz <input name="politicar" type="checkbox" id="politicar" value="dragan_sormaz"></p>

Da li biste glasali za SPS? Da<input name="sps" type="radio" id="sps" value="da"> Ne<input name="sps" type="radio" id="sps" value="ne"> Mozda<input name="sps" type="radio" id="sps" value="mozda"><br><br>
       
Zasto bi ste glasali/ne bi ste glasali: <input name="razlog" type="text" id="razlog" size="200"><br><br>
		
Ako je odgovor ne, za koga bi glasali? <select name="ako_ne">
<option value="ds">DS</option>
<option value="sps">SPS</option>
<option value="srs">SRS</option>
<option value="sns">SNS</option>
<option value="pzs">PZS</option>
<option value="kzbs">KZBS</option>
<option value="urs">URS</option>
<option value="ldp">LDP</option>
<option value="ostali">Ostali</option>
<option value="ni_za_koga"  selected="selected">Ni za koga</option>
</select></p>

<input type="Submit">
</form>


<?
$username="a4764180_anketa";
$password="a12345";
$database="a4764180_anketa";

$mesto=$_POST['mesto'];
$ime=$_POST['ime'];
$broj_clanova=$_POST['broj_clanova'];
$clanova_sa_pravom_glasa=$_POST['broj_clanova_sa_pravom_glasa'];
$adresa=$_POST['adresa'];
$broj_zaposlenih=$_POST['broj_zaposlenih'];
$broj_nezaposlenih=$_POST['broj_nezaposlenih'];
$broj_penzionera=$_POST['broj_penzionera'];
$clan_stranke=$_POST['clan_stranke'];
$glavni_problem=$_POST['glavni_problem'];
$predlog_resenja=$_POST['predlog_resenja'];
$sporedni_problemi=$_POST['sporedni_problemi'];
$predlog_resenja2=$_POST['predlog_resenja2'];
$politicar=$_POST['politicar'];
$sps=$_POST['sps'];
$razlog=$_POST['razlog'];
$ako_ne=$_POST['ako_ne'];





mysql_connect("mysql6.000webhost.com",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO anketa VALUES ('','$mesto','$ime','$broj_clanova','$clanova_sa_pravom_glasa','$adresa','$broj_zaposlenih','$broj_nezaposlenih','$broj_penzionera','$clan_stranke','$glavni_problem','$predlog_resenja','$sporedni_problemi','$predlog_resenja2','$politicar','$sps',$razlog','$ako_ne')"; 
mysql_query($query);

mysql_close();
?>

	</body>
	</html>
9
Contributors
35
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
36
Views
filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What's the problem? Use error checking, and show the error here (if any).

pritaeas
Posting Prodigy
Moderator
9,533 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

What's the problem? Use error checking, and show the error here (if any).

No just after submit I get to 000webhost page (that means no page) and data doesn't goes to base.

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

are you sure your your action is set right?
does insert.php exist and is it in the same folder

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

but what for I need insert.php when I am sending it to base not to other page?

I made insert.php now when I do sumbit it goes to blank page (insert.php) but still nothing inserted to base...

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
<form action="insert.php" method="post">
  <input type="Submit">
</form>

translated what that says is on button click take the values from the form and post them to insert.php

not sure where/how you are sending anything to base ?

jstfsklh211
Junior Poster
100 posts since Apr 2011
Reputation Points: 34
Solved Threads: 27
Skill Endorsements: 1

what for I did then INSERT into anketa blabla

mysql connect

and all parameters?

what should I add so submit goes to page which will say thank you blabla and send data to base?

mysql_connect("mysql6.000webhost.com",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO anketa VALUES ('','$mesto','$ime','$broj_clanova','$clanova_sa_pravom_glasa','$adresa','$broj_zaposlenih','$broj_nezaposlenih','$broj_penzionera','$clan_stranke','$glavni_problem','$predlog_resenja','$sporedni_problemi','$predlog_resenja2','$politicar','$sps',$razlog','$ako_ne')";
mysql_query($query);

mysql_close();
?>

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

does your db table anketa have columns at all? What i meant is that you want the script to insert the data to your dbase with all these values, but what column does these data needs to be inserted?

veedeoo
Master Poster
764 posts since Oct 2011
Reputation Points: 298
Solved Threads: 133
Skill Endorsements: 13

something like this

INSERT INTO `anketa` (col1, col2, col3, moreCOL_ifNecessary)VAlUES (All the values HERE as shown on your script);
veedeoo
Master Poster
764 posts since Oct 2011
Reputation Points: 298
Solved Threads: 133
Skill Endorsements: 13

I have all columns, first one is id that is why I left first like this and second is mesto('','$mesto'....) those are values

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Try providing NULL for your id, if it is an auto incremented column.

Read this too, you still haven't added error checking ?

pritaeas
Posting Prodigy
Moderator
9,533 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

use
mysql_query($query) or die(mysql_error());

to diagnosis the error properly.

emoviefreak
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I still recommend to use fieldnames or column names on the insert query, because without it your script can easily break. For instance, you have 25 columns in your table and you mistakenly insert 24 values only, then your script will be doomed with errors.

Another option you may want to utilize is PDO which is pretty handy.. especially, in reporting errors..

You can also do an inventory on how many columns are on your table by running this script..

<?php
$db_host = "localhost";
$db_user = "userName";
$db_password = "password";
$db_database = "database Name or table";

mysql_connect($db_host, $db_user, $db_password) or die("Sorry Dude I cannot connect");

mysql_select_db($db_database)or die("OOPS connection failed. While trying to connect to ".$db_database);
## I modified this script from another script author..so, I don't know why id won't print out on the output.
$simple_query = "select * from anketa";
$res = mysql_query($simple_query);

$n_col = mysql_num_fields($res);

for ( $some_x = 0; $some_x < $n_col; $some_x++ ) {
$col_names = mysql_field_name($res, $some_x);
echo "'".$col_names."',";
}
?>

NOTE: the column "id" will not get printed. If you want to include those fieldnames on your query you can copy and just add 'id', on top of the list. Don't forget to remove the very last , (comma)

veedeoo
Master Poster
764 posts since Oct 2011
Reputation Points: 298
Solved Threads: 133
Skill Endorsements: 13

I will try it, and I have no errors, just when I do submit I got redirected to insert.php which is blank page...

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

what should I have in insert.php so data from index goes to base?

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Try using the SET syntax that way you can ignore the autoincrement field

diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61

can you give example, I used code from tutorial just right here http://www.freewebmasterhelp.com/tutorials/phpmysql/3

idk whay is not working I just have more columns then there

filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

$query = "INSERT INTO anketa SET `field2` = '$mesto', `field3` = '$ime'...)";

Obviously you need to specify the fieldnames, but it means you can ignore the autoincrement (PK) field. However, a drawback is that you can't insert multiple rows via this method - for that you'd have to use the VALUES syntax.

The advantage of this method is that if you make a small change to the table structure, changing the sql queries should be simple. Changing the order of fields or adding / deleting some can cause a nosebleed if you don't specify fieldnames (as in your method).

The VALUES syntax does allow you to specify fieldnames though - but lenghty sql with many fields makes pairing field-values difficult.

diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61

but I have already this

$query = "INSERT INTO anketa VALUES ('','$mesto','$ime','$broj_clanova','$clanova_sa_pravom_glasa','$adresa','$broj_zaposlenih','$broj_nezaposlenih','$broj_penzionera','$clan_stranke','$glavni_problem','$predlog_resenja','$sporedni_problemi','$predlog_resenja2','$politicar','$sps',$razlog','$ako_ne')";
mysql_query($query);
 
mysql_close();
?>
filipgothic
Junior Poster in Training
58 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I know... That's the point I was making...

You can try:

$query = "INSERT INTO anketa VALUES (NULL,'$mesto','$ime','$broj_clanova','$clanova_sa_pravom_glasa','$adresa','$broj_zaposlenih','$broj_nezaposlenih','$broj_penzionera','$clan_stranke','$glavni_problem','$predlog_resenja','$sporedni_problemi','$predlog_resenja2','$politicar','$sps',$razlog','$ako_ne')";

But if there's an error after that, you'll probably have no idea what's wrong, as you don't specifically pair up fields and values. It's a lazy syntax that bites you in the backside.

diafol
Keep Smiling
Moderator
10,833 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,533
Skill Endorsements: 61

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page generated in 0.1299 seconds using 2.79MB