HI all i am trying to insert some values into database using html form, its giving me the error of Column count doesn't match value count at row 1 which i dont understand, ia new to php plz can anyone help me? Regards
php code
<?
if(isset($_POST['submit'])){
$userName = $_POST["userName"];
$FirstName =$_POST["FirstName"];
$LastName =$_POST["LastName"];
$stnumber = $_Post["ID"];
$class = $_POST["class"];
$subject = $_POST["subject"];
$grade = $_POST["grade"];
$password = $_POST["password"];
} else{
echo("wronggggggg nameeeeeeeeeeeee");
}
$stnumber = mysql_escape_string ("ID");
$userName = mysql_escape_string ("userName");
$password = mysql_escape_string ("password");
$FirstName = mysql_escape_string ("FirstName");
$LastName = mysql_escape_string ("LastName");
$class = mysql_escape_string ("class");
$subject = mysql_escape_string ("subject");
$grade = mysql_escape_string ("grade");
mysql_query ("INSERT INTO LOGIN
VALUES ('$stnumber','$userName','$password','$FirstName','$LastName','$stnumber','$class','$subject','$grade')") or die(mysql_error());
$sql = mysql_query("SELECT * FROM LOGIN WHERE userName ='$userName'");
//$row = mysql_query($sqlUser);
//$result = mysql_query($sql, $connection);
if(mysql_num_rows ($sql)> 0){
echo ("Upload sucessful");
} else
{
echo("Upload fail");
}
Try This Code
$insert_qry = mysql_query("INSERT INTO LOGIN
( ID
, userName
, password
, FirstName
, LastName
, class
, subject
, grade
) VALUES ('".$stnumber."','".$userName."','".$password."','".$FirstName."','".$LastName."','".$class."','".$subject."','".$grade."')") or die(mysql_error());
Check how many columns are in your table. It should be the exact number as how many you're inserting, ie, 9 (minus any autoincrement).
You either have to specify the fields that are to be used for the INSERT
, or correctly insert data to ALL of them.
I suggest that you try editing your query as follows (I have rather assumed the names of your fields):
INSERT INTO `LOGIN`
(`stnumber`, `username`, `password`, `forename`, `surname`, `stnumber`, `class`, `subject`, `grade`)
VALUES
('$stnumber','$userName','$password','$FirstName','$LastName','$stnumber','$class','$subject','$grade')
It's worth noting however that you're using a deprecated set of functions. You should investigate the use of MySQLi or PDO for better performance and longevity.
In this line:
mysql_query ("INSERT INTO LOGIN
VALUES ('$stnumber','$userName','$password','$FirstName','$LastName','$stnumber','$class','$subject','$grade')") or die(mysql_error());
You are using $stnumber
twice, I don't know if this is intended, but I'm guessing this probably added an extra value which shouldn't be there.
I find it best to include the column names in the INSERT
statement to explicitly define which values are being inserted into which column. For example:
INSERT INTO `table` ( `column_1`, `column_2` )
VALUES ( 'value_1', 'value_2' );
You may also consider reviewing PHP PDO as it is a much safer alternative to mysql_query
calls, which as of v5.5.0, will be deprecated.
If you provide your table structure, we may be able to identify exactly which columns are missing/extra from your INSERT statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.