user2830277
user2830277

Reputation: 13

Code doesn't write values in db

The code below should write code into the database. I have divided into two parts HTML AND PHP code are separate. HTML form code is shown below:

<form name="form1" action="insert.php" method="post">
<h3>Ime </h3> <input type="text" name="field1" > <br/> <br/>
<h3>Prezime </h3> <input type="text" name="field2" > <br/> <br/>
<h3>Firma </h3> <input type="text" name="field3" > <br/> <br/>
<h3>Adresa </h3><input type="text" name="field4" > <br/> <br/>
<h3>Telefon </h3> <input type="text" name="field5" > <br/> <br/>
<h3>Fax </h3><input type="text" name="field6" > <br/> <br/>
<h3>Mobitel </h3> <input type="text" name="field7" > <br/> <br/>
<h3>Email </h3> <input type="text" name="field8" > <br/> <br/>
<h3>Web stranica </h3> <input type="text" name="field9" > <br/> 
</form>

PhP code is shown below.

$host="localhost"; // Host name
$username="root"; // username
$password="le30mu09"; // password
$database="imenik"; // Database name
$tbl_name="clanovi"; // Table name

// Replace database connect functions depending on database you are using.

$field1=$_POST['field1'];
$field2=$_POST['field2'];
$field3=$_POST['field3'];
$field4=$_POST['field4'];
$field5=$_POST['field5'];
$field6=$_POST['field6'];
$field7=$_POST['field7'];
$field8=$_POST['field8'];
$field9=$_POST['field9']; 

$link=mysql_connect("$host", "$username", "$password");
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db("$database");
if (!$db_selected) {
    die ('db is not selected : ' . mysql_error());
}

   $query = "INSERT INTO `clanovi`(`Ime`, `Prezime`, `Firma`, `Adresa`, `Telefon`, `Fax`, `Mobitel`, `Email`, `Web_stranica`) VALUES ( "$field1", "$field2", "$field3", "$field4", "$field5", "$field6", "$field7", "$field8", "$field9")"; 

mysql_query($query);
mysql_close();

Upvotes: 0

Views: 62

Answers (4)

echo_Me
echo_Me

Reputation: 37233

you are not selecting database. you are using double quotes in not its place.

replace this

  $db_selected = mysql_select_db("$database");

by

 $db_selected = mysql_select_db($database);

and also replace this

 $link=mysql_connect("$host", "$username", "$password");

by

 $link=mysql_connect($host, $username, $password);
  • i recomand you to use PDO or mysqli instead.

Upvotes: 0

MrJustin
MrJustin

Reputation: 1076

You appear to be using a lot of quotation marks in places that you shouldn't be using them in. It is funny how you can code something for 5 hours and then try to debug it for 2 hours because of a simple quotation mark! It's funny and very depressing at the same time :(

Ok, let's fix the code a little bit!

Database

$link=mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db($database);
if (!$db_selected) {
    die ('db is not selected : ' . mysql_error());
}

Notice how I stripped all of the quotation marks out of the code? That will help with database connection and selection.

Now let's move onto the actual inserting of the information into the database!

$query = "INSERT INTO clanovi ('Ime', 'Prezime', 'Firma', 'Adresa', 'Telefon', 'Fax', 'Mobitel, 'Email', 'Web_stranica') VALUES ( $field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $field9)"; 

Again, I stripped all of the quotation marks. Plus I removed backticks and replaced with a ' and also took the '' off of your table name - You should nly use quotation marks when not using a variable.

//Correct
VALUES ($field1, "textnotvariable", $field2...

//Incorrect
VALUES ("$field1", "textnotvariable", "field2"...

The same goes with echo statements. Here's an example...

$myname = "MrJustin";

//Correct
echo $myname;

    //or

echo "My name is ". $myname .", it's nice to meet you!";

//Incorrect 

echo "My name is $myname, it's nice to meet you";

You'll notice how I used ". $myname ." - that tells the echo to break away from using text, and to pass a variable! :) That to me is the best way to explain how quotations will break a code.

Oh, and you should ALWAYS sanitize your inputs/outputs when using foreign code. I would do some Google searching on that one, and then chat us back up if you run into problems with that!

Hopefully this helps, and happy coding!!

Upvotes: 0

praveen
praveen

Reputation: 286

Modify your Insert query. It should be like this:

INSERT INTO clanovi
    (column1,column2,column3,...) 
VALUES 
    ( $field1, $field2, $field3,.....)

Upvotes: 0

gedq
gedq

Reputation: 610

You need to tell us what the actual error is. And bone up on PDO and the dangers of sending unsanitised POST variables to the DB as a matter of priority.

Upvotes: 1

Related Questions