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

5
  • 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. Commented Jul 10, 2012 at 1:43
  • I hope you're aware that this code is vulnerable to SQL injection - see stackoverflow.com/questions/60174/… Commented Jul 10, 2012 at 2: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! Commented Jul 10, 2012 at 2:24
  • @therefromhere: i am now!... but what shall I do to my code to avoid it? Commented Jul 10, 2012 at 2:40
  • @ericl read the link I posted ;) Commented Jul 10, 2012 at 3:51

2 Answers 2

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() );
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

$requete = "insert into panel values   
('','$nom','$prenom','$date','$identifiant','$password')"

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.