Difficulty processing form with IP - php

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

Related

php issue with inserting into sql

I am having an issue inserting into mysql db using php.
specifically this code is giving me problems:
$sql = "INSERT INTO '$mysql_database'.'$UsersTable' ('firstName', 'lastName', 'password', 'email','userType')VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
I can't seem to find the error with syntax here. All the information to me seems correct. where am I going wrong ?
try this
$sql = "INSERT INTO $mysql_database.$UsersTable (firstName, lastName, password, email,userType)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
remove Quote from table column
Try this query
$sql = "INSERT INTO $mysql_database.$UsersTable (firstName, lastName, password, email,userType)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
Hello you can't use single Quote from table column name so remove it
$sql = "INSERT INTO $mysql_database.$UsersTable (firstName, lastName, password, email,userType)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";
otherwise use this way
$sql = "INSERT INTO $mysql_database.$UsersTable (`firstName`, `lastName`, `password`, `email`,`userType`)VALUES ('$firstName', '$lastName', '$password','$email','$userType')";

INSERT results in DUPLICATE ENTRY '0' FOR KEY PRIMARY

I am using a simple php script to insert data into database but it's failing. The query just doesn't become successful without showing a single error which is why I am unable to figure out the problem. Some expert here help me please.
echo $name." ".$email." ".$pass." ".$phone." ".$area." ".$specialization." ".$city." ".$latitude." ".$longitude;
The result of echo is normal - without any null elements.
$query = mysqli_query($conn, "INSERT INTO users (name, email, pass, phone, area, specialization, hospital, city, latitude, longitude)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude') ");
if ($query) {
echo "Status: Registeration Successful!";
// creating directory for user and storing dummy profile picture
//mkdir('../profiles/'.$email_trim, 0777);
//$result_copy = copy("img/dp.jpg.jpg", "../profiles/".$email_trim."/dp.jpg.jpg");
} else {
echo "Status: Err";
}
This "Status: Err" is always printed. I don't know why.
P.S I have double checked the database the field labels are fine.
UPDATE 1:
I added the
die(mysqli_error($conn));
statement and it says "DUPLICATE ENTRY '0' FOR KEY PRIMARY'.
PROBLEM AND SOLUTION:
The issue was that I had an 'id' field which was primary key of the table but it was not set to AUTO_INCREMENT. So, whenever I tried to insert a new record, I was actually inserting entries with duplicate PKs which was the issue. I change it to AUTO_INCREMENT and it solved the problem.
It seems you try to insert a new element with a PK = 0, but there is already a record with this key !
What is the primary key of your table ? Do you use an "id" field which is not shown in your insert statement ? Is this field AUTO_INCREMENT ?
It would be helpful to see the structure of your 'users' table.
Wild guess: looks like you may have defined an "id" column (or with whatever other name) which is primary key with default value "0", but it's not auto increment. That way you can insert 1 row and it will get "0" as "id" column's value, but you cannot insert another row because it will also try to use default value "0", which cannot happen as primary key has to be unique.
If that is the case, then please alter users table and make sure that the primary key column is also 'auto increment'.
Please check the proper error by adding below mentioned code inside else:
echo mysqli_errno($conn) . '----' . mysqli_error($conn);
<?php
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
$query = mysqli_query($conn, "INSERT INTO `users` (`name`, `email`, `pass`, `phone`, `area`, `specialization`, `hospital`, `city`, `latitude`, `longitude`)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude') ") or die(mysqli_error());
$query = mysqli_query($conn, "INSERT INTO `users` (`name`, `email`, `pass`, `phone`, `area`, `specialization`, `hospital`, `city`, `latitude`, `longitude`)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude') ");
use ` Tick maybe because there's some reserved word in your fields.
I think you doubled your close parenthesis and do not put $conn inside the query..
$query = "INSERT INTO users (name, email, pass, phone, area, specialization, hospital, city, latitude, longitude)
VALUES ('$name', '$email', '$pass', '$phone', '$area', '$specialization', '$hospital', '$city', '$latitude', '$longitude')";
mysqli_query($query, $conn);
Something like this. I hope this helps

Column Count vs Value Count on insert statement - PHP/MySQL

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.

Inserting Integer value into mysql int using INSERT

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.

Current syntax error when connecting to version 5.0 mysql

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?)

Categories