LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2007, 05:01 PM   #1
Dapernia
LQ Newbie
 
Registered: Jul 2003
Location: Merida - Venezuela
Distribution: Ubuntu 7.04
Posts: 14

Rep: Reputation: 0
Smile Problem Inserting Data In A Mysql Database Using PHP


Hello My Friends:
I am learning PHP and MySql using some tutorials and manuals that i've found in internet. I am using Ubuntu 7.04 and I've already installed Apache2, PHP5, MySQL5.0 and everything is working fine. I 've created a small database with PHPMyAdmin called `ficha_laboral` and one table called `datos_personales` with 6 fields.

Ok, everything looks fine until here, but when i try to insert some data in the database through the web browser (firefox), MySql doesn't insert anything in the database. i am using two scripts to do this, the first script is the form where i write all the data, and the second script inserts that data in the database.

This is the form code i'm using:

[HTML]
<html>
<body>
<form method="post" action="http://127.0.0.1/cursophp/add_reg.php">
Nombre :<input type="Text" name="nombre"><br>
Apellido :<input type="Text" name="apellido"><br>
C.I. :<input type="Text" name="ci"><br>
Profesion :<input type="Text" name="profesion"><br>
Fecha de Nacimiento :<input type="Text" name="nacimiento"><br>
Estado Civil :<input type="Text" name="estado"><br>
<input type="Submit" name="enviar" value="Aceptar información">
</form>
</body>
</html>
[/HTML]

and this is the PHP Script i am using (it's name is add_reg.php)

PHP Code:
<?php 
   
// process form
   
$link mysql_connect("127.0.0.1""root","1234");
   
mysql_select_db("ficha_laboral",$link);
   
$sql "INSERT INTO `datos_personales` (`Nombre`,`Apellido`,`CI`,`Profesion`,`Fecha_Nacimiento`,`Edo_Civil`) VALUES ('$nombre', '$apellido', '$ci', '$profesion','$nacimiento','$estado');";
   
$result mysql_query($sql);
   echo 
"ˇGracias! Hemos recibido sus datos.\n"
?>
when i execute the script I don't get any error, and everything looks fine, but the database keeps blank.
I've been the whole day trying to find out what is wrong in this code. Can you please help me out???
Thank you very much...

Last edited by Dapernia; 09-16-2007 at 06:10 PM.
 
Old 09-16-2007, 07:43 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You need to check for errors, see here: http://au2.php.net/manual/en/function.mysql-error.php
 
Old 09-16-2007, 07:46 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I'm suspicious of the back quotes (``).

Please:
1. Look at this example:
http://www.w3schools.com/php/php_mysql_insert.asp

2. Add a "die" clause with "mysql_err ()" (in case the connect fails).

3. Remove the back quotes from your "insert" line.

4. Add another "die" with a corresponding "mysql_err()" to your mysql_query().

5. "echo" the value of $result and the values of $nombre, $apellido, etc

6. Don't forget to call "mysql_close ()" to free the connection.

'Hope that helps .. PSM

Last edited by paulsm4; 09-16-2007 at 11:48 PM.
 
Old 09-16-2007, 10:09 PM   #4
owenjh
Member
 
Registered: Sep 2007
Distribution: Fedora / Slackware
Posts: 46

Rep: Reputation: 15
The back ticks are fine. If the code you posted for add_reg.php is all the code you have then I would try something like:

Code:
<?php
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$ci = $_POST['ci'];
$profesion = $_POST['profesion'];
$nacimiento = $_POST['nacimiento'];
$estado = $_POST['estado'];

   // process form
   $link = mysql_connect("127.0.0.1", "root","1234");
   mysql_select_db("ficha_laboral", $link);
   $sql = "INSERT INTO `datos_personales` (`Nombre`,`Apellido`,`CI`,`Profesion`,`Fecha_Nacimiento`,`Edo_Civil`) VALUES ('$nombre', '$apellido', '$ci', '$profesion','$nacimiento','$estado');";
   $result = mysql_query($sql);
   echo "ˇGracias! Hemos recibido sus datos.\n"; 
?>
Since you aren't assigning any variables it won't add the row.

Hope this helps

- Owen

p.s Its not the most secure code in the world, but it is good for a simple tutorial
 
Old 09-17-2007, 08:59 AM   #5
Dapernia
LQ Newbie
 
Registered: Jul 2003
Location: Merida - Venezuela
Distribution: Ubuntu 7.04
Posts: 14

Original Poster
Rep: Reputation: 0
Talking Now It's working fine!

Thanks Owenjh!!. Now It's working fine!!!
It's kind of weird that none of the tutorials i found had that assignment.
 
Old 09-17-2007, 10:05 AM   #6
owenjh
Member
 
Registered: Sep 2007
Distribution: Fedora / Slackware
Posts: 46

Rep: Reputation: 15
Check out php.net/mysql_escape_string

You should use this to help with the data of the input fields since if some one puts in a quote it will break the query and leave your script open for exploitation.

I think it will be something like this for all your variables:
Code:
$ci = mysql_escape_string($_POST['ci']);
But, its been a while so without testing it I wouldn't know.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL and PHP Error when inserting data JockVSJock Programming 2 10-06-2006 01:53 PM
need to get data from xml file to MySQL database, and then use php to access Armon Linux - General 1 01-18-2006 02:54 PM
Problem inserting data into a mysql table using PHP Rockgod2099 Programming 13 08-03-2005 12:27 AM
I need help with inserting data into mysql w/ PHP lostboy Programming 3 08-25-2003 12:08 PM
Problem Inserting MySQL data from PHP! rhuser Linux - Software 9 03-03-2003 07:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration