0

Here is a little php program I did a few years ago (it worked then...) As I recently tried to incorporate it to my new "website", no data is inserted in my table...

(when I run it, I don't receive any error message)

If anyone could tell me what's wrong, I'd be very glad. THX!


// data,from a form on another page, that I want to insert in my db

$nom = $_POST['nom'];  
$prenom = $_POST['prenom'];  
$date = $_POST['date'];  
$identifiant = $_POST['identifiant'];  
$password = $_POST['password'];


// connexion to my database  
$connexion = mysql_connect("mysql5.000webh.com","a888888_user","mypassword");
mysql_select_db("a888888_mydatabase",$connexion);

// creation and sending of SQL query  
$requete = "insert into panel values   
('','$nom','$prenom','$date','$identifiant','$password')";
mysql_query($requete);

echo "Vos donnees ont ete envoyees !";  
include('page.html');

// closing Mysql connexion
mysql_close(); 

eric l
  • 1
  • 1
  • 3
    For a quick debug make the `mysql_query($requete)` line say: `mysql_query($requete) or die(mysql_error());` to give us some kind of error message. – Marcus Recck Jul 10 '12 at 01:43
  • I hope you're aware that this code is vulnerable to SQL injection - see http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php – John Carter Jul 10 '12 at 02:15
  • @MarcusRecck: TYVM! i did what you said and figured it out! the error message was "Column count doesn't match value count at row 1" I added one more value, checked, it worked! thanks again for your quick answer, have a nice day! – eric l Jul 10 '12 at 02:24
  • @therefromhere: i am now!... but what shall I do to my code to avoid it? – eric l Jul 10 '12 at 02:40
  • @ericl read the link I posted ;) – John Carter Jul 10 '12 at 03:51

2 Answers2

1

Instead of directly executing your query like,

mysql_query($requete);

Use a variable to fetch the result of the query using a variable like,

$result = mysql_query($requete);

Now use a simple check whether your query was executed or not by just using an if statement and then use the mysql_error() function to see the error.

if ( !$result ) {
    die( mysql_error() );
}
mushti
  • 11
  • 1
0

You didn't specify the fields in which to inser the values:

$requete = "insert into panel values   
('','$nom','$prenom','$date','$identifiant','$password')"
Wern Ancheta
  • 22,397
  • 38
  • 100
  • 139
  • 1
    That makes no sense, if the number of fields specified here equals to the number of fields in the database. They should match in number, order and type. – Lion Jul 10 '12 at 01:57
  • Hi! actually, my db_table had one more field, named access with a default value of 0, after password. I just thought I could omit in the line you quote because no value was inserted. – eric l Jul 10 '12 at 03:08