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>

Recommended Answers

All 35 Replies

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

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.

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

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

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

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();
?>

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?

something like this

INSERT INTO `anketa` (col1, col2, col3, moreCOL_ifNecessary)VAlUES (All the values HERE as shown on your script);

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

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

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

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

to diagnosis the error properly.

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)

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

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

Member Avatar for diafol

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

Member Avatar for diafol

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

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();
?>
Member Avatar for diafol

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.

hey flipgothic haha i don't know if you can understand us right but what we are trying to say is, when you insert use the name-values pairs algorithm, what your using now is your just specifying values it's ok but it follow correct order based on your table structure, you must've inserted the values incorrectly in wrong order, ex of pairing name-values query

mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");

and btw ha ha of course it's gonna return blank if your insert.php is empty you got to put your insert code there and put whatever output you want to display :D

<? - Its not a good practice of using this. Instead of that use <?php

but I have already values

$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);

maybe I am missing in php code like if doSubmit = Submit or something like that? what should I have in insert.php example

$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);

or?

Member Avatar for diafol

You've posyed the same thing again have tried NULL?

yes, still not going in base with (NULL,'$mesto',.......

Member Avatar for diafol

OK, but you didn't say that. Did you try the SET syntax that I suggested?

no, I need full example and place where to put it, because I just used code from tutorial and changed Values to mines and database info

Member Avatar for diafol

Well, veedeeo gave an example. I suggest that you do a little background reading, maybe check out the php.net manual, if you haven't got a clue about what you're doing. Good luck and goodbye.

filipgothic, I'm afraid you are in over your head my friend, but I will try to help anyway. Everyone is telling you how to fix your sql query, and they are dead-on right, but it appears the problem you are having is even more basic. Right now, your form submits the data to a blank PHP file that you created (insert.php). What you need to do is put the WHOLE DATABASE FUNCTION into your insert.php file, ie put THIS in insert.php:

<?php
$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 (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')"; 
mysql_query($query);
 
mysql_close();
?>

I'm not saying it will work, you still really need to fix up that sql query by declaring which fields get which data, but at least by getting this code into insert.php you have a hope of moving onto the next step. Good luck.

-Phil

no, I need full example and place where to put it, because I just used code from tutorial and changed Values to mines and database info

Hi,

Please follow Ardav's advice, and We must also give 4 thumbs up to all the members who contributed their insights to your problems. Read php.net's manual and try to understand them. What happened here is that you have presented us with big codes (php and mysql combined) to analyze. When people post questions with big codes like yours, the assumptions is that you are an advance coder.

Looking at your codes, anyone can draw their conclusion that your codes were written by an advance coder, because of the implementations of php shorthand and so as the use of shorthand query. Please be informed that nothing is wrong in using shorthands in either php or mysql. However, it has to be used oNLY in an environment where it is feasible and supported. Meaning, this <?=$something?> can falter at times (read about php.ini directives), but in perfect environment it won't. The same set of conditions applies to your mysql query.

Some people call this as mysql hack query (not the hack most people are afraid of, but the ones that makes our coding tasks a lot easier). In a favorable environment, these codes will thrive. However, this approach requires precision on your part.. you cannot skip a column value NOT EVEN once.. It has to be perfectly matched or paired to your actual database table columns. Otherwise, this approach is going to fail without a doubt. In your case, you must precisely count and make sure that your database table "anketa" have 18 columns total. FROM 'id" to the very last column, it has to be 18 total, because this is the total count of the items you have in your VAlUES.

$query = "INSERT INTO anketa VALUES (NULL,'$mesto','$ime','$broj_clanova','$clanova_sa_pravomshort_open_tag_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);

Sometimes, even with the precise count of the columns does not guarantee a NO FAIL operation, because each columns has to be defined appropriately for the data type being posted. For example, one of the values is more than 255 characters, but in your actual database table, it was set as varchar 255, how about int, a plain text or even a BLOB data types, and how about the ones assigned as NULL by default or NOT NULL? These are just a few things worth mentioning, but there are many more to explore and to learn. http://www.w3schools.com/php/php_mysql_create.asp

If the above codes provided by jawanda0 does not work for you, start with the baby steps like the one showed in this this tutorial http://www.w3schools.com/php/php_mysql_insert.asp. NOtice, how they laid out fieldnames and values? They are precisely paired. Think of it like a zipper, it has to have a pair.

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.