I am working on building an employee database but seem to have run into an interesting issue.
When I run my query to add a new user/employee, I get the error:
Column count doesn't match value count at row 1
From what I have researched, this seems to be an error with inserting more/less values than what is declared in the first part of an insert statement example:
INSERT INTO table (col1, col2) VALUES (val1, val2, val3)
The thing is though, I have looked over my query and the columns and values match perfectly (count wise). I have even looked for things in my query such as missing quotes, commas, etc.
Here is my code (query):
$db->query("INSERT INTO userdata (
Username,
Email,
Phone,
Password,
FirstName,
LastName,
Address,
City,
State,
Zip,
JobTitle,
UserGroup,
JobStatus,
Points,
Status,
BirthDate,
HireDate,
HourlyRate,
DateAdded,
SSN
) VALUES (
'$Username',
'$Email',
'$Phone',
'$Password',
'$FirstName',
'$LastName',
'$Address',
'$City',
'$State',
'$Zip',
'$JobTitle',
'$Group',
'$JobStatus',
0,
'$Status',
'$BirthDate',
'$HireDate',
'$HourlyRate'
'$TodaysDate',
'$SSN'
)") or die(mysqli_error($db));
Some things to note:
This not all of the columns in the table have data inserted here (I think its possible to do this and things such as auto incrementing ID's will fill themselves in and others will be left blank)
From the variable dumps I have done, all of these variables are valid.
I am really confused about this and any help would be appreciated.
Check the following portion again:
INSERT INTO userdata(...,
JobStatus,
UserGroup,
Points,
UserGroup,
Status,
.....
,)VALUES(...,
$JobStatus',
0,
'$Group',
'$Status',
......
)
In values(, ?, ?, ?), after jobstatus, there should be UserGroup and then Points. And UserGroup appeared twice.
Related
I have two tables:
Invoices & Invoices Parts
The reason i'm using two tables is because i dont want to save the invoice parts array into one record on the Invoices table and i dont want to save it into multiple records on the Invoices table.
When i submit the data into both tables, how can i link both tables together with the ID if i dont have the ID yet?
$result = mysqli_query($mysqli, "INSERT INTO invoices(labelwarning, status, todaysdate, todaystime, todaysdatetime, avatar, firstname, lastname, mwaid, nextgenid, manager, manageremail, dispatcher, dispatcheremail, techemail, tag, serialnumber, currentequipment, company, address, city, state, zip, contactperson, contactnumber , currentdate, timearrived, timecompleted, login_id) VALUES('$labelwarning', '$status', '$todaysdate', '$todaystime', '$todaysdatetime', '$avatar', '$firstname', '$lastname', '$mwaid', '$nextgenid', '$manager', '$manageremail', '$dispatcher', '$dispatcheremail', '$techemail', '$tag', '$serialnumber', '$currentequipment', '$company', '$address', '$city', '$state', '$zip', '$contactperson', '$contactnumber ', '$currentdate', '$timearrived', '$loginId')");
$result2 = mysqli_query($mysqli, "INSERT INTO invoiceparts(partnumber, partdescription, partprice, partquantity, partdb, login_id) VALUES '$partnumberstring', '$partdescriptionstring', '$partpricestring', '$partquantitystring', '$partdbstring', '$loginId')");
If you need to link the "login_id" and the login_id is generated with AUTO_INCREMENT then you can do it by
for Mysqli connection, mysqli_insert_id($mysqli);
for PDO connection, $mysqli->lastInsertId();
If the login_id is not an AUTO_INCREMENT field then you can do it by
SELECT login_id FROM invoices ORDER BY login_id DESC LIMIT 1;
It'll return the last inserted row from there we can fetch the login_id
I will admit I am a newbie when it comes to PDO, but I have to change over a form that is in mysql.. I am getting connection, but nothing inserted.. I am truly stuck and feel like an idiot because I know it is something simple I am missing
I have tried having the arrays above and after the insert.. Neither work
Any help would be appreciated
Here is my code:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$STH = $conn->prepare("INSERT INTO PinTrade (ID, PIN, Year, Make, Model, Mileage, FirstName, LastName, Phone, Email, Date)
VALUES ('', '$pin', '$year', '$make', '$model', '$mileage', '$first', '$last', '$phone', '$email', '1234' )");
$STH->bindParam(':PIN', $_POST['pin']);
$STH->bindParam(':Year', $_POST['year']);
$STH->bindParam(':Make', $_POST['make']);
$STH->bindParam(':Model', $_POST['model']);
$STH->bindParam(':Mileage', $_POST['mileage']);
$STH->bindParam(':FirstName', $_POST['first']);
$STH->bindParam(':LastName', $_POST['last']);
$STH->bindParam(':Phone', $_POST['phone']);
$STH->bindParam(':Email', $_POST['email']);
$STH->execute();
Get rid of the dollar signs and quotes in your query values:
$STH = $conn->prepare("INSERT INTO PinTrade (ID, PIN, Year, Make,
Model, Mileage, FirstName, LastName, Phone, Email, Date)
VALUES (null, :PIN, :Year, :Make, //and so on....
Also note, assuming ID is an auto incrementing field, just insert null
VALUES (null, :PIN,
Finally, if you're pulling from the post array, I'd use bindValue over bindParam
Having a difficulty involving a form that wants the user to fill in his/her IP.
Gives the following error;
Warning: pg_query(): Query failed: ERROR: syntax error at or near
"'192.192.192.192/32'" LINE 1: ...ess, ip, status) VALUES ('1',
'TestFirstName', 'TestLastName' '192.192.1... ^ in
/Applications/XAMPP/xamppfiles/htdocs/LoginHQ/functions.php on line 22
Apparentely, something is going wrong with the IP form field that gets sent through a POST.
My query is as following;
$result = pg_query($dbconnection, "INSERT INTO clients(clientid, firstname, lastname, ipaddress, ip, status) VALUES ('$clientid', '$FirstName', '$LastName' '$ip', '$status')");
Is there something wrong with this formatting, or should I dig deeper?
You missed a comma between '$LastName' and '$ip'.Correct your query like this
$result = pg_query($dbconnection, "INSERT INTO clients(clientid, firstname, lastname, ipaddress, ip, status) VALUES ('$clientid', '$FirstName', '$LastName', '$ip', '$status')")
You're missing a , between '$LastName' and '$ip'.
You forgot a comma (,) between '$LastName' and '$ip'.
I'm trying to insert a new record in a MySQL database from PHP, which I've done a million times before, but for some reason, I can't get it to work this time, and it really bugs me.
Inserting strings into all the varchar collumns are going great, but when I get to inserting a value into the int column, I get an error telling me that I have a syntax error.
Basically, the first query works just fine, but the second one returns the error, and as you can see, I've made damn sure it really is an integer I'm trying to insert.
I hope somebody can help. I'm really starting to develop a headache over this :/
$groupId2 = 5;
$groupId = (int)$groupId2;
if(!mysqli_query($link, "INSERT INTO contestants (firstName, lastname, email) VALUES ('$firstName', '$lastName', '$email')"))
echo "First: " . mysqli_error($link);
if(!mysqli_query($link, "INSERT INTO contestants (firstName, lastname, email, group) VALUES ('$firstName', '$lastName', '$email', '$groupId')"))
echo "Second: " . mysqli_error($link);
group is a mysql keyword use back quotes around it
"INSERT INTO contestants (firstName, lastname, email, `group`)
VALUES ('$firstName', '$lastName', '$email', '$groupId')"
The error is because you surrounded your int with ' ', you need to get rid of your apostrophes and it will work just fine.
if(!mysqli_query($link,
"INSERT INTO contestants
(firstName, lastname, email, group) VALUES
('$firstName', '$lastName', '$email', $groupId)"))
^^^^^^^^^
To clarify, when inserting numerical fields you do not need them.
According to pst this is wrong, although, the fact you do not need single quotes is still correct.
I am really having a tough time understanding what syntax to use. I am VERY green when it comes to coding. I copied this code from another website that is working just fine but they are hooked up to a 4.3 version of mysql- can anyone help?This is the error-
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, city, state, zip, ) VALUES ('', , ' 1', '', '', '','' )' at line 1
Here is my code:
mysql_select_db("membership70", $con);
$name=mysql_real_escape_string($_POST['Name']);
$address1=mysql_real_escape_string($_POST['Address1']);
$city=mysql_real_escape_string($_POST['city']);
$state=mysql_real_escape_string($_POST['state']);
$zip=mysql_real_escape_string($_POST['zip']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
You have speces in address [space] 1 both places.
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
Also you have an extra colon after zip and the order of the values does not match the order you gave the column names. Try:
$sql="INSERT INTO Members (name, email, adress1, city, state, zip) VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip')";
Get rid of the space in adress 1 and your variable $adress 1....not allowed (spaces that is)
That statement clearly tells you the SQL syntax is broken. Take a closer look into your statement in the $sql variable. echo it to debug it properly.
$sql = "INSERT INTO .....";
echo $sql; die();
At first glace, there shouldn't be a blank neither the column adress nor the corresponding php variable. And you should sort the order of your columns. email is declare second, so don't put the value of it at the end.
So, instead of...
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
...try...
$sql="INSERT INTO Members (name, email, adress1, city, state, zip) VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip' )";
You have a column adress 1 there, that's not a valid column name in mysql and it could not have worked on any mysql version. What columns does your table have?
Also, your second value is empty, put some value in there (or a default one). And a comma at the end of the columns list. Email at the wrong place.
Maybe this would work?
$sql = "INSERT INTO Members (name, email, adress1, city, state, zip) ".
"VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip')";
(I'm not sure about the third column, should it spell address1?)