0

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
37
Views
5 Years
Discussion Span
Last Post by filipgothic
0

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.

0

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

Edited by filipgothic: n/a

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 ?

Edited by jstfsklh211: n/a

0

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

Edited by filipgothic: n/a

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?

0

something like this

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

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

0

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

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

Edited by pritaeas: n/a

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)

Edited by veedeoo: n/a

0

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

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.

0

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

0

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

Edited by vaultdweller123: n/a

0

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

Edited by karthik_ppts: n/a

0

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?

0

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

0

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.

0

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

0

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.

Edited by veedeoo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.